public EndgameTrainingSession(string sessionId, ChessGame game)
        {
            SessionID = sessionId;
            Game      = game;
            if (Game is AtomicChessGame && Game.IsInCheck(Player.Black))
            {
                Game = new AtomicChessGame(Game.GetFen().Replace(" w ", " b "));
                ReadOnlyCollection <Move> validMoves = Game.GetValidMoves(Player.Black);
                if (validMoves.Count == 0)
                {
                    WasAlreadyLost = true;
                }
                else
                {
                    Game.ApplyMove(validMoves[rnd.Next(0, validMoves.Count)], true);
                    Game = new AtomicChessGame(string.Join(" ", Game.GetFen().Split(' ').Take(4)) + " 0 1");
                }
            }
            ReadOnlyCollection <Move> antiValidMoves;

            if (Game is AntichessGame && (antiValidMoves = Game.GetValidMoves(Player.White)).Count == 1 && Game.GetPieceAt(antiValidMoves[0].NewPosition) != null)
            {
                WasAlreadyLost = true;
            }
            InitialFEN = Game.GetFen();
        }
        public void TestExplosions()
        {
            Piece[][] board = new Piece[8][]
            {
                new Piece[8] { o, o, o, o, kb, o, o, o },
                new Piece[8] { o, o, o, o, o, o, o, o },
                new Piece[8] { o, o, o, o, o, o, o, o },
                new Piece[8] { o, o, o, pw, qb, o, o, o },
                new Piece[8] { o, o, o, bw, pb, o, o, o },
                new Piece[8] { o, o, o, o, o, o, nw, o },
                new Piece[8] { o, o, o, o, o, o, o, o },
                new Piece[8] { o, o, o, o, kw, o, o, o }
            };

            AtomicChessGame game = new AtomicChessGame(board, Player.White);
            Assert.AreEqual(game.ApplyMove(new Move("G3", "E4", Player.White), true), MoveType.Move | MoveType.Capture);

            Piece[][] expected = new Piece[8][]
            {
                new Piece[8] { o, o, o, o, kb, o, o, o },
                new Piece[8] { o, o, o, o, o, o, o, o },
                new Piece[8] { o, o, o, o, o, o, o, o },
                new Piece[8] { o, o, o, pw, o, o, o, o },
                new Piece[8] { o, o, o, o, o, o, o, o },
                new Piece[8] { o, o, o, o, o, o, o, o },
                new Piece[8] { o, o, o, o, o, o, o, o },
                new Piece[8] { o, o, o, o, kw, o, o, o }
            };
            Piece[][] actual = game.GetBoard();
            Assert.AreEqual(expected, actual);
        }
Esempio n. 3
0
        public void TestFenCastlingFieldAfterRookExplosion_BlackQueenside()
        {
            AtomicChessGame game = new AtomicChessGame("rnbqkbnr/pp2pppp/8/1Npp4/8/8/PPPPPPPP/R1BQKBNR w KQkq - 0 3");

            game.ApplyMove(new Move("b5", "a7", Player.White), true);
            Assert.False(game.CanBlackCastleQueenSide);
            Assert.AreEqual("2bqkbnr/1p2pppp/8/2pp4/8/8/PPPPPPPP/R1BQKBNR b KQk - 0 3", game.GetFen());
        }
Esempio n. 4
0
        public void TestFenCastlingFieldAfterRookExplosion_WhiteKingside()
        {
            AtomicChessGame game = new AtomicChessGame("rnbqkb1r/pppppppp/8/8/5Pn1/2P5/PPQPP1PP/RNB1KBNR b KQkq - 2 3");

            game.ApplyMove(new Move("g4", "h2", Player.Black), true);
            Assert.False(game.CanWhiteCastleKingSide);
            Assert.AreEqual("rnbqkb1r/pppppppp/8/8/5P2/2P5/PPQPP1P1/RNB1KB2 w Qkq - 0 4", game.GetFen());
        }
Esempio n. 5
0
        public void TestFenCastlingFieldAfterRookExplosion_WhiteQueenside()
        {
            AtomicChessGame game = new AtomicChessGame("r1bqkbnr/pppppppp/8/8/1nPP4/6P1/PP2PP1P/RNBQKBNR b KQkq - 0 3");

            game.ApplyMove(new Move("b4", "a2", Player.Black), true);
            Assert.False(game.CanWhiteCastleQueenSide);
            Assert.AreEqual("r1bqkbnr/pppppppp/8/8/2PP4/6P1/1P2PP1P/2BQKBNR w Kkq - 0 4", game.GetFen());
        }
