Beispiel #1
0
        public void King_Two_Move_No_Rook()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var move = g.PlayerMove(PlayerTypeEnum.White, "e1g1");

            Assert.IsFalse(move.Success);
        }
Beispiel #2
0
        public void PawnSwap()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("f7", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "f7f8");
            ChessPiece queen = b.Get("f8");

            Assert.IsTrue(moveResult.Success);
            Assert.IsTrue(queen.Type == PieceTypeEnum.Queen);
        }
Beispiel #3
0
        public void PotentialMoves()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("f7", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);

            PotentialMoveService s = new PotentialMoveService();
            s.GetPotentialMoves(g, PlayerTypeEnum.White);
            var whiteMoves = s.GetPotentialMoves(g, PlayerTypeEnum.White);
            var blackMoves = s.GetPotentialMoves(g, PlayerTypeEnum.Black);

            Assert.AreEqual(5, whiteMoves["e1"].Count);
            Assert.AreEqual(2, whiteMoves["f7"].Count);

            Assert.AreEqual(5, blackMoves["e8"].Count);
        }
Beispiel #4
0
        public void Castle()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("a1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Rook });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1c1");

            Assert.IsTrue(moveResult.Success);
            Assert.IsNotNull(moveResult.Moves);
            Assert.AreEqual(2, moveResult.Moves.Length);
            Assert.AreEqual("e1c1", moveResult.Moves[0]);
            Assert.AreEqual("a1d1", moveResult.Moves[1]);
            Assert.IsNotNull(g.ChessBoard.Get("d1"));
            Assert.AreEqual(PieceTypeEnum.Rook, g.ChessBoard.Get("d1").Type);
        }
Beispiel #5
0
        public void Overtake()
        {
            ChessBoard b = new ChessBoard();
            b.Set("g4", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });
            b.Set("h5", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Pawn });

            TraditionalRulesOfChess rules = new TraditionalRulesOfChess();

            ChessGame chessGame = new ChessGame(
                b,
                PlayerTypeEnum.White,
                null);

            var move = chessGame.PlayerMove(PlayerTypeEnum.White, "g4h5");

            Assert.IsTrue(move.Success, move.Error);
            Assert.IsNotNull(move.Taken);
            Assert.AreEqual(chessGame.GetTakenPiecesByPlayer(PlayerTypeEnum.White).Length, 1);
            Assert.AreEqual(chessGame.GetTakenPiecesByPlayer(PlayerTypeEnum.White)[0].Player, PlayerTypeEnum.Black);
        }
Beispiel #6
0
        public void White_Puts_Black_In_Check()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("d1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Queen });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "d1a4");

            Assert.IsTrue(moveResult.Success);
            Assert.IsTrue(moveResult.MoveType == MoveTypeEnum.Check);
        }
Beispiel #7
0
        public void Pawn()
        {
            ChessBoard b = new ChessBoard();

            b.Set("a2", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });
            b.Set("a7", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });
            b.Set("e7", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Pawn });
            b.Set("g4", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });
            b.Set("h5", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Pawn });

            TraditionalRulesOfChess rules = new TraditionalRulesOfChess();

            // basic move

            var move = rules.CanMove(b, "a2a3", PlayerTypeEnum.White, null);
            Assert.IsTrue(move.Success, move.Message);

            // move two squares on first move

            move = rules.CanMove(b, "a2a4", PlayerTypeEnum.White, null);
            Assert.IsTrue(move.Success, move.Message);

            // can't move two squares on subsequent moves

            move = rules.CanMove(b, "a2a5", PlayerTypeEnum.White, null);
            Assert.IsFalse(move.Success, move.Message);

            // can't move backwards

            move = rules.CanMove(b, "a2a1", PlayerTypeEnum.White, null);
            Assert.IsFalse(move.Success, move.Message);

            // piece does not exist

            move = rules.CanMove(b, "h2g3", PlayerTypeEnum.White, null);
            Assert.IsFalse(move.Success, move.Message);

            // g4 pawn over-take h5 pawn

            move = rules.CanMove(b, "g4h5", PlayerTypeEnum.White, null);
            Assert.IsTrue(move.Success, move.Message);

            b.RecordMoves(new[] { "a2a3" }, null, null);

            move = rules.CanMove(b, "a3a5", PlayerTypeEnum.White, null);
            Assert.IsFalse(move.Success, move.Message);

            move = rules.CanMove(b, "a3a4", PlayerTypeEnum.White, null);
            Assert.IsTrue(move.Success, move.Message);

            move = rules.CanMove(b, "a7a8", PlayerTypeEnum.White, null);
            Assert.IsTrue(move.Success, move.Message);

            move = rules.CanMove(b, "e7e6", PlayerTypeEnum.Black, null);
            Assert.IsTrue(move.Success, move.Message);

            move = rules.CanMove(b, "e7e8", PlayerTypeEnum.Black, null);
            Assert.IsFalse(move.Success, move.Message);
        }
Beispiel #8
0
        public void Cannot_Move_Into_Check()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });
            b.Set("h2", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Queen });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1e2");

            Assert.IsFalse(moveResult.Success);
        }
Beispiel #9
0
        public void Double_Check()
        {
            ChessBoard b = new ChessBoard();
            b.Set("h1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("h4", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Rook });
            b.Set("h5", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });
            b.Set("f4", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Bishop });

            b.Set("h6", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });
            b.Set("g7", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Pawn });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.Black, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.Black, "g7g5");
            Assert.IsTrue(moveResult.Success);

            moveResult = g.PlayerMove(PlayerTypeEnum.White, "h5g6");
            Assert.IsTrue(moveResult.Success);
        }
Beispiel #10
0
        public void Checkmate()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.King });
            b.Set("h2", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Queen });
            b.Set("g2", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Rook });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.Black, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.Black, "h2h1");

            Assert.IsTrue(moveResult.Success);
            Assert.IsTrue(moveResult.MoveType == MoveTypeEnum.Checkmate, "expected checkmate");
        }
Beispiel #11
0
        public void Castle_Block()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("a1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Rook });
            b.Set("b1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Knight });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1c1");

            Assert.IsFalse(moveResult.Success);
        }
Beispiel #12
0
        public void Cannot_Castle_Out_Of_Check()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("a1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Rook });

            b.Set("e8", new ChessPiece() { Player = PlayerTypeEnum.Black, Type = PieceTypeEnum.Queen });

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1c1");

            Assert.IsFalse(moveResult.Success);

            moveResult = g.PlayerMove(PlayerTypeEnum.White, "e1f1");

            Assert.IsTrue(moveResult.Success);
        }