1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 16:53:09 +00:00

san.cpp: rewrite broken move_from_san

Use a state machine to parse the input. Fixed
the many broken cases.

Tested on more then 15 milions nodes.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2008-12-01 17:46:12 +01:00
parent 20390fcb3c
commit 9f943a132a

View file

@ -141,7 +141,7 @@ Move move_from_san(const Position& pos, const std::string& movestr) {
MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly); MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
// Castling moves // Castling moves
if (movestr == "O-O-O") if (movestr == "O-O-O" || movestr == "O-O-O+")
{ {
Move m; Move m;
while ((m = mp.get_next_move()) != MOVE_NONE) while ((m = mp.get_next_move()) != MOVE_NONE)
@ -150,7 +150,7 @@ Move move_from_san(const Position& pos, const std::string& movestr) {
return MOVE_NONE; return MOVE_NONE;
} }
else if (movestr == "O-O") else if (movestr == "O-O" || movestr == "O-O+")
{ {
Move m; Move m;
while ((m = mp.get_next_move()) != MOVE_NONE) while ((m = mp.get_next_move()) != MOVE_NONE)
@ -160,94 +160,124 @@ Move move_from_san(const Position& pos, const std::string& movestr) {
return MOVE_NONE; return MOVE_NONE;
} }
// Normal moves // Normal moves. We use a simple FSM to parse the san string.
const char *cstr = movestr.c_str(); enum { START, TO_FILE, TO_RANK, PROMOTION_OR_CHECK, PROMOTION, CHECK, END };
const char *c; static const std::string pieceLetters = "KQRBN";
char *cc; PieceType pt = NO_PIECE_TYPE, promotion = NO_PIECE_TYPE;
char str[10]; File fromFile = FILE_NONE, toFile = FILE_NONE;
int i; Rank fromRank = RANK_NONE, toRank = RANK_NONE;
// Initialize str[] by making a copy of movestr with the characters
// 'x', '=', '+' and '#' removed.
cc = str;
for(i=0, c=cstr; i<10 && *c!='\0' && *c!='\n' && *c!=' '; i++, c++)
if(!strchr("x=+#", *c)) {
*cc = strchr("nrq", *c)? toupper(*c) : *c;
cc++;
}
*cc = '\0';
size_t left = 0, right = strlen(str) - 1;
PieceType pt = NO_PIECE_TYPE, promotion;
Square to; Square to;
File fromFile = FILE_NONE; int state = START;
Rank fromRank = RANK_NONE;
// Promotion? for (size_t i = 0; i < movestr.length(); i++)
if(strchr("BNRQ", str[right])) { {
promotion = piece_type_from_char(str[right]); char type, c = movestr[i];
right--; if (pieceLetters.find(c) != -1)
} type = 'P';
else else if (c >= 'a' && c <= 'h')
promotion = NO_PIECE_TYPE; type = 'F';
else if (c >= '1' && c <= '8')
type = 'R';
else
type = c;
// Find the moving piece: switch (type) {
if(left < right) { case 'P':
if(strchr("BNRQK", str[left])) { if (state == START)
pt = piece_type_from_char(str[left]); {
left++; pt = piece_type_from_char(c);
} state = TO_FILE;
else }
pt = PAWN; 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 == FILE_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;
}
} }
// Find the to square: if (state != END)
if(left < right) {
if(str[right] < '1' || str[right] > '8' ||
str[right-1] < 'a' || str[right-1] > 'h')
return MOVE_NONE; return MOVE_NONE;
to = make_square(file_from_char(str[right-1]), rank_from_char(str[right]));
right -= 2;
}
else
return MOVE_NONE;
// Find the file and/or rank of the from square: // Look for a matching move
if(left <= right) { Move m, move;
if(strchr("abcdefgh", str[left])) { to = make_square(toFile, toRank);
fromFile = file_from_char(str[left]);
left++;
}
if(strchr("12345678", str[left]))
fromRank = rank_from_char(str[left]);
}
// Look for a matching move:
Move m, move = MOVE_NONE;
int matches = 0; int matches = 0;
while((m = mp.get_next_move()) != MOVE_NONE) { while ((m = mp.get_next_move()) != MOVE_NONE)
bool match = true; if ( pos.type_of_piece_on(move_from(m)) == pt
if(pos.type_of_piece_on(move_from(m)) != pt) && move_to(m) == to
match = false; && move_promotion(m) == promotion
else if(move_to(m) != to) && (fromFile == FILE_NONE || fromFile == square_file(move_from(m)))
match = false; && (fromRank == RANK_NONE || fromRank == square_rank(move_from(m))))
else if(move_promotion(m) != promotion) {
match = false; move = m;
else if(fromFile != FILE_NONE && fromFile != square_file(move_from(m))) matches++;
match = false; }
else if(fromRank != RANK_NONE && fromRank != square_rank(move_from(m))) return (matches == 1 ? move : MOVE_NONE);
match = false;
if(match) {
move = m;
matches++;
}
}
if(matches == 1)
return move;
else
return MOVE_NONE;
} }