From c081a81daf128048bb4cf4cbdb4d5fc48110bb78 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 3 Jul 2011 11:32:34 +0100 Subject: [PATCH 1/1] Teach to_fen() about Halfmove and Fullmove number And also fix the last '/' at the end of the piece placement field. No functional change. Signed-off-by: Marco Costalba --- src/benchmark.cpp | 4 +-- src/position.cpp | 66 ++++++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/benchmark.cpp b/src/benchmark.cpp index eef27c6a..4b3e417e 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -29,8 +29,8 @@ using namespace std; static const string Defaults[] = { "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", - "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -", - "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -", + "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10", + "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11", "4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19", "rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14", "r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14", diff --git a/src/position.cpp b/src/position.cpp index 7f0e1447..281109b1 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -129,7 +129,7 @@ void Position::detach() { /// string. This function is not very robust - make sure that input FENs are /// correct (this is assumed to be the responsibility of the GUI). -void Position::from_fen(const string& fen, bool isChess960) { +void Position::from_fen(const string& fenStr, bool isChess960) { /* A FEN string defines a particular position using only the ASCII character set. @@ -161,13 +161,13 @@ void Position::from_fen(const string& fen, bool isChess960) { char col, row, token; size_t p; Square sq = SQ_A8; - std::istringstream ss(fen); + std::istringstream fen(fenStr); clear(); - ss >> std::noskipws; + fen >> std::noskipws; // 1. Piece placement - while ((ss >> token) && !isspace(token)) + while ((fen >> token) && !isspace(token)) { if (token == '/') sq -= Square(16); // Jump back of 2 rows @@ -183,17 +183,17 @@ void Position::from_fen(const string& fen, bool isChess960) { } // 2. Active color - ss >> token; + fen >> token; sideToMove = (token == 'w' ? WHITE : BLACK); - ss >> token; + fen >> token; // 3. Castling availability - while ((ss >> token) && !isspace(token)) + while ((fen >> token) && !isspace(token)) set_castling_rights(token); // 4. En passant square. Ignore if no pawn capture is possible - if ( ((ss >> col) && (col >= 'a' && col <= 'h')) - && ((ss >> row) && (row == '3' || row == '6'))) + if ( ((fen >> col) && (col >= 'a' && col <= 'h')) + && ((fen >> row) && (row == '3' || row == '6'))) { st->epSquare = make_square(File(col - 'a'), Rank(row - '1')); Color them = opposite_color(sideToMove); @@ -203,7 +203,7 @@ void Position::from_fen(const string& fen, bool isChess960) { } // 5-6. Halfmove clock and fullmove number - ss >> std::skipws >> st->rule50 >> fullMoves; + fen >> std::skipws >> st->rule50 >> fullMoves; // Various initialisations chess960 = isChess960; @@ -270,13 +270,13 @@ void Position::set_castling_rights(char token) { const string Position::to_fen() const { - string fen; + std::ostringstream fen; Square sq; - char emptyCnt; + int emptyCnt; - for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/') + for (Rank rank = RANK_8; rank >= RANK_1; rank--) { - emptyCnt = '0'; + emptyCnt = 0; for (File file = FILE_A; file <= FILE_H; file++) { @@ -284,40 +284,46 @@ const string Position::to_fen() const { if (!square_is_empty(sq)) { - if (emptyCnt != '0') + if (emptyCnt) { - fen += emptyCnt; - emptyCnt = '0'; + fen << emptyCnt; + emptyCnt = 0; } - fen += PieceToChar[piece_on(sq)]; - } else + fen << PieceToChar[piece_on(sq)]; + } + else emptyCnt++; } - if (emptyCnt != '0') - fen += emptyCnt; + if (emptyCnt) + fen << emptyCnt; + + if (rank > RANK_1) + fen << '/'; } - fen += (sideToMove == WHITE ? " w " : " b "); + fen << (sideToMove == WHITE ? " w " : " b "); if (st->castleRights != CASTLES_NONE) { if (can_castle(WHITE_OO)) - fen += chess960 ? char(toupper(file_to_char(square_file(castle_rook_square(WHITE_OO))))) : 'K'; + fen << (chess960 ? char(toupper(file_to_char(square_file(castle_rook_square(WHITE_OO))))) : 'K'); if (can_castle(WHITE_OOO)) - fen += chess960 ? char(toupper(file_to_char(square_file(castle_rook_square(WHITE_OOO))))) : 'Q'; + fen << (chess960 ? char(toupper(file_to_char(square_file(castle_rook_square(WHITE_OOO))))) : 'Q'); if (can_castle(BLACK_OO)) - fen += chess960 ? file_to_char(square_file(castle_rook_square(BLACK_OO))) : 'k'; + fen << (chess960 ? file_to_char(square_file(castle_rook_square(BLACK_OO))) : 'k'); if (can_castle(BLACK_OOO)) - fen += chess960 ? file_to_char(square_file(castle_rook_square(BLACK_OOO))) : 'q'; + fen << (chess960 ? file_to_char(square_file(castle_rook_square(BLACK_OOO))) : 'q'); } else - fen += '-'; + fen << '-'; + + fen << (ep_square() == SQ_NONE ? " -" : " " + square_to_string(ep_square())) + << " " << st->rule50 << " " << fullMoves; - fen += (ep_square() == SQ_NONE ? " -" : " " + square_to_string(ep_square())); - return fen; + return fen.str(); } @@ -331,7 +337,7 @@ void Position::print(Move move) const { if (move) { Position p(*this, thread()); - string dd = (piece_color(piece_on(move_from(move))) == BLACK ? ".." : ""); + string dd = (sideToMove == BLACK ? ".." : ""); cout << "\nMove is: " << dd << move_to_san(p, move); } -- 2.39.2