Ejemplo n.º 1
0
        public void GetLegalMoves_Should_ReturnAllLegalMoves(string fen, string from, string moves)
        {
            //	Arrange
            SUT board = new SUT(fen);
            List <ChessMove> legalMoves;
            List <string>    expectedMoves = new List <string>();

            //	Act
            foreach (string move in moves.Split(new char[] { ',' }))
            {
                expectedMoves.Add(move);
            }
            legalMoves = board.GetLegalMoves();

            //	Assert
            Assert.AreEqual(expectedMoves.Count, legalMoves.Count);
            foreach (ChessMove legalMove in legalMoves)
            {
                if (from == legalMove.From.AlgebraicNotation)
                {
                    bool found = false;
                    foreach (string expectedMove in expectedMoves)
                    {
                        if (expectedMove == legalMove.To.AlgebraicNotation)
                        {
                            found = true; break;
                        }
                    }
                    Assert.IsTrue(found);
                }
            }
        }
Ejemplo n.º 2
0
        public void GetPieceAt_Should_ThrowArgumentNullExecption()
        {
            //	Arrange
            SUT board = new SUT();

            //  Act
            board.GetPieceAt(null);
        }
Ejemplo n.º 3
0
        public void Move_Should_ThrowException_When_ToIsNull()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            board.Move(new ChessSquare("e2"), null);
        }
Ejemplo n.º 4
0
        public void Move_Should_ThrowException_When_FromIsNull()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            board.Move(null, new ChessSquare("e4"));
        }
Ejemplo n.º 5
0
        public void PutPiece_Should_ThrowException_When_PieceIsNull()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            board.PutPiece(null, new ChessSquare("a1"));
        }
Ejemplo n.º 6
0
        public void PutPiece_Should_ThrowException_When_SquareIsNull()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            board.PutPiece(new ChessPiece(ChessPieceKind.Rook, ChessColor.Black), null);
        }
Ejemplo n.º 7
0
        public void PutPiece_Should_ThrowException_When_ThereIsTwoKingOfTheSameColor()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            board.PutPiece(new ChessPiece(ChessPieceKind.King, ChessColor.Black), new ChessSquare("e5"));
        }
Ejemplo n.º 8
0
        public void RemovePieceAt_Should_ReturnNull()
        {
            //	Arrange
            SUT board = new SUT();

            //	Assert
            Assert.IsNull(board.RemovePieceAt(new ChessSquare("e4")));
        }
Ejemplo n.º 9
0
        public void InsufficientMaterial_Should_ReturnTrue(string fen)
        {
            //	Arrange
            SUT board = new SUT(fen);

            //	Assert
            Assert.IsTrue(board.IsDrawByInsufficientMaterial);
        }
Ejemplo n.º 10
0
        public void LoadFEN_Should_ThrowArgumentException()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            board.Clear();
            board.LoadFEN("rnbqkbnr/1p1p1ppp/p3p3/NP3/8/PPP2PPP/RNBQKB1R w KQkq - 0 5");
        }
Ejemplo n.º 11
0
        public void SANToMove_Should_ThrowArgumentException(string initialFEN, string san)
        {
            //	Arrange
            SUT board;

            //	Act
            board = new SUT(initialFEN);
            board.SANToMove(san);
        }
Ejemplo n.º 12
0
        public void Move_Should_ThrowException_When_MoveIsInvalid()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            ChessMove invalidMove = board.GetMoveValidity(new ChessSquare("e2"), new ChessSquare("e5"));

            board.Move(invalidMove);
        }
Ejemplo n.º 13
0
        public void RemovePieceAt_Should_ThrowArgumentNullException()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            board.RemovePieceAt(null);

            //	Assert
        }
Ejemplo n.º 14
0
        public void GetFEN_Should_Return_CorrectFenString(string expectedFEN)
        {
            //	Arrange
            SUT board;

            //	Act
            board = new SUT(expectedFEN);

            //	Assert
            Assert.AreEqual(expectedFEN, board.GetFEN());
        }
Ejemplo n.º 15
0
        public void Constructor_Should_InitializeWithInitialPosition()
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            string fen = board.GetFEN();

            //	Assert
            Assert.AreEqual(SUT.INITIAL_FEN_POSITION, fen);
        }
Ejemplo n.º 16
0
        public void GetLegalMoves_Should_ReturnAllLegalMovesForInitialPosition()
        {
            //	Arrange
            SUT board = new SUT();
            List <ChessMove> legalMoves;

            //	Act
            legalMoves = board.GetLegalMoves();

            //	Assert
            Assert.AreEqual(20, legalMoves.Count);
        }
Ejemplo n.º 17
0
        public void GetDisambiguator_Should_ReturnCorrectValue(string FENPosition, string from, string to, string expectedSAN)
        {
            //	Arrange
            SUT       board = new SUT(FENPosition);
            ChessMove actualMove;

            //	Act
            actualMove = board.GetMoveValidity(new ChessSquare(from), new ChessSquare(to));

            //	Assert
            Assert.AreEqual(expectedSAN, actualMove.ToSAN);
        }
