Ejemplo n.º 1
0
        public void King_Two_Move()
        {
            ChessBoard b = new ChessBoard();
            b.Set("e1", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.King });
            b.Set("f2", new ChessPiece() { Player = PlayerTypeEnum.White, Type = PieceTypeEnum.Pawn });

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

            ChessGame g = new ChessGame(b, PlayerTypeEnum.White, null);
            var move = g.PlayerMove(PlayerTypeEnum.White, "e1e2");
            move = g.PlayerMove(PlayerTypeEnum.Black, "e7e6");
            move = g.PlayerMove(PlayerTypeEnum.White, "e2g2");

            Assert.IsFalse(move.Success);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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");
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
        static void InteractiveConsole()
        {
            ChessBoard chessBoard = new ChessBoard();
            //(new TraditionalBoardStager()).Stage(chessBoard);
            //(new RookTestingBoardStager()).Stage(chessBoard);
            //(new BishopTestingBoardStager()).Stage(chessBoard);
            //(new QueenTestingBoardStager()).Stage(chessBoard);
            //(new EnPassantTestingBoardStager()).Stage(chessBoard);
            //(new PawnSwapBoardStager()).Stage(chessBoard);
            (new KnightTestingBoardStager()).Stage(chessBoard);

            ChessGame chessGame = new ChessGame(
                chessBoard,
                PlayerTypeEnum.White,
                new Dictionary<PlayerTypeEnum, List<ChessPiece>>());

            ChessPiece[,] state = chessBoard.CreateStateCopy();
            (new ConsoleBoardPrinter()).PrintBoard(state, PlayerTypeEnum.White);

            PlayerTypeEnum playerTurn = PlayerTypeEnum.White;

            Console.WriteLine("Input: 1 square [a2] to show possible moves; 2 square [a1b1] to move");

            string move;
            Console.Write("> ");
            while (!string.IsNullOrWhiteSpace(move = Console.ReadLine()))
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(move))
                        break;

                    if (move.Length == 2)
                    {
                        var cpyb = new ChessBoard(chessBoard.CreateStateCopy());

                        var pm = (new PotentialMoveService()).GetPotentialMoves(chessGame, playerTurn);

                        List<string> moves;
                        pm.TryGetValue(move, out moves);
                        moves = moves ?? new List<string>();

                        (new ConsoleBoardPrinter()).PrintBoard(state, PlayerTypeEnum.White, new HashSet<string>(moves));
                    }
                    else if (move.Length == 4)
                    {
                        var moveResult = chessGame.PlayerMove(playerTurn, move);
                        playerTurn = moveResult.Turn;

                        state = chessBoard.CreateStateCopy();

                        (new ConsoleBoardPrinter()).PrintBoard(state, PlayerTypeEnum.White);
                        Console.WriteLine("{0} - {1} - {2} - {3} - {4}", moveResult.Success, moveResult.Error, moveResult.Taken, moveResult.MoveType, moveResult.Turn);
                    }
                    else
                        Console.WriteLine("Not implemented.");

                    Console.Write("> ");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }