src/Engine/ChessEngine.cs
/// <summary>
/// Loads chess game from a PGN string.
/// </summary>
/// <param name="pgn">A PGN description of the game to load.</param>
Loads chess game from a PGN string.
Loads chess game from a PGN string .
public void LoadPGN(string pgn)
        {
            if (pgn is null)
            {
                throw new ArgumentNullException(nameof(pgn));
            }
            var match = PgnRegex.Match(pgn);
            if (!match.Success)
            {
                throw new ArgumentException("Invalid PGN string; could not be parsed.", nameof(pgn));
            }
            var tagKeys = match.Groups["key"].Captures.OfType<Capture>().Select(c => c.Value).ToArray();
            var tagValues = match.Groups["value"].Captures.OfType<Capture>().Select(c => c.Value).ToArray();
            var whiteMoves = match.Groups["whiteMove"].Captures.OfType<Capture>().Select(c => c.Value).ToArray();
            var blackMoves = match.Groups["blackMove"].Captures.OfType<Capture>().Select(c => c.Value).ToArray();
            var result = match.Groups["result"].Value;
            if (tagKeys.Length != tagValues.Length)
            {
                throw new ArgumentException("Invalid PGN string; invalid tag pairs.", nameof(pgn));
            }
            Game = new ChessGame();
            for (var i = 0; i < tagKeys.Length; i++)
            {
                var val = tagValues[i];
                switch (tagKeys[i].ToLowerInvariant())
                {
                    case "event":
                        Game.Event = val;
                        break;
                    case "site":
                        Game.Site = val;
                        break;
                    case "date":
                        Game.StartDate = DateTimeOffset.ParseExact(val, "yyyy.MM.dd", CultureInfo.InvariantCulture);
                        break;
                    case "round":
                        Game.Round = val;
                        break;
                    case "white":
                        Game.WhitePlayer = val;
                        break;
                    case "black":
                        Game.BlackPlayer = val;
                        break;
                    case "result":
                        Game.Result = ChessFormatter.ResultFromString(val);
                        break;
                    case "fen":
                        Game.LoadFEN(val);
                        break;
                    default:
                        break;
                }
            }
            if (Game.Result == GameResult.Ongoing)
            {
                Game.Result = ChessFormatter.ResultFromString(result);
            }
            for (var i = 0; i < whiteMoves.Length; i++)
            {
                var whiteMove = MoveFromAlgebraicNotation(whiteMoves[i]);
                Game.Move(whiteMove);
                if (i < blackMoves.Length)
                {
                    var blackMove = MoveFromAlgebraicNotation(blackMoves[i]);
                    Game.Move(blackMove);
                }
            }
        }
public void LoadPGN ( string pgn ) { if ( pgn is null ) { throw new ArgumentNullException ( nameof ( pgn ) ) ; } var match = PgnRegex . Match ( pgn ) ; if ( ! match . Success ) { throw new ArgumentException ( " " , nameof ( pgn ) ) ; } var tagKeys = match . Groups [ " " ] . Captures . OfType < Capture > ( ) . Select ( c => c . Value ) . ToArray ( ) ; var tagValues = match . Groups [ " " ] . Captures . OfType < Capture > ( ) . Select ( c => c . Value ) . ToArray ( ) ; var whiteMoves = match . Groups [ " " ] . Captures . OfType < Capture > ( ) . Select ( c => c . Value ) . ToArray ( ) ; var blackMoves = match . Groups [ " " ] . Captures . OfType < Capture > ( ) . Select ( c => c . Value ) . ToArray ( ) ; var result = match . Groups [ " " ] . Value ; if ( tagKeys . Length != tagValues . Length ) { throw new ArgumentException ( " " , nameof ( pgn ) ) ; } Game = new ChessGame ( ) ; for ( var i = 0 ; i < tagKeys . Length ; i ++ ) { var val = tagValues [ i ] ; switch ( tagKeys [ i ] . ToLowerInvariant ( ) ) { case " " : Game . Event = val ; break ; case " " : Game . Site = val ; break ; case " " : Game . StartDate = DateTimeOffset . ParseExact ( val , " " , CultureInfo . InvariantCulture ) ; break ; case " " : Game . Round = val ; break ; case " " : Game . WhitePlayer = val ; break ; case " " : Game . BlackPlayer = val ; break ; case " " : Game . Result = ChessFormatter . ResultFromString ( val ) ; break ; case " " : Game . LoadFEN ( val ) ; break ; default : break ; } } if ( Game . Result == GameResult . Ongoing ) { Game . Result = ChessFormatter . ResultFromString ( result ) ; } for ( var i = 0 ; i < whiteMoves . Length ; i ++ ) { var whiteMove = MoveFromAlgebraicNotation ( whiteMoves [ i ] ) ; Game . Move ( whiteMove ) ; if ( i < blackMoves . Length ) { var blackMove = MoveFromAlgebraicNotation ( blackMoves [ i ] ) ; Game . Move ( blackMove ) ; } } }
Loads chess game from a PGN string.
Loads chess game from a PGN string .
// Parse PGN // Get tag pairs, moves, and result from the parsed output. // Create new game and assign properties based on PGN tag pairs // Game.Result defaults to Ongoing. If it has that value after evaluating // tag pairs, check whether the result notation at the end of the move list // gives the result. // Apply moves
pgn
string
A PGN description of the game to load.
A PGN description of the game to load .