Пример #1
0
        public void King_AllowsCastling_WhenSpacesOpen(string input, string kingSquare, string expectedCastlingSquare)
        {
            var king           = MoveParser.ParseSquare(kingSquare);
            var expectedResult = MoveParser.ParseSquare(expectedCastlingSquare);
            IEnumerable <Square> validMoves;

            var game  = new Game();
            var moves = input.Split(',');

            foreach (var move in moves.SkipLast(2))
            {
                game.Move(move);

                validMoves = game.GetValidMoves(king.File, king.Rank);
                Assert.That(validMoves, Does.Not.Contain(expectedResult), $"Unexpected castling square after move {move}");
            }

            foreach (var move in moves.TakeLast(2))
            {
                game.Move(move);
            }

            validMoves = game.GetValidMoves(king.File, king.Rank);
            Assert.That(validMoves, Does.Contain(expectedResult));
        }
Пример #2
0
        public void Deserialize_ShouldSetBoardState_WhenValidStringGiven()
        {
            const string  FenString = "pQ4N1/3k3R/1r4n1/KbBbBppP/8/8/q7/7n";
            FenSerializer ser       = new FenSerializer();
            BoardState    actual;
            BoardState    expected = BoardState.Empty
                                     .SetPiece(MoveParser.ParseSquare("a8"), SquareContents.Black | SquareContents.Pawn)
                                     .SetPiece(MoveParser.ParseSquare("b8"), SquareContents.White | SquareContents.Queen)
                                     .SetPiece(MoveParser.ParseSquare("g8"), SquareContents.White | SquareContents.Knight)

                                     .SetPiece(MoveParser.ParseSquare("d7"), SquareContents.Black | SquareContents.King)
                                     .SetPiece(MoveParser.ParseSquare("h7"), SquareContents.White | SquareContents.Rook)

                                     .SetPiece(MoveParser.ParseSquare("b6"), SquareContents.Black | SquareContents.King)
                                     .SetPiece(MoveParser.ParseSquare("g6"), SquareContents.Black | SquareContents.Knight)

                                     .SetPiece(MoveParser.ParseSquare("a5"), SquareContents.White | SquareContents.King)
                                     .SetPiece(MoveParser.ParseSquare("b5"), SquareContents.Black | SquareContents.Bishop)
                                     .SetPiece(MoveParser.ParseSquare("c5"), SquareContents.White | SquareContents.Bishop)
                                     .SetPiece(MoveParser.ParseSquare("d5"), SquareContents.Black | SquareContents.Bishop)
                                     .SetPiece(MoveParser.ParseSquare("e5"), SquareContents.White | SquareContents.Bishop)
                                     .SetPiece(MoveParser.ParseSquare("f5"), SquareContents.Black | SquareContents.Pawn)
                                     .SetPiece(MoveParser.ParseSquare("g5"), SquareContents.Black | SquareContents.Pawn)
                                     .SetPiece(MoveParser.ParseSquare("h5"), SquareContents.White | SquareContents.Pawn)

                                     .SetPiece(MoveParser.ParseSquare("a2"), SquareContents.Black | SquareContents.Queen)
                                     .SetPiece(MoveParser.ParseSquare("h1"), SquareContents.Black | SquareContents.Knight);

            // Act
            actual = ser.Deserialize(FenString);

            Assert.That(expected.AllPieces, Is.EqualTo(actual.AllPieces));
            Assert.That(expected.WhitePieces, Is.EqualTo(actual.WhitePieces));
            Assert.That(expected.BlackPieces, Is.EqualTo(actual.BlackPieces));
        }
Пример #3
0
        public void GeneratesExpectedSquares_MatchesCount(string sq, SquareContents piece, int expectedSquares)
        {
            var   square    = MoveParser.ParseSquare(sq);
            ulong bitSquare = BitTranslator.TranslateToBit(square.File, square.Rank);
            var   board     = BoardState.Empty.SetPiece(bitSquare, piece);
            var   game      = new Game(board);

            var validMoves = game.GetValidMoves(square.File, square.Rank);

            Assert.That(validMoves.Count, Is.EqualTo(expectedSquares));
        }
