Support generation of SFEN (gbtami/pychess-variants#36)
authorFabian Fichter <ianfab@users.noreply.github.com>
Thu, 3 Oct 2019 14:58:20 +0000 (16:58 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Thu, 3 Oct 2019 14:58:20 +0000 (16:58 +0200)
Report SFEN next to FEN in the "d" command.

src/position.cpp
src/position.h

index ea2dfe6..94ca68e 100644 (file)
@@ -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())
   {
index db9756e..9852061 100644 (file)
@@ -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;