From c2048136ec73a1d4437d286d34539a05910d83d7 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 24 Jul 2010 16:19:46 +0100 Subject: [PATCH] Last touches to from_fen() No functional change. Signed-off-by: Marco Costalba --- src/position.cpp | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index 1942abb0..2126741e 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -42,6 +43,18 @@ using std::string; using std::cout; using std::endl; +struct PieceLetters : std::map { + + PieceLetters() { + + operator[]('K') = WK; operator[]('k') = BK; + operator[]('Q') = WQ; operator[]('q') = BQ; + operator[]('R') = WR; operator[]('r') = BR; + operator[]('B') = WB; operator[]('b') = BB; + operator[]('N') = WN; operator[]('n') = BN; + operator[]('P') = WP; operator[]('p') = BP; + } +}; //// //// Variables @@ -142,15 +155,12 @@ void Position::from_fen(const string& fen) { 6) Fullmove number: The number of the full move. It starts at 1, and is incremented after Black's move. */ - static const string pieceLetters = "KQRBNPkqrbnp"; - static const Piece pieces[] = { WK, WQ, WR, WB, WN, WP, BK, BQ, BR, BB, BN, BP }; + static PieceLetters pieceLetters; + char token; + std::istringstream ss(fen); Rank rank = RANK_8; File file = FILE_A; - size_t idx; - - std::istringstream ss(fen); - char token; clear(); @@ -159,8 +169,7 @@ void Position::from_fen(const string& fen) { { if (isdigit(token)) { - // Skip the given number of files - file += token - '1' + 1; + file += token - '0'; // Skip the given number of files continue; } else if (token == '/') @@ -170,11 +179,10 @@ void Position::from_fen(const string& fen) { continue; } - idx = pieceLetters.find(token); - if (idx == string::npos) + if (pieceLetters.find(token) == pieceLetters.end()) goto incorrect_fen; - put_piece(pieces[idx], make_square(file, rank)); + put_piece(pieceLetters[token], make_square(file, rank)); file++; } @@ -205,15 +213,15 @@ void Position::from_fen(const string& fen) { Square fenEpSquare = make_square(file_from_char(col), rank_from_char(row)); Color them = opposite_color(sideToMove); - if (attacks_from(fenEpSquare, them) & this->pieces(PAWN, sideToMove)) + if (attacks_from(fenEpSquare, them) & pieces(PAWN, sideToMove)) st->epSquare = fenEpSquare; } // 5-6. Halfmove clock and fullmove number are not parsed // Various initialisations - castleRightsMask[make_square(initialKFile, RANK_1)] ^= (WHITE_OO|WHITE_OOO); - castleRightsMask[make_square(initialKFile, RANK_8)] ^= (BLACK_OO|BLACK_OOO); + castleRightsMask[make_square(initialKFile, RANK_1)] ^= WHITE_OO | WHITE_OOO; + castleRightsMask[make_square(initialKFile, RANK_8)] ^= BLACK_OO | BLACK_OOO; castleRightsMask[make_square(initialKRFile, RANK_1)] ^= WHITE_OO; castleRightsMask[make_square(initialKRFile, RANK_8)] ^= BLACK_OO; castleRightsMask[make_square(initialQRFile, RANK_1)] ^= WHITE_OOO; @@ -393,8 +401,8 @@ void Position::print(Move m) const { cout << '|' << endl; } cout << "+---+---+---+---+---+---+---+---+" << endl - << "Fen is: " << to_fen() << endl - << "Key is: " << st->key << endl; + << "Fen is: " << to_fen() << endl + << "Key is: " << st->key << endl; RequestPending = false; } -- 2.39.2