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