src/Engine/Models/ChessGame.cs
/// <summary>
/// Initialize the board to a position given in FEN format. https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation.
/// </summary>
/// <param name="fen">The FEN-formatted game state to load.</param>
Initialize the board to a position given in FEN format.
Initialize the board to a position given in FEN format .
public void LoadFEN(string fen)
        {
            ClearGameState();
            StartingFEN = fen;
            var fenComponents = fen?.Split(new char[] { ' ' }, 6, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
            if (fenComponents.Length > 0)
            {
                var rank = 7;
                var file = 0;
                foreach (var piece in fenComponents[0])
                {
                    switch (piece)
                    {
                        case FenRankDelimiter:
                            rank--;
                            file = 0;
                            break;
                        case 'K':
                        case 'k':
                        case 'Q':
                        case 'q':
                        case 'R':
                        case 'r':
                        case 'B':
                        case 'b':
                        case 'N':
                        case 'n':
                        case 'P':
                        case 'p':
                            boardState[file][rank] = new ChessPiece(
                                ChessFormatter.PieceFromString($"{piece}"),
                                new BoardPosition(file, rank));
                            file++;
                            break;
                        default:
                            if (int.TryParse($"{piece}", out var spaceCount))
                            {
                                file += spaceCount;
                            }
                            break;
                    }
                }
            }
            if (fenComponents.Length > 1)
            {
                WhiteToMove = fenComponents[1].Equals("w", StringComparison.InvariantCultureIgnoreCase);
            }
            if (fenComponents.Length > 2)
            {
                WhiteCastlingOptions = CastlingOptions.None;
                BlackCastlingOptions = CastlingOptions.None;
                var castlingOptions = fenComponents[2];
                if (castlingOptions.Contains("K"))
                {
                    WhiteCastlingOptions |= CastlingOptions.KingSide;
                }
                if (castlingOptions.Contains("Q"))
                {
                    WhiteCastlingOptions |= CastlingOptions.QueenSide;
                }
                if (castlingOptions.Contains("k"))
                {
                    BlackCastlingOptions |= CastlingOptions.KingSide;
                }
                if (castlingOptions.Contains("q"))
                {
                    BlackCastlingOptions |= CastlingOptions.QueenSide;
                }
            }
            if (fenComponents.Length > 3 && fenComponents[3].Length == 2)
            {
                EnPassantTarget = new BoardPosition(
                    ChessFormatter.FileFromString($"{fenComponents[3][0]}"),
                    ChessFormatter.RankFromString($"{fenComponents[3][1]}"));
            }
            if (fenComponents.Length > 4)
            {
                HalfMoveClock = int.Parse(fenComponents[4], CultureInfo.InvariantCulture);
            }
            if (fenComponents.Length > 5)
            {
                MoveCount = int.Parse(fenComponents[5], CultureInfo.InvariantCulture);
            }
        }
public void LoadFEN ( string fen ) { ClearGameState ( ) ; StartingFEN = fen ; var fenComponents = fen ? . Split ( new char [ ] { ' ' } , 6 , StringSplitOptions . RemoveEmptyEntries ) ?? new string [ 0 ] ; if ( fenComponents . Length > 0 ) { var rank = 7 ; var file = 0 ; foreach ( var piece in fenComponents [ 0 ] ) { switch ( piece ) { case FenRankDelimiter : rank -- ; file = 0 ; break ; case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : boardState [ file ] [ rank ] = new ChessPiece ( ChessFormatter . PieceFromString ( $" { piece } " ) , new BoardPosition ( file , rank ) ) ; file ++ ; break ; default : if ( int . TryParse ( $" { piece } " , out var spaceCount ) ) { file += spaceCount ; } break ; } } } if ( fenComponents . Length > 1 ) { WhiteToMove = fenComponents [ 1 ] . Equals ( " " , StringComparison . InvariantCultureIgnoreCase ) ; } if ( fenComponents . Length > 2 ) { WhiteCastlingOptions = CastlingOptions . None ; BlackCastlingOptions = CastlingOptions . None ; var castlingOptions = fenComponents [ 2 ] ; if ( castlingOptions . Contains ( " " ) ) { WhiteCastlingOptions |= CastlingOptions . KingSide ; } if ( castlingOptions . Contains ( " " ) ) { WhiteCastlingOptions |= CastlingOptions . QueenSide ; } if ( castlingOptions . Contains ( " " ) ) { BlackCastlingOptions |= CastlingOptions . KingSide ; } if ( castlingOptions . Contains ( " " ) ) { BlackCastlingOptions |= CastlingOptions . QueenSide ; } } if ( fenComponents . Length > 3 && fenComponents [ 3 ] . Length == 2 ) { EnPassantTarget = new BoardPosition ( ChessFormatter . FileFromString ( $" { fenComponents [ 3 ] [ 0 ] } " ) , ChessFormatter . RankFromString ( $" { fenComponents [ 3 ] [ 1 ] } " ) ) ; } if ( fenComponents . Length > 4 ) { HalfMoveClock = int . Parse ( fenComponents [ 4 ] , CultureInfo . InvariantCulture ) ; } if ( fenComponents . Length > 5 ) { MoveCount = int . Parse ( fenComponents [ 5 ] , CultureInfo . InvariantCulture ) ; } }
Initialize the board to a position given in FEN format.
Initialize the board to a position given in FEN format .
// Parse piece positions // Parse active color // Parse castling options // Parse en passant target // Parse half move clock // Parse move count // TODO : Look for check // TODO : Set Result
fen
string
The FEN-formatted game state to load.
The FEN - formatted game state to load .