Rev 63 | Rev 65 | 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 | { |
||
64 | pmbaty | 137 | if (towupper (new_move->pgntext[fieldstop]) == L'R') new_move->part = PART_ROOK; // it's a rook |
138 | else if (towupper (new_move->pgntext[fieldstop]) == L'N') new_move->part = PART_KNIGHT; // it's a knight |
||
139 | else if (towupper (new_move->pgntext[fieldstop]) == L'B') new_move->part = PART_BISHOP; // it's a bishop |
||
140 | else if (towupper (new_move->pgntext[fieldstop]) == L'Q') new_move->part = PART_QUEEN; // it's a queen |
||
141 | else if (towupper (new_move->pgntext[fieldstop]) == L'K') new_move->part = PART_KING; // it's a king |
||
142 | else if (towupper (new_move->pgntext[fieldstop]) == L'P') new_move->part = PART_PAWN; // it's a pawn (Wikipedia says "P" is a valid part type in PGN texts...) |
||
143 | else return (false); // on error, cancel |
||
1 | pmbaty | 144 | } |
145 | else |
||
146 | new_move->part = PART_PAWN; // if not specified, it's a pawn |
||
147 | } |
||
148 | |||
149 | // now, disambiguate. |
||
150 | return (SAN_DisambiguateMove (move, new_move)); |
||
151 | } |
||
152 | |||
153 | |||
154 | bool Move_DescribeInSAN (boardmove_t *move) |
||
155 | { |
||
156 | // convert a board and its part placements into a SAN notation, writing in the pgntext buffer |
||
157 | |||
158 | int line; |
||
159 | int column; |
||
160 | boardslot_t *slot; |
||
161 | bool needs_line; |
||
162 | bool needs_column; |
||
163 | |||
164 | // build move string in abbreviated algebraic notation |
||
165 | move->pgntext[0] = 0; |
||
166 | |||
167 | // is it a king castling or a normal move ? |
||
168 | if ((move->part == PART_KING) && (move->source[1] == 4) && (move->target[1] == 2)) |
||
169 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"O-O-O"); // long castle |
||
170 | else if ((move->part == PART_KING) && (move->source[1] == 4) && (move->target[1] == 6)) |
||
171 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"O-O"); // short castle |
||
172 | else |
||
173 | { |
||
174 | // part identifier (omit it if pawn) |
||
175 | if (move->part == PART_ROOK) |
||
176 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"R"); |
||
177 | else if (move->part == PART_KNIGHT) |
||
178 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"N"); |
||
179 | else if (move->part == PART_BISHOP) |
||
180 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"B"); |
||
181 | else if (move->part == PART_QUEEN) |
||
182 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"Q"); |
||
183 | else if (move->part == PART_KING) |
||
184 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"K"); |
||
185 | |||
186 | // is it a pawn taking ? |
||
187 | if ((move->part == PART_PAWN) && (move->has_captured)) |
||
188 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->source[1])); |
||
189 | |||
190 | // else is there a possible ambiguity ? (not for pawns) |
||
191 | if ((move->part != PART_PAWN) && (Move_CountPartsByColorAndType (move, move->color, move->part) > 1)) |
||
192 | { |
||
193 | // assume we don't need to disambiguate neither by column nor by line |
||
194 | needs_column = false; |
||
195 | needs_line = false; |
||
196 | |||
197 | // cycle through the board and find all this part's siblings (be sure to parse them all) |
||
198 | for (line = 0; line < 8; line++) |
||
199 | for (column = 0; column < 8; column++) |
||
200 | { |
||
201 | slot = &move->slots[line][column]; // quick access to slot |
||
202 | |||
203 | // is this slot occupied by one of our siblings that is not the one doing the move AND its movement is valid ? |
||
204 | if ((slot->part == move->part) && (slot->color == move->color) && ((line != move->source[0]) || (column != move->source[1])) |
||
205 | && Move_IsMoveValid (move, line, column, move->target[0], move->target[1])) |
||
206 | { |
||
207 | if (column != move->source[1]) |
||
208 | needs_column = true; // if columns differ, remember to write start column |
||
209 | else |
||
210 | needs_line = true; // else if lines differ, remember to write start line |
||
211 | } |
||
212 | } |
||
213 | |||
214 | // do we need to write start column or start line ? |
||
215 | if (needs_column) |
||
216 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->source[1])); // if so, write start column |
||
217 | if (needs_line) |
||
218 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), LINE_TO_WSTRING (move->source[0])); // if so, write start line |
||
219 | } |
||
220 | |||
221 | // does it capture something ? |
||
222 | if (move->has_captured) |
||
223 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"x"); |
||
224 | |||
225 | // target column, target line |
||
226 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->target[1])); |
||
227 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), LINE_TO_WSTRING (move->target[0])); |
||
228 | |||
229 | // is there a promotion ? |
||
230 | if (move->promotion_type == PART_ROOK) |
||
231 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=R"); |
||
232 | else if (move->promotion_type == PART_KNIGHT) |
||
233 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=N"); |
||
234 | else if (move->promotion_type == PART_BISHOP) |
||
235 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=B"); |
||
236 | else if (move->promotion_type == PART_QUEEN) |
||
237 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=Q"); |
||
238 | else if (move->promotion_type == PART_KING) |
||
239 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=K"); |
||
240 | |||
241 | // is there a check or a checkmate ? |
||
242 | if (move->is_check) |
||
243 | { |
||
244 | if (move->is_stalemate) |
||
245 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"#"); // checkmate |
||
246 | else |
||
247 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"+"); // normal check |
||
248 | } |
||
249 | |||
250 | // is it an en passant coup ? |
||
251 | if (move->is_enpassant) |
||
252 | wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"e.p."); |
||
253 | } |
||
254 | |||
255 | return (true); // finished |
||
256 | } |
||
257 | |||
258 | |||
259 | static bool SAN_DisambiguateMove (boardmove_t *move, boardmove_t *new_move) |
||
260 | { |
||
261 | // handy helper to disambiguate a move literal of which we don't know the source, |
||
262 | // but for which we know the part type and the target slot |
||
263 | |||
264 | int index_line; |
||
265 | int index_column; |
||
266 | |||
267 | // are both source line and column known ? |
||
268 | if ((new_move->source[0] != -1) && (new_move->source[1] != -1)) |
||
269 | return (true); // no need to disambiguate anything |
||
270 | |||
271 | // else is source line known ? |
||
272 | else if (new_move->source[0] != -1) |
||
273 | { |
||
274 | // cycle through all the columns and find the part of the same type that has the right to move there |
||
275 | for (index_column = 0; index_column < 8; index_column++) |
||
276 | if ((move->slots[new_move->source[0]][index_column].color == new_move->color) |
||
277 | && (move->slots[new_move->source[0]][index_column].part == new_move->part) |
||
278 | && Move_IsMoveValid (move, new_move->source[0], index_column, new_move->target[0], new_move->target[1])) |
||
279 | { |
||
280 | new_move->source[1] = index_column; // save column |
||
281 | return (true); // we've found it, stop searching |
||
282 | } |
||
283 | } |
||
284 | |||
285 | // else is source column known ? |
||
286 | else if (new_move->source[1] != -1) |
||
287 | { |
||
288 | // cycle through all the lines and find the part of the same type that has the right to move there |
||
289 | for (index_line = 0; index_line < 8; index_line++) |
||
290 | if ((move->slots[index_line][new_move->source[1]].color == new_move->color) |
||
291 | && (move->slots[index_line][new_move->source[1]].part == new_move->part) |
||
292 | && Move_IsMoveValid (move, index_line, new_move->source[1], new_move->target[0], new_move->target[1])) |
||
293 | { |
||
294 | new_move->source[0] = index_line; // save line |
||
295 | return (true); // we've found it, stop searching |
||
296 | } |
||
297 | } |
||
298 | |||
299 | // else neither source line nor column is known |
||
300 | else |
||
301 | { |
||
302 | // cycle through all the board and find the part of the same type that has the right to move there |
||
303 | for (index_line = 0; index_line < 8; index_line++) |
||
304 | for (index_column = 0; index_column < 8; index_column++) |
||
305 | if ((move->slots[index_line][index_column].color == new_move->color) |
||
306 | && (move->slots[index_line][index_column].part == new_move->part) |
||
307 | && Move_IsMoveValid (move, index_line, index_column, new_move->target[0], new_move->target[1])) |
||
308 | { |
||
309 | new_move->source[0] = index_line; // save line and column |
||
310 | new_move->source[1] = index_column; |
||
311 | return (true); // we've found it, stop searching |
||
312 | } |
||
313 | } |
||
314 | |||
315 | return (false); // no possibility found |
||
316 | } |