Exemple #1
0
        internal Game GetEmptyGame()
        {
            var pgn = new Pgn();

            pgn.Add("1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 a6 6.Be3 e5 7.Nb3 Be7 8.f3 Be6");
            pgn.Add("9.Qd2 O-O 10.O-O-O a5 11.a4 Nc6 12.g4 Nb4 13.Kb1 Qc7 14.Bb5 Rac8 15.g5 Nh5");
            pgn.Add("16.Rhg1 g6 17.Qf2 Bd8 18.Qd2 Be7 19.Qf2 Bd8 20.Qd2 Be7 21.Qf2 1/2-1/2");

            return(new Game(new Metadata(), pgn));
        }
        public async Task <IActionResult> Edit(Pgn model)
        {
            if (ModelState.IsValid)
            {
                await pgnRepo.Update(model);

                return(RedirectToAction("index"));
            }

            return(View(model));
        }
Exemple #3
0
        public TestFixture()
        {
            FakeReader = Substitute.For <IReadPgnFiles>();
            FakeWriter = Substitute.For <IWritePgnFiles>();
            FakeRepo   = Substitute.For <IGameRepository>();
            FakeLogger = Substitute.For <ILogger>();

            PgnWithAtLeast15Moves = new Pgn();
            PgnWithAtLeast15Moves.Add("1.e4 e5 2.Bc4 Bc5 3.c3 Nf6 4.d4 exd4 5.cxd4 Bb6 6.Nc3 O-O 7.Nge2 c6 8.Bd3 d5 9.e5 Ne8 10.Be3 f6 11.Qd2 fxe5 12.dxe5 Be6 13.Nf4 Qe7 14.Bxb6 axb6 15.O-O Nd7");

            PgnWithLessThan15Moves = new Pgn();
            PgnWithLessThan15Moves.Add("1.e4 e5 2.Bc4 Bc5 3.c3 Nf6 4.d4 exd4 5.cxd4 Bb6 6.Nc3 O-O 7.Nge2 c6 8.Bd3 d5");
        }
Exemple #4
0
        public void Of_BuildPgn_Succeeds(string pgnFile)
        {
            // Arrange
            string lines = ResourceHelper.ReadEmbeddedRessource(pgnFile);

            // Act
            Option <Pgn> expected = Pgn.Import(lines);

            // Assert
            expected.Match(
                None: () => Assert.Fail(),
                Some: p => Assert.IsTrue(true));
        }
Exemple #5
0
        public void ReadGame(Action <Game> returnGame)
        {
            var metadata = new Metadata();
            var pgn      = new Pgn();
            var files    = Directory.EnumerateFiles(_folder, "*.pgn", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                using (var reader = new StreamReader(file))
                {
                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        if (ContainsMetadata(line))
                        {
                            metadata.Add(line);
                        }

                        if (ContainsMoves(line))
                        {
                            pgn.Add(line);
                        }

                        if (ContainsResultAtTheEnd(line) && !ContainsMoves(line))
                        {
                            pgn.Add(line);
                        }

                        if (ContainsResultAtTheEnd(line))
                        {
                            returnGame(new Game(metadata, pgn)
                            {
                                Source = file
                            });
                            metadata = new Metadata();
                            pgn      = new Pgn();
                        }


                        if (!ContainsMetadata(line) && !ContainsMoves(line) &&
                            !ContainsResultAtTheEnd(line) && !ContainsEmpty(line))
                        {
                            metadata = new Metadata();
                            pgn      = new Pgn();
                        }
                    }
                }
            }
        }
Exemple #6
0
        public void Add_should_replace_star_with_draw()
        {
            // Arrange
            var pgn    = new Pgn();
            var spaces = GenerateRandomSpaces();

            var line     = $"1. d4 Nf6 2. c4 e6 3. Nf3 d5 4. g3 dxc4 5. Bg2 c6 *";
            var withDraw = "1.d4 Nf6 2.c4 e6 3.Nf3 d5 4.g3 dxc4 5.Bg2 c6 1/2-1/2 ";

            // Act
            pgn.Add(line);

            // Assert
            pgn.Moves.ShouldBe(withDraw);
        }
Exemple #7
0
        public void Add_should_remove_space_after_dot()
        {
            // Arrange
            var pgn    = new Pgn();
            var spaces = GenerateRandomSpaces();

            var line          = $"1. d4 Nf6 2. c4 e6 3. Nf3 d5 4. g3 dxc4 5. Bg2 c6";
            var withoutSpaces = "1.d4 Nf6 2.c4 e6 3.Nf3 d5 4.g3 dxc4 5.Bg2 c6 ";

            // Act
            pgn.Add(line);

            // Assert
            pgn.Moves.ShouldBe(withoutSpaces);
        }
Exemple #8
0
        public void PlyCount_should_count_all_moves_of_the_game()
        {
            // Arrange
            var line = "1.d4 Nf6 2.c4 e6 3.Nf3 d5 4.g3 dxc4 5.Bg2 c6";
            var pgn  = new Pgn();

            pgn.Add(line);


            // Act
            var game = new Game(new Metadata(), pgn);

            // Assert
            game.PlyCount.ShouldBe(10);
        }
