src/Engine/Models/ChessGame.cs
/// <summary>
/// Get the game state in FEN notation. https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation.
/// </summary>
/// <returns>FEN notation description of the game state.</returns>
Get the game state in FEN notation.
Get the game state in FEN notation .
public string GetFEN()
        {
            var fen = new StringBuilder();
            var emptySquares = 0;
            for (var rank = BoardSize - 1; rank >= 0; rank--)
            {
                for (var file = 0; file < BoardSize; file++)
                {
                    var squareState = boardState[file][rank];
                    if (squareState is null)
                    {
                        emptySquares++;
                    }
                    else
                    {
                        if (emptySquares > 0)
                        {
                            fen.Append(emptySquares);
                            emptySquares = 0;
                        }
                        fen.Append(ChessFormatter.PieceToString(squareState.PieceType, true, true));
                    }
                }
                if (emptySquares > 0)
                {
                    fen.Append(emptySquares);
                    emptySquares = 0;
                }
                if (rank > 0)
                {
                    fen.Append(FenRankDelimiter);
                }
            }
            fen.Append($" {(WhiteToMove ? "w" : "b")} ");
            if (BlackCastlingOptions == CastlingOptions.None && WhiteCastlingOptions == CastlingOptions.None)
            {
                fen.Append("- ");
            }
            else
            {
                if ((WhiteCastlingOptions & CastlingOptions.KingSide) == CastlingOptions.KingSide)
                {
                    fen.Append("K");
                }
                if ((WhiteCastlingOptions & CastlingOptions.QueenSide) == CastlingOptions.QueenSide)
                {
                    fen.Append("Q");
                }
                if ((BlackCastlingOptions & CastlingOptions.KingSide) == CastlingOptions.KingSide)
                {
                    fen.Append("k");
                }
                if ((BlackCastlingOptions & CastlingOptions.QueenSide) == CastlingOptions.QueenSide)
                {
                    fen.Append("q");
                }
                fen.Append(' ');
            }
            fen.Append($"{EnPassantTarget?.ToString() ?? "-"} {HalfMoveClock} {MoveCount}");
            return fen.ToString();
        }
public string GetFEN ( ) { var fen = new StringBuilder ( ) ; var emptySquares = 0 ; for ( var rank = BoardSize - 1 ; rank >= 0 ; rank -- ) { for ( var file = 0 ; file < BoardSize ; file ++ ) { var squareState = boardState [ file ] [ rank ] ; if ( squareState is null ) { emptySquares ++ ; } else { if ( emptySquares > 0 ) { fen . Append ( emptySquares ) ; emptySquares = 0 ; } fen . Append ( ChessFormatter . PieceToString ( squareState . PieceType , true , true ) ) ; } } if ( emptySquares > 0 ) { fen . Append ( emptySquares ) ; emptySquares = 0 ; } if ( rank > 0 ) { fen . Append ( FenRankDelimiter ) ; } } fen . Append ( $" { ( WhiteToMove ? " " : " " ) } " ) ; if ( BlackCastlingOptions == CastlingOptions . None && WhiteCastlingOptions == CastlingOptions . None ) { fen . Append ( " " ) ; } else { if ( ( WhiteCastlingOptions & CastlingOptions . KingSide ) == CastlingOptions . KingSide ) { fen . Append ( " " ) ; } if ( ( WhiteCastlingOptions & CastlingOptions . QueenSide ) == CastlingOptions . QueenSide ) { fen . Append ( " " ) ; } if ( ( BlackCastlingOptions & CastlingOptions . KingSide ) == CastlingOptions . KingSide ) { fen . Append ( " " ) ; } if ( ( BlackCastlingOptions & CastlingOptions . QueenSide ) == CastlingOptions . QueenSide ) { fen . Append ( " " ) ; } fen . Append ( ' ' ) ; } fen . Append ( $" { EnPassantTarget ? . ToString ( ) ?? " " } { HalfMoveClock } { MoveCount } " ) ; return fen . ToString ( ) ; }
Get the game state in FEN notation.
Get the game state in FEN notation .
// Write moves // Write active color // Write castling options // Write en passant state, halfmove clock, and move count
FEN notation description of the game state.
FEN notation description of the game state .