Rev 54 | Rev 64 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | // san.cpp |
2 | |||
3 | #include "common.h" |
||
4 | |||
5 | |||
6 | // handy definitions |
||
7 | #define WCHAR_TO_COLUMN(a) ((a) == L'a' ? 0 : ((a) == L'b' ? 1 : ((a) == L'c' ? 2 : ((a) == L'd' ? 3 : ((a) == L'e' ? 4 : ((a) == L'f' ? 5 : ((a) == L'g' ? 6 : ((a) == L'h' ? 7 : -1)))))))) |
||
8 | #define WCHAR_TO_LINE(a) ((a) == L'1' ? 0 : ((a) == L'2' ? 1 : ((a) == L'3' ? 2 : ((a) == L'4' ? 3 : ((a) == L'5' ? 4 : ((a) == L'6' ? 5 : ((a) == L'7' ? 6 : ((a) == L'8' ? 7 : -1)))))))) |
||
9 | #define COLUMN_TO_WSTRING(c) ((c) == 0 ? L"a" : ((c) == 1 ? L"b" : ((c) == 2 ? L"c" : ((c) == 3 ? L"d" : ((c) == 4 ? L"e" : ((c) == 5 ? L"f" : ((c) == 6 ? L"g" : ((c) == 7 ? L"h" : L"_")))))))) |
||
10 | #define LINE_TO_WSTRING(l) ((l) == 0 ? L"1" : ((l) == 1 ? L"2" : ((l) == 2 ? L"3" : ((l) == 3 ? L"4" : ((l) == 4 ? L"5" : ((l) == 5 ? L"6" : ((l) == 6 ? L"7" : ((l) == 7 ? L"8" : L"_")))))))) |
||
11 | |||
12 | |||
13 | // prototypes of local functions |
||
14 | static bool SAN_DisambiguateMove (boardmove_t *move, boardmove_t *new_move); |
||
15 | |||
16 | |||
17 | bool Move_SetupFromSAN (boardmove_t *move, boardmove_t *new_move, int move_color) |
||
18 | { |
||
19 | // helper function to translate a SAN move into numeric from and to positions on the specified board |
||
20 | // WARNING: THIS FUNCTION DOES NOT RETURN WITH A FULLY CONSTRUCTED MOVE! |
||
21 | |||
54 | pmbaty | 22 | // FIXME: accelerate this function by reducing the number of towupper()/towlower() calls |
23 | |||
1 | pmbaty | 24 | int fieldstart; |
25 | int fieldstop; |
||
26 | int length; |
||
27 | |||
28 | // first, get string length |
||
29 | length = wcslen (new_move->pgntext); |
||
30 | |||
31 | // parse it from the beginning |
||
32 | fieldstart = 0; |
||
33 | fieldstop = 0; |
||
34 | |||
35 | // find where it stops |
||
36 | while ((fieldstop < length) && !iswspace (new_move->pgntext[fieldstop])) |
||
37 | fieldstop++; // reach the next space |
||
38 | |||
39 | fieldstop--; // ignore the last space |
||
40 | |||
41 | // reset relevant parts of the move we're about to build |
||
42 | new_move->color = move_color; // save its color |
||
43 | new_move->part = PART_NONE; |
||
44 | new_move->promotion_type = PART_NONE; |
||
45 | new_move->source[0] = -1; |
||
46 | new_move->source[1] = -1; |
||
47 | new_move->target[0] = -1; |
||
48 | new_move->target[1] = -1; |
||
49 | |||
50 | // does it finish with a move value estimation ? |
||
51 | while ((fieldstop >= fieldstart) && ((new_move->pgntext[fieldstop] == L'?') || (new_move->pgntext[fieldstop] == L'!'))) |
||
52 | fieldstop--; // chop off these signs, they are redundant |
||
53 | |||
54 | // does it finish with a check or a checkmate sign ? |
||
55 | if ((fieldstop >= fieldstart) && ((new_move->pgntext[fieldstop] == L'+') || (new_move->pgntext[fieldstop] == L'#'))) |
||
56 | fieldstop--; // chop off these signs, they are redundant |
||
57 | |||
58 | // is it a long castle ? |
||
54 | pmbaty | 59 | if ((_wcsnicmp (&new_move->pgntext[fieldstart], L"O-O-O", 5) == 0) || (wcsncmp (&new_move->pgntext[fieldstart], L"0-0-0", 5) == 0)) |
1 | pmbaty | 60 | { |
61 | new_move->source[0] = (move_color == COLOR_WHITE ? 0 : 7); |
||
62 | new_move->source[1] = 4; |
||
63 | new_move->target[0] = (move_color == COLOR_WHITE ? 0 : 7); |
||
64 | new_move->target[1] = 2; |
||
65 | new_move->part = PART_KING; // it's a king's move |
||
66 | } |
||
67 | |||
68 | // else is it a short castle ? |
||
54 | pmbaty | 69 | else if ((_wcsnicmp (&new_move->pgntext[fieldstart], L"O-O", 3) == 0) || (wcsncmp (&new_move->pgntext[fieldstart], L"0-0", 3) == 0)) |
1 | pmbaty | 70 | { |
71 | new_move->source[0] = (move_color == COLOR_WHITE ? 0 : 7); |
||
72 | new_move->source[1] = 4; |
||
73 | new_move->target[0] = (move_color == COLOR_WHITE ? 0 : 7); |
||
74 | new_move->target[1] = 6; |
||
75 | new_move->part = PART_KING; // it's a king's move |
||
76 | } |
||
77 | |||
78 | // else it's a normal move |
||
79 | else |
||
80 | { |
||
81 | // is the last character a part type ? |
||
54 | pmbaty | 82 | if ((fieldstop >= fieldstart) && (towupper (new_move->pgntext[fieldstop]) == L'R')) |
1 | pmbaty | 83 | { |
84 | new_move->promotion_type = PART_ROOK; // there's a promotion to rook |
||
85 | fieldstop--; // proceed to previous character |
||
86 | } |
||
54 | pmbaty | 87 | else if ((fieldstop >= fieldstart) && (towupper (new_move->pgntext[fieldstop]) == L'N')) |
1 | pmbaty | 88 | { |
89 | new_move->promotion_type = PART_KNIGHT; // there's a promotion to knight |
||
90 | fieldstop--; // proceed to previous character |
||
91 | } |
||
54 | pmbaty | 92 | else if ((fieldstop >= fieldstart) && (towupper (new_move->pgntext[fieldstop]) == L'B')) |
1 | pmbaty | 93 | { |
94 | new_move->promotion_type = PART_BISHOP; // there's a promotion to bishop |
||
95 | fieldstop--; // proceed to previous character |
||
96 | } |
||
54 | pmbaty | 97 | else if ((fieldstop >= fieldstart) && (towupper (new_move->pgntext[fieldstop]) == L'Q')) |
1 | pmbaty | 98 | { |
99 | new_move->promotion_type = PART_QUEEN; // there's a promotion to queen |
||
100 | fieldstop--; // proceed to previous character |
||
101 | } |
||
102 | |||
103 | // is there the promotion's equal sign ? |
||
104 | if ((fieldstop >= fieldstart) && (new_move->pgntext[fieldstop] == L'=')) |
||
105 | fieldstop--; // skip it, it's redundant |
||
106 | |||
107 | // read the target line and column |
||
108 | if (fieldstop >= fieldstart) |
||
109 | { |
||
110 | new_move->target[0] = WCHAR_TO_LINE (new_move->pgntext[fieldstop]); // read the target line |
||
111 | fieldstop--; // proceed to previous character |
||
112 | } |
||
113 | if (fieldstop >= fieldstart) |
||
114 | { |
||
63 | pmbaty | 115 | new_move->target[1] = WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]); // read the target column (WARNING: ONLY IF LOWERCASE) |
1 | pmbaty | 116 | fieldstop--; // proceed to previous character |
117 | } |
||
118 | |||
119 | if ((fieldstop >= fieldstart) && (new_move->pgntext[fieldstop] == L'x')) |
||
120 | fieldstop--; // if there's a taking piece indication, it's superfluous, so skip it |
||
121 | |||
122 | // read the eventual source line and column |
||
123 | if ((fieldstop >= fieldstart) && (WCHAR_TO_LINE (new_move->pgntext[fieldstop]) != -1)) |
||
124 | { |
||
125 | new_move->source[0] = WCHAR_TO_LINE (new_move->pgntext[fieldstop]); // read the source line |
||
126 | fieldstop--; // proceed to previous character |
||
127 | } |
||
63 | pmbaty | 128 | if ((fieldstop >= fieldstart) && (WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]) != -1)) |
1 | pmbaty | 129 | { |
63 | pmbaty | 130 | new_move->source[1] = WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]); // read the source column (WARNING: ONLY IF LOWERCASE) |
1 | pmbaty | 131 | fieldstop--; // proceed to previous character |
132 | } |
||
133 | |||
134 | // read the part's type |
||
135 | if (fieldstop >= fieldstart) |
||
136 | { |
||
54 | pmbaty | 137 | if (towupper (new_move->pgntext[fieldstop]) == L'R') |
1 | pmbaty | 138 | new_move->part = PART_ROOK; // it's a rook |
54 | pmbaty | 139 | else if (towupper (new_move->pgntext[fieldstop]) == L'N') |
1 | pmbaty | 140 | new_move->part = PART_KNIGHT; // it's a knight |
54 | pmbaty | 141 | else if (towupper (new_move->pgntext[fieldstop]) == L'B') |
1 | pmbaty | 142 | new_move->part = PART_BISHOP; // it's a bishop |
54 | pmbaty | 143 | else if (towupper (new_move->pgntext[fieldstop]) == L'Q') |
1 | pmbaty | 144 | new_move->part = PART_QUEEN; // it's a queen |
54 | pmbaty | 145 | else if (towupper (new_move->pgntext[fieldstop]) == L'K') |
1 | pmbaty | 146 | new_move->part = PART_KING; // it's a king |
147 | else |
||
148 | return (false); // on error, cancel |
||
149 | } |
||
150 | else |
||
151 | new_move->part = PART_PAWN; // if not specified, it's a pawn |
||
152 | } |
||
153 | |||
154 | // now, disambiguate. |
||
155 | return (SAN_DisambiguateMove (move, new_move)); |
||
156 | } |
||
157 | |||
158 | |||
159 | bool Move_DescribeInSAN (boardmove_t *move) |
||
160 | { |
||
161 | // convert a board and its part placements into a SAN notation, writing in the pgntext buffer |
||
162 | |||
163 | int line; |
||
164 | int column; |
||
165 | boardslot_t *slot; |
||
166 | bool needs_line; |
||
167 | bool needs_column; |
||
168 | |||
169 | // build move string in abbreviated algebraic notation |
||
170 | move->pgntext[0] = 0; |
||
171 | |||
172 | // is it a king castling or a normal move ? |
||
173 | if ((move->part == PART_KING) && (move->source[1] == 4) && (move->target[1] == 2)) |
||
174 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"O-O-O"); // long castle |
||
175 | else if ((move->part == PART_KING) && (move->source[1] == 4) && (move->target[1] == 6)) |
||
176 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"O-O"); // short castle |
||
177 | else |
||
178 | { |
||
179 | // part identifier (omit it if pawn) |
||
180 | if (move->part == PART_ROOK) |
||
181 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"R"); |
||
182 | else if (move->part == PART_KNIGHT) |
||
183 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"N"); |
||
184 | else if (move->part == PART_BISHOP) |
||
185 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"B"); |
||
186 | else if (move->part == PART_QUEEN) |
||
187 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"Q"); |
||
188 | else if (move->part == PART_KING) |
||
189 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"K"); |
||
190 | |||
191 | // is it a pawn taking ? |
||
192 | if ((move->part == PART_PAWN) && (move->has_captured)) |
||
193 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->source[1])); |
||
194 | |||
195 | // else is there a possible ambiguity ? (not for pawns) |
||
196 | if ((move->part != PART_PAWN) && (Move_CountPartsByColorAndType (move, move->color, move->part) > 1)) |
||
197 | { |
||
198 | // assume we don't need to disambiguate neither by column nor by line |
||
199 | needs_column = false; |
||
200 | needs_line = false; |
||
201 | |||
202 | // cycle through the board and find all this part's siblings (be sure to parse them all) |
||
203 | for (line = 0; line < 8; line++) |
||
204 | for (column = 0; column < 8; column++) |
||
205 | { |
||
206 | slot = &move->slots[line][column]; // quick access to slot |
||
207 | |||
208 | // is this slot occupied by one of our siblings that is not the one doing the move AND its movement is valid ? |
||
209 | if ((slot->part == move->part) && (slot->color == move->color) && ((line != move->source[0]) || (column != move->source[1])) |
||
210 | && Move_IsMoveValid (move, line, column, move->target[0], move->target[1])) |
||
211 | { |
||
212 | if (column != move->source[1]) |
||
213 | needs_column = true; // if columns differ, remember to write start column |
||
214 | else |
||
215 | needs_line = true; // else if lines differ, remember to write start line |
||
216 | } |
||
217 | } |
||
218 | |||
219 | // do we need to write start column or start line ? |
||
220 | if (needs_column) |
||
221 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->source[1])); // if so, write start column |
||
222 | if (needs_line) |
||
223 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), LINE_TO_WSTRING (move->source[0])); // if so, write start line |
||
224 | } |
||
225 | |||
226 | // does it capture something ? |
||
227 | if (move->has_captured) |
||
228 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"x"); |
||
229 | |||
230 | // target column, target line |
||
231 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->target[1])); |
||
232 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), LINE_TO_WSTRING (move->target[0])); |
||
233 | |||
234 | // is there a promotion ? |
||
235 | if (move->promotion_type == PART_ROOK) |
||
236 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=R"); |
||
237 | else if (move->promotion_type == PART_KNIGHT) |
||
238 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=N"); |
||
239 | else if (move->promotion_type == PART_BISHOP) |
||
240 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=B"); |
||
241 | else if (move->promotion_type == PART_QUEEN) |
||
242 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=Q"); |
||
243 | else if (move->promotion_type == PART_KING) |
||
244 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=K"); |
||
245 | |||
246 | // is there a check or a checkmate ? |
||
247 | if (move->is_check) |
||
248 | { |
||
249 | if (move->is_stalemate) |
||
250 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"#"); // checkmate |
||
251 | else |
||
252 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"+"); // normal check |
||
253 | } |
||
254 | |||
255 | // is it an en passant coup ? |
||
256 | if (move->is_enpassant) |
||
257 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"e.p."); |
||
258 | } |
||
259 | |||
260 | return (true); // finished |
||
261 | } |
||
262 | |||
263 | |||
264 | static bool SAN_DisambiguateMove (boardmove_t *move, boardmove_t *new_move) |
||
265 | { |
||
266 | // handy helper to disambiguate a move literal of which we don't know the source, |
||
267 | // but for which we know the part type and the target slot |
||
268 | |||
269 | int index_line; |
||
270 | int index_column; |
||
271 | |||
272 | // are both source line and column known ? |
||
273 | if ((new_move->source[0] != -1) && (new_move->source[1] != -1)) |
||
274 | return (true); // no need to disambiguate anything |
||
275 | |||
276 | // else is source line known ? |
||
277 | else if (new_move->source[0] != -1) |
||
278 | { |
||
279 | // cycle through all the columns and find the part of the same type that has the right to move there |
||
280 | for (index_column = 0; index_column < 8; index_column++) |
||
281 | if ((move->slots[new_move->source[0]][index_column].color == new_move->color) |
||
282 | && (move->slots[new_move->source[0]][index_column].part == new_move->part) |
||
283 | && Move_IsMoveValid (move, new_move->source[0], index_column, new_move->target[0], new_move->target[1])) |
||
284 | { |
||
285 | new_move->source[1] = index_column; // save column |
||
286 | return (true); // we've found it, stop searching |
||
287 | } |
||
288 | } |
||
289 | |||
290 | // else is source column known ? |
||
291 | else if (new_move->source[1] != -1) |
||
292 | { |
||
293 | // cycle through all the lines and find the part of the same type that has the right to move there |
||
294 | for (index_line = 0; index_line < 8; index_line++) |
||
295 | if ((move->slots[index_line][new_move->source[1]].color == new_move->color) |
||
296 | && (move->slots[index_line][new_move->source[1]].part == new_move->part) |
||
297 | && Move_IsMoveValid (move, index_line, new_move->source[1], new_move->target[0], new_move->target[1])) |
||
298 | { |
||
299 | new_move->source[0] = index_line; // save line |
||
300 | return (true); // we've found it, stop searching |
||
301 | } |
||
302 | } |
||
303 | |||
304 | // else neither source line nor column is known |
||
305 | else |
||
306 | { |
||
307 | // cycle through all the board and find the part of the same type that has the right to move there |
||
308 | for (index_line = 0; index_line < 8; index_line++) |
||
309 | for (index_column = 0; index_column < 8; index_column++) |
||
310 | if ((move->slots[index_line][index_column].color == new_move->color) |
||
311 | && (move->slots[index_line][index_column].part == new_move->part) |
||
312 | && Move_IsMoveValid (move, index_line, index_column, new_move->target[0], new_move->target[1])) |
||
313 | { |
||
314 | new_move->source[0] = index_line; // save line and column |
||
315 | new_move->source[1] = index_column; |
||
316 | return (true); // we've found it, stop searching |
||
317 | } |
||
318 | } |
||
319 | |||
320 | return (false); // no possibility found |
||
321 | } |