-/// move_from_san() takes a position and a string as input, and tries to
-/// interpret the string as a move in short algebraic notation. On success,
-/// the move is returned. On failure (i.e. if the string is unparsable, or
-/// if the move is illegal or ambiguous), MOVE_NONE is returned.
-
-Move move_from_san(const Position& pos, const std::string& movestr) {
-
- assert(pos.is_ok());
-
- MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
-
- // Castling moves
- if (movestr == "O-O-O")
- {
- Move m;
- while ((m = mp.get_next_move()) != MOVE_NONE)
- if (move_is_long_castle(m) && pos.pl_move_is_legal(m))
- return m;
-
- return MOVE_NONE;
- }
- else if (movestr == "O-O")
- {
- Move m;
- while ((m = mp.get_next_move()) != MOVE_NONE)
- if(move_is_short_castle(m) && pos.pl_move_is_legal(m))
- return m;
-
- return MOVE_NONE;
- }
-
- // Normal moves
- const char *cstr = movestr.c_str();
- const char *c;
- char *cc;
- char str[10];
- int i;
-
- // 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;
- File fromFile = FILE_NONE;
- Rank fromRank = RANK_NONE;
-
- // Promotion?
- if(strchr("BNRQ", str[right])) {
- promotion = piece_type_from_char(str[right]);
- right--;
- }
- else
- promotion = NO_PIECE_TYPE;
-
- // Find the moving piece:
- if(left < right) {
- if(strchr("BNRQK", str[left])) {
- pt = piece_type_from_char(str[left]);
- left++;
- }
- else
- pt = PAWN;
- }
-
- // Find the to square:
- if(left < right) {
- if(str[right] < '1' || str[right] > '8' ||
- str[right-1] < 'a' || str[right-1] > 'h')
- 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:
- if(left <= right) {
- if(strchr("abcdefgh", str[left])) {
- 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;
-
- 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++;
- }
- }
-
- if(matches == 1)
- return move;
- else
- return MOVE_NONE;
-}
-
-