public void Should_find_promotions(string sanText, string expectedMoveText, Colours toPlay, ChessPieceName promotionPiece)
        {
            var builder = new ChessBoardBuilder()
                          .Board("    k   " +
                                 "       P" +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "    K   "
                                 );

            var game = ChessFactory.CustomChessGame(builder.ToGameSetup(), toPlay);
            var move = new SanMoveFinder(game.BoardState)
                       .Find(sanText.ToSan(), toPlay);

            Assert.NotNull(move, "No move found!");

            Assert.That(move.ToChessCoords(), Is.EqualTo(expectedMoveText));

            var moveExtraData = move.ExtraData;

            Assert.NotNull(moveExtraData);
            var data = ChessFactory.MoveExtraData(toPlay, promotionPiece);

            Assert.That(move.ToChessCoords(), Is.EqualTo(expectedMoveText));
            Assert.That(data, Is.EqualTo(moveExtraData), $"Unexpected promotion: {moveExtraData}");
        }
        public void Regression_king_side_castle_bug()
        {
            var board = new ChessBoardBuilder()
                        .Board(".rbqkbnr" +
                               "pppppppp" +
                               "n......." +
                               "........" +
                               "........" +
                               ".....NPB" +
                               "PPPPPP.P" +
                               "RNBQK..R"
                               );

            var buildGame  = ChessFactory.CustomChessGame(board.ToGameSetup(), Colours.White);
            var boardState = buildGame.BoardState;

            var msg = buildGame.Move(_whiteKingSideCastle.ToChessCoords());

            Assert.IsEmpty(msg, msg);

            Assert.False(boardState.IsEmpty("G1".ToBoardLocation()), $"No item at G1");
            var king = boardState.GetItem("G1".ToBoardLocation());

            Assert.That(king.Item.EntityType, Is.EqualTo((int)ChessPieceName.King), "king not moved correctly");

            Assert.NotNull(boardState.GetItem("F1".ToBoardLocation()), $"No item at F1");
            var rook = boardState.GetItem("F1".ToBoardLocation());

            Assert.That(rook.Item.EntityType, Is.EqualTo((int)ChessPieceName.Rook), "castle not moved correctly");
        }
Beispiel #3
0
        public void Spike_debugging_a_move_problem()
        {
            var board = new ChessBoardBuilder()
                        //                .X(8, "rnbqkbnr")
                        //                .Y(ChessFile.A, "RP    pr")
                        //                .At(ChessFile.D, 2, 'P')
                        //                .FromChessGame(new ChessGame())
                        .Board("r.bq.rk." +
                               "...nbppp" +
                               "p.pp.n.." +
                               ".p..p..." +
                               "..PPP..." +
                               ".B...N.P" +
                               "PP...PP." +
                               "RNBQR.K.")
            ;

            var game = ChessFactory.CustomChessGame(board.ToGameSetup(), Colours.White);
//            var game = new ChessGame(NullLogger<ChessGame>.Instance, _chessBoardEngineProvider, board.ToGameSetup(), Colours.White);

            // TODO: Fix this bug, pawn can't take pawn, something to do with
            // enpassant i guess based on pawn positions

            var msg = game.Move("c4b5");

            msg.ShouldBeEmpty();
        }
        public void Should_find_simple_check_condition()
        {
            var builder = new ChessBoardBuilder()
                          .Board("R   k   " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "    K  R"
                                 );
            var game = ChessFactory.CustomChessGame(builder.ToGameSetup(), Colours.Black);

            Assert.That(game.CheckState, Is.EqualTo(GameCheckState.BlackInCheck));
        }
