- // Look for a matching move:
- Move m, move = MOVE_NONE;
- int matches = 0;
-
- while((m = mp.get_next_move()) != MOVE_NONE) {
- bool match = true;
- if(pos.type_of_piece_on(move_from(m)) != pt)
- match = false;
- else if(move_to(m) != to)
- match = false;
- else if(move_promotion(m) != promotion)
- match = false;
- else if(fromFile != FILE_NONE && fromFile != square_file(move_from(m)))
- match = false;
- else if(fromRank != RANK_NONE && fromRank != square_rank(move_from(m)))
- match = false;
- if(match) {
- move = m;
- matches++;
- }
+ // Normal moves. We use a simple FSM to parse the san string
+ for (size_t i = 0; i < movestr.length(); i++)
+ {
+ char type, c = movestr[i];
+
+ if (pieceLetters.find(c) != string::npos)
+ type = 'P';
+ else if (c >= 'a' && c <= 'h')
+ type = 'F';
+ else if (c >= '1' && c <= '8')
+ type = 'R';
+ else
+ type = c;
+
+ switch (type) {
+ case 'P':
+ if (state == START)
+ {
+ pt = piece_type_from_char(c);
+ state = TO_FILE;
+ }
+ else if (state == PROMOTION)
+ {
+ promotion = piece_type_from_char(c);
+ state = (i < movestr.length() - 1 ? CHECK : END);
+ }
+ else
+ return MOVE_NONE;
+ break;
+ case 'F':
+ if (state == START)
+ {
+ pt = PAWN;
+ fromFile = toFile = file_from_char(c);
+ state = TO_RANK;
+ }
+ else if (state == TO_FILE)
+ {
+ toFile = file_from_char(c);
+ state = TO_RANK;
+ }
+ else if (state == TO_RANK && toFile != FILE_NONE)
+ {
+ // Previous file was for disambiguation
+ fromFile = toFile;
+ toFile = file_from_char(c);
+ }
+ else
+ return MOVE_NONE;
+ break;
+ case 'R':
+ if (state == TO_RANK)
+ {
+ toRank = rank_from_char(c);
+ state = (i < movestr.length() - 1) ? PROMOTION_OR_CHECK : END;
+ }
+ else if (state == TO_FILE && fromRank == RANK_NONE)
+ {
+ // It's a disambiguation rank instead of a file
+ fromRank = rank_from_char(c);
+ }
+ else
+ return MOVE_NONE;
+ break;
+ case 'x':
+ case 'X':
+ if (state == TO_RANK)
+ {
+ // Previous file was for disambiguation, or it's a pawn capture
+ fromFile = toFile;
+ state = TO_FILE;
+ }
+ else if (state != TO_FILE)
+ return MOVE_NONE;
+ break;
+ case '=':
+ if (state == PROMOTION_OR_CHECK)
+ state = PROMOTION;
+ else
+ return MOVE_NONE;
+ break;
+ case '+':
+ case '#':
+ if (state == PROMOTION_OR_CHECK || state == CHECK)
+ state = END;
+ else
+ return MOVE_NONE;
+ break;
+ default:
+ return MOVE_NONE;
+ break;
+ }