public static void TestPromotion()
        {
            AntichessGame game = new AntichessGame("8/7P/6n1/8/8/8/8/8 w - - 0 1");

            Assert.AreEqual(5, game.GetValidMoves(Player.White).Count);
            Assert.True(game.IsValidMove(new Move("H7", "H8", Player.White, 'K')));
        }
        public static void TestIsWinnerWithNoPiecesLeft()
        {
            AntichessGame game = new AntichessGame("8/8/5B2/6P1/6B1/1P2P3/P1PQ1P1P/1N2K1NR b - - 0 19");

            Assert.True(game.IsStalemated(Player.Black));
            Assert.True(game.IsWinner(Player.Black));
        }
Пример #3
0
        void GenerateAntichessForcedCapturePositions(string pgn)
        {
            PgnReader <AntichessGame> pgnReader = new PgnReader <AntichessGame>();
            AntichessGame             copy      = new AntichessGame();

            pgnReader.ReadPgnFromString(pgn);
            string fen;
            ReadOnlyCollection <DetailedMove> moves = pgnReader.Game.Moves;

            if (moves.Count >= 2)
            {
                for (int i = 0; i < moves.Count; i++)
                {
                    copy.ApplyMove(moves[i], true);
                    if (i < 1)
                    {
                        continue;
                    }
                    fen = copy.GetFen();
                    ReadOnlyCollection <Move> validMoves = copy.GetValidMoves(copy.WhoseTurn);
                    DetailedMove lastMove = moves[i];
                    if (validMoves.Count != 1)
                    {
                        continue;
                    }
                    if (copy.GetPieceAt(validMoves[0].NewPosition) != null ||
                        (copy.GetPieceAt(validMoves[0].OriginalPosition) is Pawn && validMoves[0].OriginalPosition.File != validMoves[0].NewPosition.File))
                    {
                        fenQueue.Add(new Tuple <string, string[], string, string>(fen,
                                                                                  new string[] { lastMove.OriginalPosition.ToString().ToLowerInvariant(), lastMove.NewPosition.ToString().ToLowerInvariant() }, "Antichess", "forcedCapture"));
                    }
                }
            }
        }
        public static void TestFenGeneration()
        {
            AntichessGame game = new AntichessGame();

            game.MakeMove(new Move("E2", "E3", Player.White), true);
            Assert.AreEqual("rnbqkbnr/pppppppp/8/8/8/4P3/PPPP1PPP/RNBQKBNR b - - 0 1", game.GetFen());
        }
        public static void TestEnPassant()
        {
            AntichessGame game = new AntichessGame("rnbqkbnr/p1ppp2p/1p6/5PN1/8/1P6/P1PPPP1P/RNBQKB1R b - - 0 5");

            game.MakeMove(new Move("E7", "E5", Player.Black), true);
            Assert.True(game.IsValidMove(new Move("F5", "E6", Player.White)));
        }
        public static void TestIsWinnerWithAPieceLeft()
        {
            AntichessGame game = new AntichessGame("1nbqk3/3pp3/2p2p2/5P2/8/8/8/4r3 w - - 0 20");

            Assert.True(game.IsStalemated(Player.White));
            Assert.True(game.IsWinner(Player.White));
        }
