예제 #1
0
        public void EnPassantTest()
        {
            ChessBoard b = CreateBoardWithPositions(
                Pos("a5"), ChessPieceType.Pawn, 1,
                Pos("e1"), ChessPieceType.King, 1,
                Pos("b7"), ChessPieceType.Pawn, 2,
                Pos("e8"), ChessPieceType.King, 2);

            ApplyMove(b, Move("e1", "e2"));
            // move black pawn forward twice
            ApplyMove(b, Move("b7", "b5"));

            var possMoves         = b.GetPossibleMoves() as IEnumerable <ChessMove>;
            var enPassantExpected = GetMovesAtPosition(possMoves, Pos("a5"));

            enPassantExpected.Should().HaveCount(2, "pawn can move forward one or en passant").And
            .Contain(Move("a5", "a6")).And.Contain(Move("a5", "b6"));

            // Apply the en passant
            ApplyMove(b, Move("a5", "b6"));
            BlankView.PrintView(Console.Out, b);
            var pawn = b.GetPieceAtPosition(Pos("b6"));

            pawn.Player.Should().Be(1, "pawn performed en passant move");
            pawn.PieceType.Should().Be(ChessPieceType.Pawn);
            var captured = b.GetPieceAtPosition(Pos("b5"));

            captured.Player.Should().Be(0, "the pawn that moved to b5 was captured by en passant");
            b.Value.Should().Be(1);

            GetAllPiecesForPlayer(b, 1).Should().HaveCount(2, "white controls a pawn and king");
            GetAllPiecesForPlayer(b, 2).Should().HaveCount(1, "black only controls a king");

            // Undo the move and check the board state
            b.UndoLastMove();
            b.Value.Should().Be(0);
            pawn = b.GetPieceAtPosition(Pos("a5"));
            pawn.Player.Should().Be(1);
            captured = b.GetPieceAtPosition(Pos("b5"));
            captured.Player.Should().Be(2);
            var empty = b.GetPieceAtPosition(Pos("b6"));

            empty.Player.Should().Be(0);

            GetAllPiecesForPlayer(b, 1).Should().HaveCount(2, "white controls a pawn and king");
            GetAllPiecesForPlayer(b, 2).Should().HaveCount(2, "black controls a pawn and king");
        }
예제 #2
0
        public void Castling4()
        {
            ChessBoard b = CreateBoardWithPositions(
                Pos("e8"), ChessPieceType.King, 2,
                Pos("e1"), ChessPieceType.King, 1,
                Pos("h1"), ChessPieceType.RookKing, 1); //create a board with possible castling position

            ApplyMove(b, Move("e1", "g1"));             //castling king normally cant move this way
            ApplyMove(b, Move("e8", "e7"));
            var poss = b.GetPossibleMoves() as IEnumerable <ChessMove>;


            //var possible = b.GetPossibleMoves();
            //  possible.Should().BeEmpty();

            var Rooking = GetMovesAtPosition(poss, Pos("f1"));

            Rooking.Should().HaveCount(12, "After Castling");             //


            BlankView.PrintView(Console.Out, b);                 //view the board to see the castling
            var King     = b.GetPieceAtPosition(Pos("g1"));      //king should position at g1
            var RookKing = b.GetPieceAtPosition(Pos("f1"));      // rook should position at f1

            King.Player.Should().Be(1, "King Castling");         //verify player of the castling king
            RookKing.Player.Should().Be(1, "RookKing Castling"); //verify player of the castling rook
            King.PieceType.Should().Be(ChessPieceType.King);
            RookKing.PieceType.Should().Be(ChessPieceType.RookKing);

            b.UndoLastMove();             // undo twice undo kings move
            b.UndoLastMove();             //undo castling
            King = b.GetPieceAtPosition(Pos("e1"));
            King.Player.Should().Be(1);
            RookKing = b.GetPieceAtPosition(Pos("h1"));
            RookKing.Player.Should().Be(1);

            GetAllPiecesForPlayer(b, 1).Should().HaveCount(2, "white controls a King and RookKing");
            GetAllPiecesForPlayer(b, 2).Should().HaveCount(1, "black controls a King");
        }
예제 #3
0
        public void CastlingTest7()
        {
            ChessBoard b = CreateBoardWithPositions(
                Pos("e1"), ChessPieceType.King, 1,
                Pos("a1"), ChessPieceType.RookQueen, 1);

            var possMoves        = b.GetPossibleMoves() as IEnumerable <ChessMove>;
            var castlingExpected = GetMovesAtPosition(possMoves, Pos("e1"));

            castlingExpected.Should().HaveCount(6, "king can move (1) left, (2) right, (3) up, (4) diagonal left, (5) diagonal right, or (6) castling")
            .And.Contain(Move("e1", "d1"))
            .And.Contain(Move("e1", "f1"))
            .And.Contain(Move("e1", "e2"))
            .And.Contain(Move("e1", "d2"))
            .And.Contain(Move("e1", "f2"))
            .And.Contain(Move("e1", "c1"));

            // Apply the castling
            ApplyMove(b, Move("e1", "c1"));
            BlankView.PrintView(Console.Out, b);
            var king = b.GetPieceAtPosition(Pos("c1"));

            king.Player.Should().Be(1, "king performed castling move");
            king.PieceType.Should().Be(ChessPieceType.King);
            var rook = b.GetPieceAtPosition(Pos("d1"));

            rook.Player.Should().Be(1, "the queenside rook should take place next to the king on his right");

            // Undo the move and check the board state
            b.UndoLastMove();
            king = b.GetPieceAtPosition(Pos("e1"));
            king.Player.Should().Be(1);
            rook = b.GetPieceAtPosition(Pos("a1"));
            rook.Player.Should().Be(1);
            var empty = b.GetPieceAtPosition(Pos("b1"));

            empty.Player.Should().Be(0);
        }