Ejemplo n.º 18
0
        public void GetLegalMoves_Should_ReturnEmptyList()
        {
            //	Arrange
            SUT board = new SUT("8/4p3/p3k3/P2R1R2/4K1p1/6P1/8/8 b - - 0 1");
            List <ChessMove> legalMoves;

            //	Act
            legalMoves = board.GetLegalMoves();

            //	Assert
            Assert.AreEqual(0, legalMoves.Count);
        }
Ejemplo n.º 19
0
        public void LoadFEN_Should_InitializeTheBoard(string expectedFEN)
        {
            //	Arrange
            SUT board = new SUT();

            //	Act
            board.Clear();
            board.LoadFEN(expectedFEN);

            //	Assert
            Assert.AreEqual(expectedFEN, board.GetFEN());
        }
Ejemplo n.º 20
0
        public void Move_Should_ResetEnPassant(string fen, string nextMove)
        {
            //  Arrange
            var board = new SUT(fen);

            //  Act
            board.Move(board.SANToMove(nextMove));
            fen = board.GetFEN();

            //  Assert
            Assert.AreEqual("-", new FEN(fen).EnPassant);
        }
Ejemplo n.º 21
0
        public void PutPiece_Should_PutThePieceOnTheBoard(ChessPieceKind kindOfPiece, ChessColor pieceColor, string square)
        {
            //	Arrange
            SUT        board = new SUT();
            ChessPiece piece = new ChessPiece(kindOfPiece, pieceColor);

            //	Act
            board.PutPiece(piece, new ChessSquare(square));

            //	Assert
            Assert.IsTrue(piece == board.GetPieceAt(new ChessSquare(square)));
        }
Ejemplo n.º 22
0
        public void RemovePieceAt_Should_RemoveThePiece(string square, ChessPieceKind expectedPiece, ChessColor expectedColor)
        {
            //	Arrange
            SUT        board = new SUT();
            ChessPiece actualPiece;

            //	Act
            actualPiece = board.RemovePieceAt(new ChessSquare(square));

            //	Assert
            Assert.AreEqual(expectedPiece, actualPiece.Kind);
            Assert.AreEqual(expectedColor, actualPiece.Color);
        }
Ejemplo n.º 23
0
        public void Clear_Should_ResetTheState()
        {
            //	Arrange
            SUT board = new SUT("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPPKPPP/RNBQ1BNR b kq - 1 2");

            //	Act
            board.Clear();

            //	Assert
            Assert.AreEqual("8/8/8/8/8/8/8/8 w - - 0 1", board.GetFEN());
            Assert.AreEqual(ChessColor.White, board.Turn);
            Assert.AreEqual(ChessCastling.None, board.GetCastlingRights(ChessColor.White));
            Assert.AreEqual(ChessCastling.None, board.GetCastlingRights(ChessColor.Black));
        }
Ejemplo n.º 24
0
        public void Reset_Should_ResetAndSetupInitialPosition()
        {
            //	Arrange
            SUT board = new SUT("2rq1rk1/ppp2ppp/n3bn2/1B1pp1B1/1b1PP3/2N2N2/PPP1QPPP/2KRR3 w - - 0 1");

            //	Act
            board.Reset();

            //	Assert
            Assert.AreEqual(SUT.INITIAL_FEN_POSITION, board.GetFEN());
            Assert.AreEqual(ChessColor.White, board.Turn);
            Assert.AreEqual(ChessCastling.KingSide | ChessCastling.QueenSide, board.GetCastlingRights(ChessColor.White));
            Assert.AreEqual(ChessCastling.KingSide | ChessCastling.QueenSide, board.GetCastlingRights(ChessColor.Black));
        }
Ejemplo n.º 25
0
        public void GetCastlingRights_Should_ReturnCorrectValues(string fenString, ChessCastling expectedForWhite, ChessCastling expectedForBlack)
        {
            //	Arrange
            SUT           board;
            ChessCastling actualWhite;
            ChessCastling actualBlack;

            //	Act
            board       = new SUT(fenString);
            actualWhite = board.GetCastlingRights(ChessColor.White);
            actualBlack = board.GetCastlingRights(ChessColor.Black);

            //	Assert
            Assert.AreEqual(expectedForWhite, actualWhite);
            Assert.AreEqual(expectedForBlack, actualBlack);
        }
Ejemplo n.º 26
0
        public void Constructor_Should_InitializeWithGivenPosition(string expectedFen,
                                                                   string expectedSide,
                                                                   ChessCastling expectedCastlingForWhite,
                                                                   ChessCastling expectedCastlingForBlack)
        {
            //	Arrange
            SUT board = new SUT(expectedFen);

            //	Act
            string fen = board.GetFEN();

            //	Assert
            Assert.AreEqual(expectedFen, fen);
            Assert.AreEqual(expectedSide, board.Turn == ChessColor.White ? "w" : "b");
            Assert.AreEqual(expectedCastlingForWhite, board.GetCastlingRights(ChessColor.White));
            Assert.AreEqual(expectedCastlingForBlack, board.GetCastlingRights(ChessColor.Black));
        }