Пример #7
0
 public static void TestGetValidMovesWhiteWithForcedCaptures()
 {
     AntichessGame game = new AntichessGame("rn1qkbnr/p1pppppp/b7/1B6/8/4P3/PPPP1PPP/RNBQK1NR w - - 1 3");
     ReadOnlyCollection<Move> validMoves = game.GetValidMoves(Player.White);
     Assert.AreEqual(2, validMoves.Count);
     Assert.Contains(new Move("B5", "A6", Player.White), validMoves);
     Assert.Contains(new Move("B5", "D7", Player.White), validMoves);
 }
        public static void TestGetValidMovesWhiteWithForcedCaptures()
        {
            AntichessGame             game       = new AntichessGame("rn1qkbnr/p1pppppp/b7/1B6/8/4P3/PPPP1PPP/RNBQK1NR w - - 1 3");
            ReadOnlyCollection <Move> validMoves = game.GetValidMoves(Player.White);

            Assert.AreEqual(2, validMoves.Count);
            Assert.Contains(new Move("B5", "A6", Player.White), validMoves);
            Assert.Contains(new Move("B5", "D7", Player.White), validMoves);
        }
        public static void TestUndoEnPassant()
        {
            string        fen  = "rnbqkb1r/pppp1ppp/7n/8/5pP1/7P/PPPPP3/RNBQKBNR b - g3 0 4";
            AntichessGame game = new AntichessGame(fen);

            game.MakeMove(new Move("F4", "G3", Player.Black), true);
            Assert.True(game.Undo());
            Assert.AreEqual(fen, game.GetFen());
            Assert.AreEqual(Player.Black, game.WhoseTurn);
        }
        public static void TestUndoPromotion()
        {
            string        fen  = "8/5P2/2b5/8/8/8/5b2/8 w - - 0 39";
            AntichessGame game = new AntichessGame(fen);

            game.MakeMove(new Move("F7", "F8", Player.White, 'K'), true);
            Assert.True(game.Undo());
            Assert.AreEqual(fen, game.GetFen());
            Assert.AreEqual(Player.White, game.WhoseTurn);
        }
        public static void TestUndoCapturePromotion()
        {
            string        fen  = "rnbqkb1r/pppp1ppp/7n/8/8/7P/PPPPPp2/RNB1QBNR b - - 1 6";
            AntichessGame game = new AntichessGame(fen);

            game.MakeMove(new Move("F2", "E1", Player.Black, 'R'), true);
            Assert.True(game.Undo());
            Assert.AreEqual(fen, game.GetFen());
            Assert.AreEqual(Player.Black, game.WhoseTurn);
        }
        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.MakeMove(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.MakeMove(valid, true);
                        ReadOnlyCollection <Move> validMoves2 = copy.GetValidMoves(copy.WhoseTurn);
                        bool wonForAll = true;
                        foreach (Move valid2 in validMoves2)
                        {
                            AntichessGame copy2 = new AntichessGame(copy.GetFen());
                            copy2.MakeMove(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();
        }
        public static void TestUndoMove()
        {
            AntichessGame game       = new AntichessGame();
            string        initialFen = game.GetFen();

            game.MakeMove(new Move("E2", "E3", Player.White), true);
            Assert.True(game.Undo());
            Assert.AreEqual(initialFen, game.GetFen());
            Assert.AreEqual(0, game.Moves.Count);
            Assert.AreEqual(Player.White, game.WhoseTurn);
        }
        public static void TestNoCastling()
        {
            AntichessGame game = new AntichessGame();

            game.MakeMove(new Move("E2", "E3", Player.White), true);
            game.MakeMove(new Move("E7", "E6", Player.Black), true);
            game.MakeMove(new Move("G1", "F3", Player.White), true);
            game.MakeMove(new Move("G8", "F6", Player.Black), true);
            game.MakeMove(new Move("F1", "E2", Player.White), true);
            game.MakeMove(new Move("F8", "E7", Player.Black), true);
            Assert.False(game.IsValidMove(new Move("E1", "G1", Player.White)));
        }
        public static void TestUndoMultiple()
        {
            string        fen  = "rnbqkb1r/pppp1ppp/7n/8/5pP1/7P/PPPPP3/RNBQKBNR b - g3 0 4";
            AntichessGame game = new AntichessGame(fen);

            game.MakeMove(new Move("F4", "G3", Player.Black), true);
            game.MakeMove(new Move("E1", "F2", Player.White), true);
            game.MakeMove(new Move("G3", "F2", Player.Black), true);
            game.MakeMove(new Move("D1", "E1", Player.White), true);
            game.MakeMove(new Move("F2", "E1", Player.White, 'N'), true);
            Assert.AreEqual(5, game.Undo(5));
            Assert.AreEqual(fen, game.GetFen());
            Assert.AreEqual(Player.Black, game.WhoseTurn);
        }
        public static void TestUndoCapture()
        {
            AntichessGame game = new AntichessGame();

            game.MakeMove(new Move("E2", "E3", Player.White), true);
            game.MakeMove(new Move("B7", "B5", Player.Black), true);
            string expected = game.GetFen();

            game.MakeMove(new Move("F1", "B5", Player.White), true);
            Assert.True(game.Undo());
            Assert.AreEqual(expected, game.GetFen());
            Assert.AreEqual(2, game.Moves.Count);
            Assert.AreEqual(Player.White, game.WhoseTurn);
        }
        public SubmittedMoveResponse SubmitMove(Move move)
        {
            SubmittedMoveResponse response = new SubmittedMoveResponse();
            MoveType type = Game.ApplyMove(move, false);

            if (type == MoveType.Invalid)
            {
                response.Success = false;
                response.Error   = "Invalid move.";
                response.Correct = -3;
                return(response);
            }
            response.Success = true;
            response.Check   = Game.IsInCheck(Game.WhoseTurn) ? Game.WhoseTurn.ToString().ToLowerInvariant() : null;
            response.FEN     = Game.GetFen();

            if (Game.IsWinner(ChessUtilities.GetOpponentOf(Game.WhoseTurn)))
            {
                response.Correct = 1;
                return(response);
            }
            else if (Game.IsWinner(Game.WhoseTurn))
            {
                response.Correct = -3;
                return(response);
            }
            else if (Game.DrawCanBeClaimed)
            {
                response.Correct = -1;
                return(response);
            }
            else if (Game.IsStalemated(Game.WhoseTurn))
            {
                response.Correct = -2;
                return(response);
            }

            response.Correct = 0;

            Move chosen = null;

            if (Game is AtomicChessGame)
            {
                Position whiteKing = null;
                Position blackKing = null;
                for (int i = 0; i < 8; i++)
                {
                    for (int j = 1; j < 9; j++)
                    {
                        if (Game.GetPieceAt((File)i, j) == new King(Player.White))
                        {
                            whiteKing = new Position((File)i, j);
                        }

                        if (Game.GetPieceAt((File)i, j) == new King(Player.Black))
                        {
                            blackKing = new Position((File)i, j);
                        }

                        if (whiteKing != null && blackKing != null)
                        {
                            break;
                        }
                    }
                }

                ReadOnlyCollection <Move> validKingMoves  = Game.GetValidMoves(blackKing);
                List <Move> validKingMovesToAdjacentKings = new List <Move>();
                foreach (Move validMove in validKingMoves)
                {
                    PositionDistance distance = new PositionDistance(whiteKing, validMove.NewPosition);
                    if (distance.DistanceX <= 1 && distance.DistanceY <= 1)
                    {
                        validKingMovesToAdjacentKings.Add(validMove);
                    }
                }
                if (validKingMovesToAdjacentKings.Count > 0)
                {
                    chosen = validKingMovesToAdjacentKings[rnd.Next(0, validKingMovesToAdjacentKings.Count)];
                }
                else
                {
                    chosen = validKingMoves[rnd.Next(0, validKingMoves.Count)];
                }
            }
            else // (Game is AntichessGame)
            {
                ReadOnlyCollection <Move> validMovesBlack = Game.GetValidMoves(Player.Black);
                foreach (Move validMove in validMovesBlack)
                {
                    AntichessGame copy = new AntichessGame(Game.GetFen());
                    copy.ApplyMove(validMove, true);
                    ReadOnlyCollection <Move> validMovesWhite = copy.GetValidMoves(Player.White);
                    if (validMovesWhite.Any(x => copy.GetPieceAt(x.NewPosition) != null))
                    {
                        chosen = validMove;
                        break;
                    }
                    if (chosen == null)
                    {
                        bool thisOne = true;
                        foreach (Move vmWhite in validMovesWhite)
                        {
                            AntichessGame copy2 = new AntichessGame(copy.GetFen());
                            copy2.ApplyMove(vmWhite, true);
                            ReadOnlyCollection <Move> validMovesBlackStep2 = copy2.GetValidMoves(Player.Black);
                            if (validMovesBlackStep2.Any(x => copy2.GetPieceAt(x.NewPosition) != null))
                            {
                                thisOne = false;
                            }
                        }
                        if (thisOne)
                        {
                            chosen = validMove;
                        }
                    }
                }
                if (chosen == null)
                {
                    chosen = validMovesBlack[rnd.Next(0, validMovesBlack.Count)];
                }
            }
            Game.ApplyMove(chosen, true);
            response.CheckAfterAutoMove = Game.IsInCheck(Game.WhoseTurn) ? Game.WhoseTurn.ToString().ToLowerInvariant() : null;
            response.WinAfterAutoMove   = Game.IsWinner(Game.WhoseTurn);
            response.FenAfterPlay       = Game.GetFen();
            response.Moves    = Game.GetValidMoves(Game.WhoseTurn);
            response.LastMove = new string[2] {
                chosen.OriginalPosition.ToString().ToLowerInvariant(), chosen.NewPosition.ToString().ToLowerInvariant()
            };
            if (Game.DrawCanBeClaimed)
            {
                response.DrawAfterAutoMove = true;
            }
            else
            {
                response.DrawAfterAutoMove = false;
            }

            return(response);
        }
Пример #18
0
 public static void TestFenGeneration()
 {
     AntichessGame game = new AntichessGame();
     game.ApplyMove(new Move("E2", "E3", Player.White), true);
     Assert.AreEqual("rnbqkbnr/pppppppp/8/8/8/4P3/PPPP1PPP/RNBQKBNR b - - 0 1", game.GetFen());
 }
Пример #19
0
 public static void TestIsWinnerWithNoPiecesLeft()
 {
     AntichessGame game = new AntichessGame("8/8/5B2/6P1/6B1/1P2P3/P1PQ1P1P/1N2K1NR b - - 0 19");
     Assert.True(game.IsStalemated(Player.Black));
     Assert.True(game.IsWinner(Player.Black));
 }
Пример #20
0
 public static void TestIsInvalidMove_WrongTurnColor()
 {
     AntichessGame game = new AntichessGame("rnbqkbnr/p1pppppp/8/1p6/8/4P3/PPPP1PPP/RNBQKBNR w - - 0 2");
     Assert.False(game.IsValidMove(new Move("H7", "H6", Player.Black)));
 }
        public static void TestGetValidMovesBlackNoForcedCaptures()
        {
            AntichessGame game = new AntichessGame("rnbqkbnr/pppppppp/8/8/8/4P3/PPPP1PPP/RNBQKBNR b - - 0 1");

            Assert.AreEqual(20, game.GetValidMoves(Player.Black).Count);
        }
        public static void TestIsInvalidMove_IllegalMove()
        {
            AntichessGame game = new AntichessGame();

            Assert.False(game.IsValidMove(new Move("G1", "G3", Player.White)));
        }
Пример #23
0
 public static void TestIsWinnerWithAPieceLeft()
 {
     AntichessGame game = new AntichessGame("1nbqk3/3pp3/2p2p2/5P2/8/8/8/4r3 w - - 0 20");
     Assert.True(game.IsStalemated(Player.White));
     Assert.True(game.IsWinner(Player.White));
 }
Пример #24
0
 public static void TestIsInvalidMove_IllegalMove()
 {
     AntichessGame game = new AntichessGame();
     Assert.False(game.IsValidMove(new Move("G1", "G3", Player.White)));
 }
Пример #25
0
 public static void TestNoCastling()
 {
     AntichessGame game = new AntichessGame();
     game.ApplyMove(new Move("E2", "E3", Player.White), true);
     game.ApplyMove(new Move("E7", "E6", Player.Black), true);
     game.ApplyMove(new Move("G1", "F3", Player.White), true);
     game.ApplyMove(new Move("G8", "F6", Player.Black), true);
     game.ApplyMove(new Move("F1", "E2", Player.White), true);
     game.ApplyMove(new Move("F8", "E7", Player.Black), true);
     Assert.False(game.IsValidMove(new Move("E1", "G1", Player.White)));
 }
        public static void TestIsValidMoveNoForcedCapture()
        {
            AntichessGame game = new AntichessGame();

            Assert.True(game.IsValidMove(new Move("G1", "H3", Player.White)));
        }
Пример #27
0
 public static void TestIsInvalidMove_ForcedCaptureExists()
 {
     AntichessGame game = new AntichessGame("rnbqkbnr/p1pppppp/8/1p6/8/4P3/PPPP1PPP/RNBQKBNR w - - 0 2");
     Assert.False(game.IsValidMove(new Move("H2", "H3", Player.White)));
 }
        public static void TestIsInvalidMove_ForcedCaptureExists()
        {
            AntichessGame game = new AntichessGame("rnbqkbnr/p1pppppp/8/1p6/8/4P3/PPPP1PPP/RNBQKBNR w - - 0 2");

            Assert.False(game.IsValidMove(new Move("H2", "H3", Player.White)));
        }
Пример #29
0
 public static void TestIsValidMoveForcedCapture()
 {
     AntichessGame game = new AntichessGame("rnbqkbnr/p1pppppp/8/1p6/8/4P3/PPPP1PPP/RNBQKBNR w - - 0 2");
     Assert.True(game.IsValidMove(new Move("F1", "B5", Player.White)));
 }
        public static void TestIsInvalidMove_WrongTurnColor()
        {
            AntichessGame game = new AntichessGame("rnbqkbnr/p1pppppp/8/1p6/8/4P3/PPPP1PPP/RNBQKBNR w - - 0 2");

            Assert.False(game.IsValidMove(new Move("H7", "H6", Player.Black)));
        }
        public static void TestNotStalemated()
        {
            AntichessGame game = new AntichessGame("8/7P/6n1/8/8/8/8/8 w - - 0 1");

            Assert.False(game.IsStalemated(Player.White));
        }
Пример #32
0
 public static void TestIsValidMoveNoForcedCapture()
 {
     AntichessGame game = new AntichessGame();
     Assert.True(game.IsValidMove(new Move("G1", "H3", Player.White)));
 }
Пример #33
0
 public static void TestGetValidMovesBlackNoForcedCaptures()
 {
     AntichessGame game = new AntichessGame("rnbqkbnr/pppppppp/8/8/8/4P3/PPPP1PPP/RNBQKBNR b - - 0 1");
     Assert.AreEqual(20, game.GetValidMoves(Player.Black).Count);
 }
Пример #34
0
 public static void TestNotStalemated()
 {
     AntichessGame game = new AntichessGame("8/7P/6n1/8/8/8/8/8 w - - 0 1");
     Assert.False(game.IsStalemated(Player.White));
 }
Пример #35
0
 public static void TestEnPassant()
 {
     AntichessGame game = new AntichessGame("rnbqkbnr/p1ppp2p/1p6/5PN1/8/1P6/P1PPPP1P/RNBQKB1R b - - 0 5");
     game.ApplyMove(new Move("E7", "E5", Player.Black), true);
     Assert.True(game.IsValidMove(new Move("F5", "E6", Player.White)));
 }
        public static void TestIsValidMoveForcedCapture()
        {
            AntichessGame game = new AntichessGame("rnbqkbnr/p1pppppp/8/1p6/8/4P3/PPPP1PPP/RNBQKBNR w - - 0 2");

            Assert.True(game.IsValidMove(new Move("F1", "B5", Player.White)));
        }
Пример #37
0
 public static void TestPromotion()
 {
     AntichessGame game = new AntichessGame("8/7P/6n1/8/8/8/8/8 w - - 0 1");
     Assert.AreEqual(5, game.GetValidMoves(Player.White).Count);
     Assert.True(game.IsValidMove(new Move("H7", "H8", Player.White, 'K')));
 }