Пример #1
0
        static void Main(string[] args)
        {
            using (var context = new GamesStoreContext())
            {
                var parser = new GameParser();
                var repo = new Repository<GameDTO>(context);
                for (int i = 1; i <= 8; i++)
                {
                    using (var errorLog = new StreamWriter($"..\\..\\puzzles\\errorLog.txt"))
                    using (var reader = new StreamReader($"..\\..\\puzzles\\m2_{i}.pgn"))
                    {
                        var builder = new StringBuilder();
                        var line = string.Empty;
                        while (!reader.EndOfStream)
                        {
                            builder.Clear();
                            builder.AppendLine(line);
                            while (!reader.EndOfStream && !(line = reader.ReadLine()).Contains("Event"))
                            {
                                if (line.Contains("Date"))
                                {
                                    line = line.Replace("-", ".");
                                    line.Replace("\"?\"", "\"????\"");
                                }

                                // Skip [Round] as it's not properly parsed by pgn
                                if (line.Contains("Round")) continue;

                                builder.AppendLine(line);
                            }

                            var text = builder.ToString();
                            text = text.TrimEnd('\r', '\n');
                            try
                            {
                                foreach (var game in parser.Parse(text))
                                {
                                    var gameDto = new GameDTO()
                                    {
                                        PgnString = text
                                    };

                                    repo.Insert(gameDto);
                                    repo.Save();
                                }
                            }
                            catch (Exception ex)
                            {
                                errorLog.WriteLine($"File m2_{i}.pgn\n Error: {ex.Message}\n While parsing FEN: {text}");
                            }
                        }
                    }
                    Console.WriteLine($"File m2_{i}.pgn processed");
                }
            }
        }
Пример #2
0
        public void Whether_Game_NotifiesWhenPuzzleFailed_On_PieceMove()
        {
            var failed = false;
            var parser = new GameParser();
            var result = parser.Parse(fenWhiteMove);
            var game = result.FirstOrDefault();
            game.PuzzleFailed += (b, m) => failed = true;
            var targetSquare = new Square(7, 7);

            var rook = game.Board.Pieces.FirstOrDefault(x => x.Type == PieceType.Rook
                    && x.PossibleMoves().Contains(targetSquare));

            game.Board.PutPiece(targetSquare, rook);

            Assert.That(failed, Is.True);
        }
Пример #3
0
        public void Whether_GameParser_ParsesMultipleGames_On_Parse()
        {
            var parser = new GameParser();

            var result = parser.Parse(gamesPgn);

            Assert.That(result != null);
            Assert.That(result.Count() == 2);
        }
Пример #4
0
        public void Whether_GameParser_ParsesIsWhiteMove_On_Parse(string fenGame, bool isWhiteMove)
        {
            var parser = new GameParser();
            var result = parser.Parse(fenGame);

            var game = result.FirstOrDefault();

            Assert.That(game != null);
            Assert.That(game.Board.IsWhiteMove == isWhiteMove);
        }
Пример #5
0
 public void Whether_GameParser_ParsesGame_On_Parse()
 {
     var parser = new GameParser();
     var result = parser.Parse(game);
     Assert.That(result != null);
 }
Пример #6
0
        public void Whether_GameParser_ParsesFENGameMoves_On_Parse()
        {
            var parser = new GameParser();
            var result = parser.Parse(fenBlackMove);

            var game = result.FirstOrDefault();

            Assert.That(game != null);
            Assert.That(game.MoveEntries != null);
            Assert.That(game.MoveEntries.Count() == 3);
        }
Пример #7
0
        public void Whether_Game_OpponentMakesNextMove_On_PieceMoved()
        {
            var pieceMoved = false;
            var parser = new GameParser();
            var result = parser.Parse(fenGame);
            var game = result.FirstOrDefault();
            var targetSquare = new Square(8, 3);

            var rook = game.Board.Pieces.FirstOrDefault(x => x.Type == PieceType.Rook
                    && x.PossibleMoves().Contains(targetSquare)
                    && x.Color == (game.Board.IsWhiteMove ? PieceColor.White : PieceColor.Black));

            var whiteQueen = game.Board.Pieces.FirstOrDefault(x =>
                    x.Type == PieceType.Queen
                    && x.Color == PieceColor.White);
            whiteQueen.PositionUpdated += (e, s) => pieceMoved = true;

            game.Board.PutPiece(targetSquare, rook);

            Assert.That(pieceMoved, Is.True);
        }
Пример #8
0
        public void Whether_Game_SendsPuzzleSolvedMessage_On_PuzzleSolved()
        {
            var solved = false;
            var parser = new GameParser();
            var result = parser.Parse(fenWhiteMove);
            var game = result.FirstOrDefault();
            var targetSquare = new Square(7, 8);
            Messenger.Default.Register<PuzzleCompletedMessage>(this, (msg) => solved = msg.IsSolved);

            var rook = game.Board.Pieces.FirstOrDefault(x => x.Type == PieceType.Rook
                    && x.PossibleMoves().Contains(targetSquare)
                    && x.Color == (game.Board.IsWhiteMove ? PieceColor.White : PieceColor.Black));

            game.Board.PutPiece(targetSquare, rook);

            Assert.That(solved, Is.True);
        }
Пример #9
0
        public void Whether_Game_ParsesPgnGame_On_Construct()
        {
            var parser = new GameParser();

            var game = parser.Parse(fenGame).FirstOrDefault();

            Assert.That(game.Board != null);
            Assert.That(game.Board.Pieces.Count() == 17);
        }