Ejemplo n.º 27
0
        public void GetMoveHistory_Should_Return_AllTheMoves()
        {
            //	Arrange
            SUT board = new SUT();
            List <ChessMove> playedMoves = new List <ChessMove>();

            ChessMove[]      moveHistory;
            int              moveCount  = 0;
            List <ChessMove> legalMoves = board.GetLegalMoves();

            System.Random randomizer = new System.Random(System.DateTime.Now.Second);

            //	Act
            do
            {
                var moveIndex = randomizer.Next(0, legalMoves.Count);
                board.Move(legalMoves[moveIndex]);
                playedMoves.Add(legalMoves[moveIndex]);

                legalMoves = board.GetLegalMoves();
                moveCount++;
            } while (legalMoves.Count > 0 && moveCount < 30);
            moveHistory = board.GetMoveHistory();

            //	Assert
            Assert.IsNotNull(moveHistory);
            Assert.AreEqual(playedMoves.Count, moveHistory.Length);
            for (int i = 0; i < moveHistory.Length; i++)
            {
                Assert.AreEqual(moveHistory[i].CapturedPiece, playedMoves[i].CapturedPiece);
                Assert.IsTrue(moveHistory[i].From == playedMoves[i].From);
                Assert.IsTrue(moveHistory[i].To == playedMoves[i].To);
                Assert.AreEqual(moveHistory[i].IsValid, playedMoves[i].IsValid);
                Assert.AreEqual(moveHistory[i].MoveKind, playedMoves[i].MoveKind);
                Assert.AreEqual(moveHistory[i].MovingPiece.Kind, playedMoves[i].MovingPiece.Kind);
                Assert.AreEqual(moveHistory[i].PromotedTo, playedMoves[i].PromotedTo);
                Assert.AreEqual(moveHistory[i].IllegalReason, playedMoves[i].IllegalReason);
            }
        }
Ejemplo n.º 28
0
        public void LegalMoves_Should_ReturnQueenSideCastle()
        {
            //	Arrange
            SUT board = new SUT("rnbqk2r/pppp1ppp/3b1n2/4p3/8/8/8/R3K3 w Qkq - 0 1");
            List <ChessMove> legalMoves;

            //	Act
            legalMoves = board.GetLegalMoves();

            //	Assert
            Assert.AreEqual(15, legalMoves.Count);
            bool found = false;

            foreach (ChessMove chessMove in legalMoves)
            {
                if (chessMove.MoveKind == ChessMoveType.QSide_Castle)
                {
                    found = true;
                }
            }
            Assert.IsTrue(found);
        }
Ejemplo n.º 29
0
        public void Ascii_Should_ReturnCorrectFormat()
        {
            //	Arrange
            SUT    board = new SUT();
            string actual;

            //	Act
            actual = board.Ascii();

            //	Assert
            Assert.AreEqual("   +------------------------+\n" +
                            " 8 | r  n  b  q  k  b  n  r |\n" +
                            " 7 | p  p  p  p  p  p  p  p |\n" +
                            " 6 | .  .  .  .  .  .  .  . |\n" +
                            " 5 | .  .  .  .  .  .  .  . |\n" +
                            " 4 | .  .  .  .  .  .  .  . |\n" +
                            " 3 | .  .  .  .  .  .  .  . |\n" +
                            " 2 | P  P  P  P  P  P  P  P |\n" +
                            " 1 | R  N  B  Q  K  B  N  R |\n" +
                            "   +------------------------+\n" +
                            "     a  b  c  d  e  f  g  h\n", actual);
        }
Ejemplo n.º 30
0
        public void GetPieceAt_Should_ReturnThePiece()
        {
            //	Arrange
            SUT board = new SUT();

            //	Assert
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Rook, ChessColor.White) == board.GetPieceAt(new ChessSquare("a1")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Knight, ChessColor.White) == board.GetPieceAt(new ChessSquare("b1")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Bishop, ChessColor.White) == board.GetPieceAt(new ChessSquare("c1")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Queen, ChessColor.White) == board.GetPieceAt(new ChessSquare("d1")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.King, ChessColor.White) == board.GetPieceAt(new ChessSquare("e1")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Bishop, ChessColor.White) == board.GetPieceAt(new ChessSquare("f1")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Knight, ChessColor.White) == board.GetPieceAt(new ChessSquare("g1")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Rook, ChessColor.White) == board.GetPieceAt(new ChessSquare("h1")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Rook, ChessColor.Black) == board.GetPieceAt(new ChessSquare("a8")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Knight, ChessColor.Black) == board.GetPieceAt(new ChessSquare("b8")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Bishop, ChessColor.Black) == board.GetPieceAt(new ChessSquare("c8")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Queen, ChessColor.Black) == board.GetPieceAt(new ChessSquare("d8")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.King, ChessColor.Black) == board.GetPieceAt(new ChessSquare("e8")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Bishop, ChessColor.Black) == board.GetPieceAt(new ChessSquare("f8")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Knight, ChessColor.Black) == board.GetPieceAt(new ChessSquare("g8")));
            Assert.IsTrue(new ChessPiece(ChessPieceKind.Rook, ChessColor.Black) == board.GetPieceAt(new ChessSquare("h8")));
        }