Exemple #9
0
        private void BtnLoadPgn_Click(object sender, EventArgs e)
        {
            this.openFileDialog1             = new OpenFileDialog();
            this.openFileDialog1.Filter      = "pgn files (*.pgn)|*.pgn|All files (*.*)|*.*";
            this.openFileDialog1.FilterIndex = 0;
            this.openFileDialog1.Title       = "Browse Portable Game Notation files";
            var result = this.openFileDialog1.ShowDialog();

            if (DialogResult.OK.Equals(result))
            {
                string pgnString = string.Empty;
                Using(new StreamReader(this.openFileDialog1.FileName), reader => pgnString = reader.ReadToEnd());
                this.boardController.SetFromPgn(Pgn.Import(pgnString));
            }
        }
Exemple #10
0
        public void ToString_should_add_empty_line_between_metadata_and_moves()
        {
            // Arrange
            var line  = "[Event \"11th Kings Rapid women\"]";
            var moves = "1.d4 Nf6 2.c4 e6 3.Nf3 d5 4.g3 dxc4 5.Bg2 c6 ";
            var emptyLineInBetween = $"{line}{Environment.NewLine}[PlyCount \"10\"]{Environment.NewLine}{Environment.NewLine}{moves}";

            var metadata = new Metadata();

            metadata.Add(line);

            var pgn = new Pgn();

            pgn.Add(moves);

            var game = new Game(metadata, pgn);

            // Act
            var text = game.ToString();

            // Assert
            text.ShouldBe(emptyLineInBetween);
        }
Exemple #11
0
        internal Game GetGame()
        {
            var metadata = new Metadata();

            metadata.Add("[Event \"Gibraltar Masters 2017\"]");
            metadata.Add("[Site \"Caleta ENG\"]");
            metadata.Add("[Date \"2017.01.29\"]");
            metadata.Add("[Round \"6.65\"]");
            metadata.Add("[White \"Heinemann, Josefine\"]");
            metadata.Add("[Black \"Tsolakidou, Stavroula\"]");
            metadata.Add("[Result \"1/2-1/2\"]");
            metadata.Add("[WhiteElo \"2227\"]");
            metadata.Add("[BlackElo \"2387\"]");
            metadata.Add("[ECO \"B90\"]");
            metadata.Add("[EventDate \"2017.01.24\"]");

            var pgn = new Pgn();

            pgn.Add("1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 a6 6.Be3 e5 7.Nb3 Be7 8.f3 Be6");
            pgn.Add("9.Qd2 O-O 10.O-O-O a5 11.a4 Nc6 12.g4 Nb4 13.Kb1 Qc7 14.Bb5 Rac8 15.g5 Nh5");
            pgn.Add("16.Rhg1 g6 17.Qf2 Bd8 18.Qd2 Be7 19.Qf2 Bd8 20.Qd2 Be7 21.Qf2 1/2-1/2");

            return(new Game(metadata, pgn));
        }
 public async Task Delete(Pgn pgn)
 {
     _context.Pgns.Remove(pgn);
     await _context.SaveChangesAsync();
 }
 public async Task Update(Pgn pgn)
 {
     _context.Pgns.Update(pgn);
     await _context.SaveChangesAsync();
 }
 public async Task Add(Pgn pgn)
 {
     _context.Pgns.Add(pgn);
     await _context.SaveChangesAsync();
 }
        private Pgn ParsePGNString(string pgn)
        {
            pgn = pgn.Trim();
            string[] pgnArray = Regex.Split(pgn, @"(?<=\])\s*(?=1\.\s{0,1}[a-zA-Z])");
            string   headers;
            string   moves;
            Pgn      model;

            if (pgnArray.Length > 2)
            {
                throw new Exception("Invalid Format");
            }
            else
            {
                if (pgnArray.Length == 1)
                {
                    moves = pgnArray[0].Trim();
                    if (!moves.StartsWith("1"))
                    {
                        throw new Exception("Invalid Format");
                    }

                    if (!moves.EndsWith("0-1") && !moves.EndsWith("1-0") && !moves.EndsWith("1/2-1/2"))
                    {
                        throw new Exception("Invalid Format");
                    }

                    model = new Pgn
                    {
                        Moves = moves
                    };
                }
                else if (pgnArray.Length == 2)
                {
                    headers = pgnArray[0];
                    moves   = pgnArray[1].Trim();
                    if (!moves.StartsWith("1"))
                    {
                        throw new Exception("Invalid Format");
                    }

                    if (!moves.EndsWith("0-1") && !moves.EndsWith("1-0") && !moves.EndsWith("1/2-1/2"))
                    {
                        throw new Exception("Invalid Format");
                    }

                    if (!headers.Contains("[") && !headers.Contains("]"))
                    {
                        throw new Exception("Invalid Format");
                    }

                    model = new Pgn
                    {
                        Moves = moves
                    };

                    string[] headerArray = Regex.Split(headers, @"\s(?=\[)");
                    foreach (string h in headerArray)
                    {
                        string headerTitle  = Regex.Match(h, @"(?<=\[)[a-zA-Z]+").Value;
                        var    propertyName = model.GetType().GetProperty(headerTitle);
                        if (propertyName != null)
                        {
                            var headerData = h.Replace("\"", "")
                                             .Replace(headerTitle, "")
                                             .Replace("[", "")
                                             .Replace("]", "")
                                             .Trim();
                            propertyName.SetValue(model, headerData);
                        }
                    }
                    ;
                }
                else
                {
                    throw new Exception("Invalid Format");
                }
            }

            return(model);
        }