Beispiel #5
0
        public void SetUp()
        {
            var board = new ChessBoardBuilder()
                        .Board("   qk  r" +
                               "        " +
                               "Pe Pb PP" +
                               "        " +
                               "        " +
                               "        " +
                               "        " +
                               "    K  R"
                               );

            _boardState = ChessFactory.CustomChessGame(board.ToGameSetup()).BoardState;
            _validator  = new EnPassantTakeValidator(new ChessValidationSteps());
        }
        public void ValidateMove_fails_if_path_under_attack()
        {
            var board = new ChessBoardBuilder()
                        .Board("    k   " +
                               "        " +
                               "        " +
                               "        " +
                               "     r  " +
                               "        " +
                               "        " +
                               "    K  R"
                               );

            var boardState = ChessFactory.CustomChessGame(board.ToGameSetup(), Colours.White).BoardState;

            Assert.False(_validator.ValidateMove(_whiteKingSideCastle, boardState), "Invalid king side castle move allowed");
        }
        public void ValidateMove_fails_unless_king_is_in_starting_position()
        {
            var board = new ChessBoardBuilder()
                        .Board("    k   " +
                               "        " +
                               "        " +
                               "        " +
                               "        " +
                               "        " +
                               "        " +
                               "   K   R"
                               );

            var boardState = ChessFactory.CustomChessGame(board.ToGameSetup(), Colours.White).BoardState;

            Assert.False(_validator.ValidateMove(_whiteInvalidKingCastle, boardState), "Invalid castle move allowed");
        }
        public void Should_find_castle_moves(string sanText, string expectedMoveText, Colours toPlay)
        {
            var builder = new ChessBoardBuilder()
                          .Board("r   k  r" +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "        " +
                                 "R   K  R"
                                 );

            var game = ChessFactory.CustomChessGame(builder.ToGameSetup(), toPlay);
            var move = new SanMoveFinder(game.BoardState)
                       .Find(sanText.ToSan(), toPlay);

            Assert.NotNull(move, "No move found!");
            Assert.That(move.ToChessCoords(), Is.EqualTo(expectedMoveText));
        }
        public void Should_not_find_move_that_leaves_king_in_check()
        {
            var board = new ChessBoardBuilder()
                        .Board("    k   " +
                               "        " +
                               "        " +
                               "    p   " +
                               "   PQ   " +
                               "        " +
                               "        " +
                               "    K   "
                               );
            var game = ChessFactory.CustomChessGame(board.ToGameSetup(), Colours.Black);

            var blockedPieceLocation = "E5".ToBoardLocation();

            var blockedPiece = game.BoardState.GetItem(blockedPieceLocation);

            Assert.False(blockedPiece.Paths.ContainsMoveTo("D4".ToBoardLocation()),
                         $"Pawn at E5 should NOT be able to move D4");
        }
        public void Should_put_plus_on_end_of_moves_that_cause_check()
        {
            // TODO: Better way to check this, than using a full board.
            var builder = new ChessBoardBuilder()
                          .Board("....rrk." +
                                 ".b...pp." +
                                 ".n...q.p" +
                                 "..p.N..." +
                                 ".pB....." +
                                 ".......P" +
                                 "PP...PP." +
                                 "R..QR.K."
                                 );

            var game      = ChessFactory.CustomChessGame(builder.ToGameSetup());
            var from      = "C4".ToBoardLocation();
            var piece     = game.BoardState.GetItem(from);
            var boardMove = piece.Paths.FindMove(from, "f7".ToBoardLocation());
            var san       = StandardAlgebraicNotation.ParseFromGameMove(game.BoardState, boardMove, true);

            san.ToNotation().ShouldBe("Bxf7+");
        }
        public void Regression_Kings_cant_move_next_to_each_other_black()
        {
            var board = new ChessBoardBuilder()
                        .Board("  K k   " +
                               "        " +
                               "        " +
                               "        " +
                               "        " +
                               "        " +
                               "        " +
                               "        "
                               );

            var boardState = ChessFactory.CustomChessGame(board.ToGameSetup(), Colours.Black).BoardState;

            var from = "e8".ToBoardLocation();
            var to   = "d8".ToBoardLocation();
            var move = BoardMove.Create(from, to, (int)ChessMoveTypes.KingMove);
            var item = boardState.GetItem(from);

            Assert.False(item.Paths.ContainsMoveTo(to), "kings cannot move next to each other");
        }