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