/// <summary>
        /// Determines if the given <see cref="StartingPosition" /> is complete.
        /// </summary>
        /// <param name="value">The OptionValue instance.</param>
        /// <param name="validationContext">Validation context.</param>
        /// <returns>The result of the validation.</returns>
        protected override ValidationResult IsValid(
            object value,
            ValidationContext validationContext)
        {
            var startingPosition = (StartingPosition)value;
            int nullCount        = 0;

            nullCount += string.IsNullOrEmpty(startingPosition.Pgn) ? 1 : 0;
            nullCount += string.IsNullOrEmpty(startingPosition.Fen) ? 1 : 0;

            if (nullCount != 1)
            {
                return(new ValidationResult(MissingValueError));
            }

            if (!string.IsNullOrEmpty(startingPosition.Pgn))
            {
                var parser = new PgnGameParser();
                try
                {
                    parser.ParsePgn(startingPosition.Pgn);
                }
                catch (Exception e)
                {
                    return(new ValidationResult(InvalidPgnGameError + " " + e.Message));
                }
            }

            return(ValidationResult.Success);
        }
Ejemplo n.º 2
0
        public void CanParseDebugGame()
        {
            var parser       = new PgnGameParser();
            var game         = parser.ParseGame(TestResources.GameWithNullComment);
            var mainLineList = game.MainLineAsList;

            Assert.Equal(103, mainLineList.Count);
        }
Ejemplo n.º 3
0
        public void PlayerInformationExtractsCorrectly()
        {
            var parser = new PgnGameParser();
            var game   = parser.ParseGame(TestResources.JohnDavisBeatsSieica);

            Assert.Equal("johndavis_59", game.WhitePlayer.Name);
            Assert.Equal(1972, game.WhitePlayer.Rating !.Value);
            Assert.Null(game.WhitePlayer.Title);
        }
Ejemplo n.º 4
0
        public void ParsesLichessOrgGame()
        {
            var parser       = new PgnGameParser();
            var game         = parser.ParseGame(TestResources.JohnDavisBeatsSieica);
            var mainLineList = game.MainLineAsList;

            Assert.Equal(97, mainLineList.Count);

            var firstPly = mainLineList.First();

            Assert.Equal("e4", firstPly.San);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Determines the true starting position based on a potentially incomplete starting position.
        /// </summary>
        /// <param name="position">The incomplete position to extrapolate.</param>
        /// <returns>The completed starting position.</returns>
        /// <exception cref="MalformedStartingPositionException">On invalid starting position.</exception>
        public static StartingPosition ExtrapolateStartingPosition(StartingPosition position)
        {
            string pgn = position.Pgn;
            string fen = position.Fen;

            if (fen != null)
            {
                return(position);
            }

            var parser = new PgnGameParser();

            try
            {
                var pgnGame = parser.ParsePgn(pgn);
                var ply     = pgnGame.FirstMove;
                while (ply.NextMoveInMainLine != null)
                {
                    ply = ply.NextMoveInMainLine;
                }

                var board = ply.ToBoard();
                fen = board.ToFen();

                return(new StartingPosition()
                {
                    Fen = fen,
                    Pgn = pgn,
                });
            }
            catch (Exception e)
            {
                if (e is AlienChessException || e is PgnFormatException)
                {
                    throw new MalformedStartingPositionException("The provided starting position could not be parsed from the PGN format.", e);
                }
                else
                {
                    throw new MalformedStartingPositionException("The provided starting position could not be parsed from the PGN format.");
                }
            }
        }
Ejemplo n.º 6
0
        public void ProducesUsefulExceptions()
        {
            var parser = new PgnGameParser();

            Exception parseEx = null;

            try
            {
                parser.ParseGame(TestResources.GameWithIllegalNullMoveIndicator);
            }
            catch (Exception e)
            {
                parseEx = e;
            }

            Assert.NotNull(parseEx);
            Assert.IsType <Game.PgnParsingException>(parseEx);
            var parsingEx = (Game.PgnParsingException)parseEx;

            Assert.Equal(35, parsingEx.Line);
            Assert.Equal(43, parsingEx.Column);
        }