From 3de8f457dad1d31fed77793f33cae16d71426f39 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 28 Apr 2025 20:20:34 +0200 Subject: [PATCH] printing fen on printboard | bench 1136338 --- source/board.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ source/board.h | 1 + 2 files changed, 80 insertions(+) diff --git a/source/board.cpp b/source/board.cpp index 9a427ca..2358139 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -86,6 +86,84 @@ void Board::SetByFen(std::string_view fen) { ResetAccPair(); } +std::string Board::GetFen() { + std::ostringstream fen; + + for (int rank = 7; rank >= 0; --rank) { + int emptyCount = 0; + + for (int file = 0; file < 8; ++file) { + int square = rank * 8 + file; + const uint64_t squareBit = 1ULL << square; + + if (!(occupied & squareBit)) { + ++emptyCount; + continue; + } + + if (emptyCount > 0) { + fen << emptyCount; + emptyCount = 0; + } + + static constexpr std::array pieceChars = {'p', 'n', 'b', 'r', 'q', 'k'}; + char pieceChar = '?'; + + for (size_t pieceType = 0; pieceType < pieceChars.size(); ++pieceType) { + if (pieces[pieceType] & squareBit) { + pieceChar = pieceChars[pieceType]; + break; + } + } + + fen << (colors[White] & squareBit ? static_cast(std::toupper(pieceChar)) : pieceChar); + } + + if (emptyCount > 0) { + fen << emptyCount; + } + + if (rank > 0) { + fen << '/'; + } + } + + fen << ' ' << (sideToMove ? 'b' : 'w'); + + fen << ' '; + bool hasCastlingRights = false; + + const std::array, 4> castlingOptions = { + {{whiteKingRight, 'K'}, {whiteQueenRight, 'Q'}, {blackKingRight, 'k'}, {blackQueenRight, 'q'}} + }; + + for (const auto& [right, symbol] : castlingOptions) { + if (castlingRights & right) { + fen << symbol; + hasCastlingRights = true; + } + } + + if (!hasCastlingRights) { + fen << '-'; + } + + fen << ' '; + if (enPassantTarget == noEPTarget) { + fen << '-'; + } else { + const int file = enPassantTarget % 8; + const int rank = enPassantTarget / 8; + fen << static_cast('a' + file) << (rank + 1); + } + + fen << ' ' << halfMoves; + + fen << ' ' << fullMoves; + + return fen.str(); +} + void Board::PrintBoard() { for (int rank = 7; rank >= 0; rank--) { @@ -136,6 +214,7 @@ void Board::PrintBoard() { std::cout << std::endl << std::endl; std::cout << " Hashkey: 0x" << std::hex << hashKey << std::dec << std::endl; + std::cout << " Fen: " << GetFen() << std::endl; } void Board::PrintNNUE() { diff --git a/source/board.h b/source/board.h index 702589a..137ddcd 100644 --- a/source/board.h +++ b/source/board.h @@ -43,6 +43,7 @@ public: void Reset(); void SetByFen(std::string_view fen); + std::string GetFen(); void PrintBoard(); void PrintNNUE();