Esempio n. 6
0
        public void TestFenCastlingFieldAfterRookExplosion_BlackKingside()
        {
            AtomicChessGame game = new AtomicChessGame("rnbqkbnr/ppp3pp/3pp3/5pN1/8/4P3/PPPP1PPP/RNBQKB1R w KQkq - 0 4");

            game.ApplyMove(new Move("g5", "h7", Player.White), true);
            Assert.False(game.CanBlackCastleKingSide);
            Assert.AreEqual("rnbqkb2/ppp3p1/3pp3/5p2/8/4P3/PPPP1PPP/RNBQKB1R b KQq - 0 4", game.GetFen());
        }
        public EndgameTrainingSession(string sessionId, ChessGame game)
        {
            SessionID = sessionId;
            Game      = game;
            if (Game is AtomicChessGame && Game.IsInCheck(Player.Black))
            {
                Game = new AtomicChessGame(Game.GetFen().Replace(" w ", " b "));
                ReadOnlyCollection <Move> validMoves = Game.GetValidMoves(Player.Black);
                if (validMoves.Count == 0)
                {
                    WasAlreadyLost = true;
                }
                else
                {
                    Game.ApplyMove(validMoves[rnd.Next(0, validMoves.Count)], true);
                    Game = new AtomicChessGame(string.Join(" ", Game.GetFen().Split(' ').Take(4)) + " 0 1");
                }
            }
            ReadOnlyCollection <Move> antiValidMoves;

            if (Game is AntichessGame)
            {
                if ((antiValidMoves = Game.GetValidMoves(Player.White)).Count == 1 && Game.GetPieceAt(antiValidMoves[0].NewPosition) != null)
                {
                    WasAlreadyLost = true;
                }
                else
                {
                    WasAlreadyLost = true;
                    foreach (Move valid in antiValidMoves)
                    {
                        AntichessGame copy = new AntichessGame(Game.GetFen());
                        copy.ApplyMove(valid, true);
                        ReadOnlyCollection <Move> validMoves2 = copy.GetValidMoves(copy.WhoseTurn);
                        bool wonForAll = true;
                        foreach (Move valid2 in validMoves2)
                        {
                            AntichessGame copy2 = new AntichessGame(copy.GetFen());
                            copy2.ApplyMove(valid2, true);
                            if (copy2.GetValidMoves(copy2.WhoseTurn).Any(x => copy2.GetPieceAt(x.NewPosition) != null))
                            {
                                wonForAll = false;
                                break;
                            }
                        }
                        if (wonForAll)
                        {
                            WasAlreadyLost = false;
                            break;
                        }
                    }
                }
            }
            InitialFEN = Game.GetFen();
        }
Esempio n. 8
0
        public void TestInvalidMove_CapturingOwn()
        {
            AtomicChessGame game = new AtomicChessGame("rqknrnbb/pppppppp/8/8/8/8/PPPPPPPP/RQKNRNBB w KQkq - 0 1");

            string[] moves = { "g2g3", "d8c6", "h2h3", "f8e6", "f2f3", "f7f6", "a2a3", "g8f7", "b2b3", "g7g6", "c2c3", "h8g7", "e2e3", "b7b6", "d2d3", "b8b7", "c3c4" };
            foreach (string m in moves)
            {
                game.ApplyMove(new Move(m.Substring(0, 2), m.Substring(2, 2), game.WhoseTurn), true);
            }

            Assert.False(game.IsValidMove(new Move("C8", "C7", Player.Black)));
        }
Esempio n. 9
0
        public void Test960Castling2()
        {
            AtomicChessGame game = new AtomicChessGame("bnrbkrnq/pppppppp/8/8/8/8/PPPPPPPP/BNRBKRNQ w KQkq - 0 1");

            string[] moves = { "b2b3", "h7h6", "a1b2", "g7g6", "b1a3", "e7e6", "c2c3", "d7d6", "d1c2", "c7c6", "g1f3", "b7b6", "h2h3", "a7a6", "h1h2", "f7f6" };
            foreach (string m in moves)
            {
                game.ApplyMove(new Move(m.Substring(0, 2), m.Substring(2, 2), game.WhoseTurn), true);
            }

            Assert.True(game.IsValidMove(new Move("E1", "C1", Player.White)));
            Assert.True(game.IsValidMove(new Move("E1", "F1", Player.White)));
        }
Esempio n. 10
0
 public void TestVariantEnd()
 {
     Piece[][] board = new Piece[8][]
     {
         new[] { rb, nb, bb, qb, kb, bb, nb, rb },
         new[] { o, pb, pb, pb, o, pb, pb, pb },
         new[] { pb, o, o, o, pb, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, pw, qw, o },
         new[] { pw, pw, pw, pw, o, pw, pw, pw },
         new[] { rw, nw, bw, o, kw, bw, nw, rw }
     };
     AtomicChessGame game = new AtomicChessGame(board, Player.White);
     game.ApplyMove(new Move("F3", "F7", Player.White), true);
     Assert.True(game.KingIsGone(Player.Black));
     Assert.True(game.IsWinner(Player.White));
 }