Пример #4
0
        public void TryParseMove_ParsesPromotion(string input, string expectedStart, string expectedEnd, PieceColor color, SquareContents piece)
        {
            var endSq      = MoveParser.ParseSquare(expectedEnd);
            var startSq    = MoveParser.ParseSquare(expectedStart);
            var pieceColor = color == PieceColor.White ? SquareContents.White : SquareContents.Black;
            var board      = BoardState.Empty.SetPiece(startSq, SquareContents.Pawn | pieceColor);

            var game           = new Game(board, color);
            var result         = game.Move(input);
            var expectedSquare = game.GetSquareContents(endSq.File, endSq.Rank);

            Assert.That(result, Is.EqualTo(ErrorCondition.None));
            Assert.That(expectedSquare, Is.EqualTo(piece));
        }
Пример #5
0
        public void GeneratesExpectedSquares_OmitsUnexpected(string sq, SquareContents piece, string expectedSquares)
        {
            var expected = expectedSquares.Split(',').Select(MoveParser.ParseSquare).ToArray();

            var   square    = MoveParser.ParseSquare(sq);
            ulong bitSquare = BitTranslator.TranslateToBit(square.File, square.Rank);
            var   board     = BoardState.Empty.SetPiece(bitSquare, piece);

            var game = new Game(board);

            var validMoves = game.GetValidMoves(square.File, square.Rank).ToArray();

            Assert.That(validMoves, Does.Not.Contain(expected[0]));
        }
Пример #6
0
        public void GetValidMoves_DetectsWhenOnEdge(string position, string targetSq, string expectedSq)
        {
            var fen   = new FenSerializer();
            var board = fen.Deserialize(position);

            var target     = MoveParser.ParseSquare(targetSq);
            var targetMask = BitTranslator.TranslateToBit(target.File, target.Rank);

            var validMovesMask = MoveGenerator.GetBishopMovements(targetMask, board);
            var validMoves     = BitTranslator.TranslateToSquares(validMovesMask);

            var expected = MoveParser.ParseSquare(expectedSq);

            Assert.That(validMoves, Does.Contain(expected));
        }
Пример #7
0
        public void PlacesPieceOnIntendedSquare(string focusSquare, SquareContents expectedContents, params string[] moveStrings)
        {
            var game = new Game();

            foreach (var moveString in moveStrings)
            {
                var result = game.Move(moveString);
                Assert.That(result, Is.EqualTo(ErrorCondition.None), "Failed for move: " + moveString);
            }

            var targetSquare   = MoveParser.ParseSquare(focusSquare);
            var actualContents = game.GetSquareContents(targetSquare.File, targetSquare.Rank);

            Assert.That(actualContents, Is.EqualTo(expectedContents));
        }
Пример #8
0
        public void Move_UpdatesPiecesWhenCastling(string input, string kingSquare, string rookSquare, SquareContents color)
        {
            var king = MoveParser.ParseSquare(kingSquare);
            var rook = MoveParser.ParseSquare(rookSquare);

            var game = new Game();

            foreach (var move in input.Split(','))
            {
                game.Move(move);
            }

            var expectedKing = game.GetSquareContents(king.File, king.Rank);
            var expectedRook = game.GetSquareContents(rook.File, rook.Rank);

            Assert.That(expectedKing, Is.EqualTo(SquareContents.King | color));
            Assert.That(expectedRook, Is.EqualTo(SquareContents.Rook | color));
        }
Пример #9
0
        public void King_DisallowsCastling_WhenInvalidState(string input, string kingSquare, string expectedCastlingSquare)
        {
            var king           = MoveParser.ParseSquare(kingSquare);
            var expectedResult = MoveParser.ParseSquare(expectedCastlingSquare);

            var game  = new Game();
            var moves = input.Split(',');

            foreach (var move in moves.SkipLast(1))
            {
                game.Move(move);
            }

            game.Move(moves.Last());
            Assert.That(game.AttackState, Is.Not.EqualTo(AttackState.Check));

            var validMoves = game.GetValidMoves(king.File, king.Rank);

            Assert.That(validMoves, Does.Not.Contain(expectedResult));
        }