-
Notifications
You must be signed in to change notification settings - Fork 3
/
GobanState.cpp
executable file
·313 lines (268 loc) · 9.42 KB
/
GobanState.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* @file GobanState.cpp
* @ingroup Go-CamRecorder
* @author Dominique Vaufreydaz, personnal project
* @copyright All right reserved.
*/
#include "GobanState.h"
// Validdate capture, i.e do not find any liberty
void GobanState::CheckLibertiesRecursive( int a, int b, int StoneColor, Omiscid::SimpleList<cv::Point>& CurrentChain )
{
if ( Goban[a][b].CaptureChecked == true )
{
// Already check, current validation is still questionned, go ahead
return;
}
// Check if Current is Empty
if ( Goban[a][b].State == Empty )
{
// Found liberty, go out!
throw Omiscid::SimpleException( "Liberty found" );
}
// if Stone is from another color, no liberty
if ( Goban[a][b].State != StoneColor )
{
return;
}
// Ok, we have check this stone, add it to the current chain
Goban[a][b].CaptureChecked = true;
CurrentChain.AddTail( cv::Point( a, b ) );
// Check points in a cross manner, TO CHECK : do we need to check diagonals?
if ( a > 0 )
{
CheckLibertiesRecursive( a-1, b, StoneColor, CurrentChain );
}
if ( a < (NumCells-1) )
{
CheckLibertiesRecursive( a+1, b, StoneColor, CurrentChain );
}
if ( b > 0 )
{
CheckLibertiesRecursive( a, b-1, StoneColor, CurrentChain );
}
if ( b < (NumCells-1) )
{
CheckLibertiesRecursive( a, b+1, StoneColor, CurrentChain );
}
}
bool GobanState::CheckLiberties( int a, int b, int StoneColor, StoneDetector( &AllDetectors )[MaxNumCells][MaxNumCells], double CurrentTimestamp )
{
if ( Goban[a][b].State != StoneColor )
{
// Not a stone in the opposite color, do not check
return false;
}
Omiscid::SimpleList<cv::Point> CurrentChain;
try
{
CheckLibertiesRecursive( a, b, StoneColor, CurrentChain );
// Here, capture is validated (not exception thrown), remove stone
for ( CurrentChain.First(); CurrentChain.NotAtEnd(); CurrentChain.Next() )
{
cv::Point& p = CurrentChain.GetCurrent();
Goban[p.x][p.y].State = Empty;
Goban[p.x][p.y].CaptureChecked = false;
AllDetectors[p.x][p.y].State = Empty;
AllDetectors[p.x][p.y].Timestamp = CurrentTimestamp;
}
return true;
}
catch ( Omiscid::SimpleException& /* Exception */ )
{
// Here Liberty found, remove CaptureChecked flag
for ( CurrentChain.First(); CurrentChain.NotAtEnd(); CurrentChain.Next() )
{
cv::Point& p = CurrentChain.GetCurrent();
Goban[p.x][p.y].CaptureChecked = false;
}
// liberty found
return false;
}
}
bool GobanState::ValidateCapture( int a, int b, int StoneColor, StoneDetector( &AllDetectors )[MaxNumCells][MaxNumCells], double CurrentTimestamp )
{
// Check points in a crossed manner
if ( a > 0 )
{
CheckLiberties( a-1, b, StoneColor, AllDetectors, CurrentTimestamp );
}
if ( a < (NumCells-1) )
{
CheckLiberties( a+1, b, StoneColor, AllDetectors, CurrentTimestamp );
}
if ( b > 0 )
{
CheckLiberties( a, b-1, StoneColor, AllDetectors, CurrentTimestamp );
}
if ( b < (NumCells-1) )
{
CheckLiberties( a, b+1, StoneColor, AllDetectors, CurrentTimestamp );
}
return true;
}
// Could be done in one pass by making an ordered list of new event...
bool GobanState::LookupForOlderEvent( StoneDetector( &AllDetectors )[MaxNumCells][MaxNumCells], double CurrentTimestamp, Omiscid::SimpleString Comment /* = "" */, bool EndKifu /* = false */ )
{
int posa = -1, posb = -1;
// +1.0 will include current event
double lTimestamp = CurrentTimestamp + 1.0;
int NbNewFoundOlder = 0;
for ( int a = 0; a < NumCells; a++ )
{
for ( int b = 0; b < NumCells; b++ )
{
// Do not consider moving cells
if ( AllDetectors[a][b].InMotionExtended == true )
{
continue;
}
int CurrentStoneState = AllDetectors[a][b].State; // ComputeAndRetrieveState(CurrentTimestamp);
if ( CurrentStoneState != Goban[a][b].State && // Is it a different event, aka new event?
(CurrentTimestamp - AllDetectors[a][b].Timestamp) >= 5.0 ) // Older enough?
{
if ( CurrentStoneState == Empty )
{
// Ok, back to empty state
if ( (CurrentTimestamp - AllDetectors[a][b].Timestamp) >= 10.0 )
{
Goban[a][b].State = Empty;
}
// Do not count it as event
continue;
}
// New event, not empty
NbNewFoundOlder++;
if ( CurrentStoneState == SearchFor && AllDetectors[a][b].Timestamp < lTimestamp )
{
// Do we already found a search event?
if ( posa == -1 )
{
// no, first one !
posa = a;
posb = b;
}
else
{
if ( AllDetectors[a][b].Timestamp > AllDetectors[a][b].Timestamp )
{
// New oldest event of the good color
posa = a;
posb = b;
}
// Not better, we already count it, and we keep previous posa, posb event
}
}
}
}
}
// did we found an event ?
if ( posa >= 0 )
{
// yes, fixe it !
Goban[posa][posb].State = SearchFor;
AllDetectors[posa][posb].Fixed = true; // Still interesting?
// Add move to the Kifu sgf file
SGFWriter.AddMove( SearchFor, posa, posb, Comment );
// Switch for next search
SwitchState();
// Check if we need to remove stones!
// Use new value for SearchFor (that has been switched) as color for possibly removal stones
ValidateCapture( posa, posb, SearchFor, AllDetectors, CurrentTimestamp );
return true;
}
// Not found an event but with the wrong color, add it but set a comment on this stone
if ( NbNewFoundOlder >= 1 )
{
// here, we have 2 consecutive bad event, do not wait longer to solve it...
// call recursively, we already know that the state will match
SwitchState();
return LookupForOlderEvent( AllDetectors, CurrentTimestamp, "Check this move" );
}
// Here not found, or only 1 event but with the wrong color, wait for next event to try to solve it
return false;
}
void GobanState::UpdateCurrentState( StoneDetector( &AllDetectors )[MaxNumCells][MaxNumCells], double CurrentTimestamp )
{
// Search iteratively alternatively for stones: black, white, black, white...
while ( LookupForOlderEvent( AllDetectors, CurrentTimestamp ) );
}
// Draw a empty goban centered within the drawing area
void GobanState::DrawGoban( cv::Mat& WhereToDraw )
{
// Compute goban size
int minSize = Min( WhereToDraw.cols, WhereToDraw.rows );
// NumCells for the grid +1 for the space arround the goban
int DrawingCellSize = minSize/(NumCells+1);
// Compute center
int CenterX = WhereToDraw.cols/2;
int CenterY = WhereToDraw.rows/2;
// Comput starting point from center of image
int StartCol = CenterX - DrawingCellSize*(NumCells/2);
int StartRow = CenterY - DrawingCellSize*(NumCells/2);
// Fond blanc
WhereToDraw = cv::Scalar( 255, 255, 255 );
// Draw goban
cv::rectangle( WhereToDraw, cv::Rect( StartCol-DrawingCellSize/2, StartRow-DrawingCellSize/2, DrawingCellSize*NumCells, DrawingCellSize*NumCells ), cv::Scalar( 110, 190, 235 ), -1 );
// Draw lines
int LineSize = (NumCells-1)*DrawingCellSize;
for ( int a = 0; a < NumCells; a++ )
{
cv::line( WhereToDraw, cv::Point( StartCol+a*DrawingCellSize, StartRow ), cv::Point( StartCol+a*DrawingCellSize, StartRow+LineSize ), cv::Scalar( 0, 0, 0 ), 2 );
cv::line( WhereToDraw, cv::Point( StartCol, StartRow+a*DrawingCellSize ), cv::Point( StartCol+LineSize, StartRow+a*DrawingCellSize ), cv::Scalar( 0, 0, 0 ), 2 );
}
// TO CHECK, point name
// Draw points on goban
if ( NumCells == 7 )
{
for ( int a = 3; a < NumCells; a += NumCells/2 )
{
for ( int b = 3; b < NumCells; b += 6 )
{
cv::circle( WhereToDraw, cv::Point( StartCol+(a)*DrawingCellSize, StartRow+(b)*DrawingCellSize ), DrawingCellSize*15/100, cv::Scalar( 0, 0, 0 ), -1 );
}
}
}
else
{
for ( int a = 3; a < NumCells; a += 6 )
{
for ( int b = 3; b < NumCells; b += 6 )
{
cv::circle( WhereToDraw, cv::Point( StartCol+(a)*DrawingCellSize, StartRow+(b)*DrawingCellSize ), DrawingCellSize*15/100, cv::Scalar( 0, 0, 0 ), -1 );
}
}
}
// TO CHECK: other non standard size?
}
void GobanState::DrawCurrentState( cv::Mat& WhereToDraw, double CurrentTimestamp )
{
// Compute goban size
int minSize = Min( WhereToDraw.cols, WhereToDraw.rows );
// NumCells for the grid +1 for the space arround the goban
int DrawingCellSize = minSize/(NumCells+1);
int DrawingStoneSize = (DrawingCellSize/2)*90/100;
// Comput starting point from center of image
int StartCol = WhereToDraw.cols/2-DrawingCellSize*(NumCells/2);
int StartRow = WhereToDraw.rows/2-DrawingCellSize*(NumCells/2);
DrawGoban( WhereToDraw );
// Draw stones if any
int colpos = StartCol;
for ( int a = 0; a < NumCells; a++, colpos += DrawingCellSize )
{
int rowpos = StartRow;
for ( int b = 0; b < NumCells; b++, rowpos += DrawingCellSize )
{
if ( Goban[a][b].State == StoneDetector::Black )
{
// AllDetectors[a][b].Fixed = true;
cv::circle( WhereToDraw, cv::Point( StartCol+(a)*DrawingCellSize, StartRow+(b)*DrawingCellSize ), DrawingStoneSize, cv::Scalar( 0, 0, 0 ), -1 );
}
else if ( Goban[a][b].State == StoneDetector::White )
{
// AllDetectors[a][b].Fixed = true;
cv::circle( WhereToDraw, cv::Point( StartCol+(a)*DrawingCellSize, StartRow+(b)*DrawingCellSize ), DrawingStoneSize, cv::Scalar( 20, 20, 20 ), -1 );
cv::circle( WhereToDraw, cv::Point( StartCol+(a)*DrawingCellSize, StartRow+(b)*DrawingCellSize ), DrawingStoneSize-2, cv::Scalar( 255, 255, 255 ), -1 );
}
}
}
}