From 735cac5d53f502df9353887988d985eff62a9b34 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Fri, 10 Jun 2011 15:50:39 +0200 Subject: [PATCH] Retire PieceLetters struct Use a much simpler std::string instead. No functional change. Signed-off-by: Marco Costalba --- src/position.cpp | 63 +++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index 4613ec4d..ef90913c 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include @@ -77,33 +76,8 @@ namespace { // Bonus for having the side to move (modified by Joona Kiiski) const Score TempoValue = make_score(48, 22); - struct PieceLetters : public 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; - operator[](' ') = PIECE_NONE; - operator[]('.') = PIECE_NONE_DARK_SQ; - } - - char from_piece(Piece p) const { - - std::map::const_iterator it; - for (it = begin(); it != end(); ++it) - if (it->second == p) - return it->first; - - assert(false); - return 0; - } - }; - - PieceLetters pieceLetters; + // To convert a Piece to and from a FEN char + const string PieceToChar(".PNBRQK pnbrqk "); } @@ -192,17 +166,19 @@ void Position::from_fen(const string& fen, bool isChess960) { char token; int hmc, fmn; - std::istringstream ss(fen); + size_t p; Square sq = SQ_A8; + std::istringstream ss(fen); clear(); + ss >> std::noskipws; // 1. Piece placement field - while (ss.get(token) && token != ' ') + while ((ss >> token) && !isspace(token)) { - if (pieceLetters.find(token) != pieceLetters.end()) + if ((p = PieceToChar.find(token)) != string::npos) { - put_piece(pieceLetters[token], sq); + put_piece(Piece(p), sq); sq++; } else if (isdigit(token)) @@ -214,23 +190,23 @@ void Position::from_fen(const string& fen, bool isChess960) { } // 2. Active color - if (!ss.get(token) || (token != 'w' && token != 'b')) + if (!(ss >> token) || (token != 'w' && token != 'b')) goto incorrect_fen; sideToMove = (token == 'w' ? WHITE : BLACK); - if (!ss.get(token) || token != ' ') + if (!(ss >> token) || !isspace(token)) goto incorrect_fen; // 3. Castling availability - while (ss.get(token) && token != ' ') + while ((ss >> token) && !isspace(token)) if (!set_castling_rights(token)) goto incorrect_fen; // 4. En passant square char col, row; - if ( (ss.get(col) && (col >= 'a' && col <= 'h')) - && (ss.get(row) && (row == '3' || row == '6'))) + if ( ((ss >> col) && (col >= 'a' && col <= 'h')) + && ((ss >> row) && (row == '3' || row == '6'))) { st->epSquare = make_square(file_from_char(col), rank_from_char(row)); @@ -241,7 +217,7 @@ void Position::from_fen(const string& fen, bool isChess960) { } // 5. Halfmove clock - if (ss >> hmc) + if (ss >> std::skipws >> hmc) st->rule50 = hmc; // 6. Fullmove number @@ -337,10 +313,12 @@ const string Position::to_fen() const { string fen; Square sq; - char emptyCnt = '0'; + char emptyCnt; for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/') { + emptyCnt = '0'; + for (File file = FILE_A; file <= FILE_H; file++) { sq = make_square(file, rank); @@ -352,16 +330,13 @@ const string Position::to_fen() const { fen += emptyCnt; emptyCnt = '0'; } - fen += pieceLetters.from_piece(piece_on(sq)); + fen += PieceToChar[piece_on(sq)]; } else emptyCnt++; } if (emptyCnt != '0') - { fen += emptyCnt; - emptyCnt = '0'; - } } fen += (sideToMove == WHITE ? " w " : " b "); @@ -413,7 +388,7 @@ void Position::print(Move move) const { piece = PIECE_NONE_DARK_SQ; char c = (color_of_piece_on(sq) == BLACK ? '=' : ' '); - cout << c << pieceLetters.from_piece(piece) << c << '|'; + cout << c << PieceToChar[piece] << c << '|'; } } cout << dottedLine << "Fen is: " << to_fen() << "\nKey is: " << st->key << endl; -- 2.39.2