From e92c6ec8404628a4922d37e543d2f35b5f7b9bce Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Thu, 3 Oct 2019 16:58:20 +0200 Subject: [PATCH] Support generation of SFEN (gbtami/pychess-variants#36) Report SFEN next to FEN in the "d" command. --- src/position.cpp | 17 +++++++++++++++-- src/position.h | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index ea2dfe6..94ca68e 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -105,7 +105,7 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) { os << "+\n"; } - os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase + os << "\nFen: " << pos.fen() << "\nSfen: " << pos.fen(true) << "\nKey: " << std::hex << std::uppercase << std::setfill('0') << std::setw(16) << pos.key() << std::setfill(' ') << std::dec << "\nCheckers: "; @@ -583,7 +583,7 @@ Position& Position::set(const string& code, Color c, StateInfo* si) { /// Position::fen() returns a FEN representation of the position. In case of /// Chess960 the Shredder-FEN notation is used. This is mainly a debugging function. -const string Position::fen() const { +const string Position::fen(bool sfen) const { int emptyCnt; std::ostringstream ss; @@ -618,6 +618,19 @@ const string Position::fen() const { ss << '/'; } + // SFEN + if (sfen) + { + ss << (sideToMove == WHITE ? " b " : " w "); + for (Color c : {WHITE, BLACK}) + for (PieceType pt = KING; pt >= PAWN; --pt) + ss << std::string(pieceCountInHand[c][pt], piece_to_char()[make_piece(c, pt)]); + if (!count_in_hand(WHITE, ALL_PIECES) && !count_in_hand(BLACK, ALL_PIECES)) + ss << '-'; + ss << " " << gamePly + 1; + return ss.str(); + } + // pieces in hand if (piece_drops() || gating()) { diff --git a/src/position.h b/src/position.h index db9756e..9852061 100644 --- a/src/position.h +++ b/src/position.h @@ -89,7 +89,7 @@ public: // FEN string input/output Position& set(const Variant* v, const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th, bool sfen = false); Position& set(const std::string& code, Color c, StateInfo* si); - const std::string fen() const; + const std::string fen(bool sfen = false) const; // Variant rule properties const Variant* variant() const; -- 1.7.0.4