Esempio n. 11
0
        public void TestVariantEnd()
        {
            Piece[][] board = new Piece[8][]
            {
                new[] { rb, nb, bb, qb, kb, bb, nb, rb },
                new[] { o, pb, pb, pb, o, pb, pb, pb },
                new[] { pb, o, o, o, pb, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, pw, qw, o },
                new[] { pw, pw, pw, pw, o, pw, pw, pw },
                new[] { rw, nw, bw, o, kw, bw, nw, rw }
            };
            AtomicChessGame game = new AtomicChessGame(board, Player.White);

            game.ApplyMove(new Move("F3", "F7", Player.White), true);
            Assert.True(game.KingIsGone(Player.Black));
            Assert.True(game.IsWinner(Player.White));
        }
Esempio n. 12
0
        public void TestCheckmate()
        {
            Piece[][] board = new Piece[8][]
            {
                new[] { o, kb, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, qw },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, o, o, o, o },
                new[] { o, o, o, o, kw, o, o, o }
            };
            AtomicChessGame game = new AtomicChessGame(board, Player.White);

            game.ApplyMove(new Move("H7", "B7", Player.White), true);
            Assert.True(game.IsCheckmated(Player.Black));
            Assert.True(game.IsWinner(Player.White));
        }
Esempio n. 13
0
 public void TestFenCastlingFieldAfterRookExplosion_BlackKingside()
 {
     AtomicChessGame game = new AtomicChessGame("rnbqkbnr/ppp3pp/3pp3/5pN1/8/4P3/PPPP1PPP/RNBQKB1R w KQkq - 0 4");
     game.ApplyMove(new Move("g5", "h7", Player.White), true);
     Assert.False(game.CanBlackCastleKingSide);
     Assert.AreEqual("rnbqkb2/ppp3p1/3pp3/5p2/8/4P3/PPPP1PPP/RNBQKB1R b KQq - 0 4", game.GetFen());
 }
Esempio n. 14
0
 public void TestFenCastlingFieldAfterRookExplosion_BlackQueenside()
 {
     AtomicChessGame game = new AtomicChessGame("rnbqkbnr/pp2pppp/8/1Npp4/8/8/PPPPPPPP/R1BQKBNR w KQkq - 0 3");
     game.ApplyMove(new Move("b5", "a7", Player.White), true);
     Assert.False(game.CanBlackCastleQueenSide);
     Assert.AreEqual("2bqkbnr/1p2pppp/8/2pp4/8/8/PPPPPPPP/R1BQKBNR b KQk - 0 3", game.GetFen());
 }
Esempio n. 15
0
 public void TestFenCastlingFieldAfterRookExplosion_WhiteKingside()
 {
     AtomicChessGame game = new AtomicChessGame("rnbqkb1r/pppppppp/8/8/5Pn1/2P5/PPQPP1PP/RNB1KBNR b KQkq - 2 3");
     game.ApplyMove(new Move("g4", "h2", Player.Black), true);
     Assert.False(game.CanWhiteCastleKingSide);
     Assert.AreEqual("rnbqkb1r/pppppppp/8/8/5P2/2P5/PPQPP1P1/RNB1KB2 w Qkq - 0 4", game.GetFen());
 }
Esempio n. 16
0
        public void TestExplosions()
        {
            Piece[][] board = new Piece[8][]
            {
                new Piece[8] {
                    o, o, o, o, kb, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, pw, qb, o, o, o
                },
                new Piece[8] {
                    o, o, o, bw, pb, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, nw, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, kw, o, o, o
                }
            };

            AtomicChessGame game = new AtomicChessGame(board, Player.White);

            Assert.AreEqual(game.ApplyMove(new Move("G3", "E4", Player.White), true), MoveType.Move | MoveType.Capture);

            Piece[][] expected = new Piece[8][]
            {
                new Piece[8] {
                    o, o, o, o, kb, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, pw, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, o, o, o, o
                },
                new Piece[8] {
                    o, o, o, o, kw, o, o, o
                }
            };
            Piece[][] actual = game.GetBoard();
            Assert.AreEqual(expected, actual);
        }
Esempio n. 17
0
 public void TestFenCastlingFieldAfterRookExplosion_WhiteQueenside()
 {
     AtomicChessGame game = new AtomicChessGame("r1bqkbnr/pppppppp/8/8/1nPP4/6P1/PP2PP1P/RNBQKBNR b KQkq - 0 3");
     game.ApplyMove(new Move("b4", "a2", Player.Black), true);
     Assert.False(game.CanWhiteCastleQueenSide);
     Assert.AreEqual("r1bqkbnr/pppppppp/8/8/2PP4/6P1/1P2PP1P/2BQKBNR w Kkq - 0 4", game.GetFen());
 }
Esempio n. 18
0
 public void TestCheckmate()
 {
     Piece[][] board = new Piece[8][]
     {
         new[] { o, kb, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, qw },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, o, o, o, o },
         new[] { o, o, o, o, kw, o, o, o }
     };
     AtomicChessGame game = new AtomicChessGame(board, Player.White);
     game.ApplyMove(new Move("H7", "B7", Player.White), true);
     Assert.True(game.IsCheckmated(Player.Black));
     Assert.True(game.IsWinner(Player.White));
 }