16 #include "move_legal.h"
24 static const bool UseSlowDebug = FALSE;
35 static bool san_to_lan (const char san[], const board_t * board, char string[], int size);
36 static int move_from_lan (const char string[], const board_t * board);
38 static int ambiguity (int move, const board_t * board);
42 bool move_to_san(int move, const board_t * board, char string[], int size) {
47 ASSERT(move_is_ok(move));
48 ASSERT(board_is_ok(board));
52 ASSERT(move_is_legal(move,board));
54 if (size < 8) return FALSE;
58 from = move_from(move);
65 if (move_is_castle(move,board)) {
70 strcat(string,"O-O-O");
78 piece = board->square[from];
80 if (piece_is_pawn(piece)) {
84 if (move_is_capture(move,board)) {
85 sprintf(tmp_string,"%c",file_to_char(square_file(from)));
86 strcat(string,tmp_string);
93 sprintf(tmp_string,"%c",toupper(piece_to_char(piece)));
94 strcat(string,tmp_string);
98 switch (ambiguity(move,board)) {
102 sprintf(tmp_string,"%c",file_to_char(square_file(from)));
103 strcat(string,tmp_string);
106 sprintf(tmp_string,"%c",rank_to_char(square_rank(from)));
107 strcat(string,tmp_string);
109 case AMBIGUITY_SQUARE:
110 if (!square_to_string(from,tmp_string,256)) return FALSE;
111 strcat(string,tmp_string);
121 if (move_is_capture(move,board)) strcat(string,"x");
125 if (!square_to_string(to,tmp_string,256)) return FALSE;
126 strcat(string,tmp_string);
130 if (move_is_promote(move)) {
131 sprintf(tmp_string,"=%c",toupper(piece_to_char(move_promote(move,board))));
132 strcat(string,tmp_string);
139 if (move_is_mate(move,board)) {
141 } else if (move_is_check(move,board)) {
150 int move_from_san(const char string[], const board_t * board) {
155 ASSERT(string!=NULL);
156 ASSERT(board_is_ok(board));
158 san_to_lan(string,board,s,256);
159 move = move_from_lan(s,board);
161 ASSERT(!UseSlowDebug||move==move_from_san_debug(string,board));
166 // move_from_san_debug()
168 int move_from_san_debug(const char string[], const board_t * board) {
172 char move_string[256];
174 ASSERT(string!=NULL);
175 ASSERT(board_is_ok(board));
177 gen_legal_moves(list,board);
179 for (i = 0; i < list_size(list); i++) {
180 move = list_move(list,i);
181 if (!move_to_san(move,board,move_string,256)) ASSERT(FALSE);
182 if (my_string_equal(move_string,string)) return move;
190 static bool san_to_lan(const char san[], const board_t * board, char string[], int size) {
196 char king_string[3], rook_string[3];
199 ASSERT(board_is_ok(board));
200 ASSERT(string!=NULL);
205 if (size < 8) return FALSE;
206 strcpy(string,"???????");
213 // skip trailing '+' or '#'
217 if (c == '+' || c == '#') right--;
226 } else if (right == 3 && strncmp(san,"O-O",3) == 0) {
228 if (board->castle[board->turn][SideH] == SquareNone) return FALSE;
230 king = king_pos(board,board->turn);
231 rook = board->castle[board->turn][SideH];
233 square_to_string(king,king_string,3);
234 square_to_string(rook,rook_string,3);
236 sprintf(string,"K%s?%s?",king_string,rook_string);
238 } else if (right == 5 && strncmp(san,"O-O-O",5) == 0) {
240 if (board->castle[board->turn][SideA] == SquareNone) return FALSE;
242 king = king_pos(board,board->turn);
243 rook = board->castle[board->turn][SideA];
245 square_to_string(king,king_string,3);
246 square_to_string(rook,rook_string,3);
248 sprintf(string,"K%s?%s?",king_string,rook_string);
258 if (char_is_piece(c)) {
268 c = toupper(san[right-1]);
270 if (char_is_piece(c)) {
277 if (left < right && san[right-1] == '=') right--;
287 if (char_is_rank(c)) {
299 if (char_is_file(c)) {
311 if (char_is_piece(c)) {
317 // skip middle '-' or 'x'
321 if (c == '-' || c == 'x') right--;
330 if (char_is_file(c)) {
342 if (char_is_rank(c)) {
348 if (left != right) return FALSE;
358 static int move_from_lan(const char string[], const board_t * board) {
373 ASSERT(string!=NULL);
374 ASSERT(board_is_ok(board));
378 len = strlen(string);
379 if (len != 7) return MoveNone;
382 colour = board->turn;
389 case '?': // not a promotion
392 promote = MovePromoteKnight;
395 promote = MovePromoteBishop;
398 promote = MovePromoteRook;
401 promote = MovePromoteQueen;
414 to = square_from_string(s);
415 if (to == SquareNone) return MoveNone;
417 // known from square?
419 if (string[1] != '?' && string[2] != '?') {
427 from = square_from_string(s);
428 if (from == SquareNone) return MoveNone;
430 // convert "king slide" castling to KxR
432 if (piece_is_king(board->square[from])
433 && square_rank(to) == square_rank(from)
434 && abs(to-from) > 1) {
435 side = (to > from) ? SideH : SideA;
436 to = board->castle[colour][side];
437 if (to == SquareNone) return MoveNone;
442 move = move_make(from,to) | promote;
449 if (string[0] == '?' && string[1] == '?') {
451 if (board->square[to] != Empty) return MoveNone; // useful?
453 inc = (colour_is_white(colour)) ? +16 : -16;
456 if (board->square[from] == Empty && square_side_rank(to,colour) == Rank4) {
460 if (board->square[from] != piece_make_pawn(colour)) { // useful?
466 move = move_make(from,to) | promote;
473 piece_char = string[0];
475 if (piece_char == '?' && string[1] != '?') {
483 for (ptr = board->list[colour]; (from=*ptr) != SquareNone; ptr++) {
485 piece = board->square[from];
487 if (toupper(piece_to_char(piece)) == piece_char) {
488 if (piece_attack(board,piece,from,to)) {
490 && (string[1] == '?' || file_to_char(square_file(from)) == string[1])
491 && (string[2] == '?' || rank_to_char(square_rank(from)) == string[2])) {
492 if (!is_pinned(board,from,to,colour)) {
493 move = move_make(from,to) | promote;
501 if (n != 1) move = MoveNone;
508 static int ambiguity(int move, const board_t * board) {
516 from = move_from(move);
518 piece = move_piece(move,board);
520 gen_legal_moves(list,board);
526 for (i = 0; i < list_size(list); i++) {
527 m = list_move(list,i);
528 if (move_piece(m,board) == piece && move_to(m) == to) {
533 if (n == 1) return AMBIGUITY_NONE;
539 for (i = 0; i < list_size(list); i++) {
540 m = list_move(list,i);
541 if (move_piece(m,board) == piece && move_to(m) == to) {
542 if (square_file(move_from(m)) == square_file(from)) n++;
546 if (n == 1) return AMBIGUITY_FILE;
552 for (i = 0; i < list_size(list); i++) {
553 m = list_move(list,i);
554 if (move_piece(m,board) == piece && move_to(m) == to) {
555 if (square_rank(move_from(m)) == square_rank(from)) n++;
559 if (n == 1) return AMBIGUITY_RANK;
563 return AMBIGUITY_SQUARE;