Beispiel #1
0
    def test_moves_in_check(self):
        g = castle.Game(PlayerType.HUMAN, PlayerType.HUMAN)
        g.board.clear()
        g.board.place_piece(Piece(PieceType.KING, Color.WHITE), 'h1')
        g.board.place_piece(Piece(PieceType.PAWN, Color.WHITE), 'f1')
        g.board.place_piece(Piece(PieceType.QUEEN, Color.BLACK), 'g2')
        g.board.place_piece(Piece(PieceType.BISHOP, Color.BLACK), 'f3')
        g.board.place_piece(Piece(PieceType.KNIGHT, Color.BLACK), 'g4')
        g.board.place_piece(Piece(PieceType.BISHOP, Color.BLACK), 'e3')

        # only way out of check is for pawn to capture queen
        legal_moves = g.get_all_legal_moves(Color.WHITE)
        self.assertEqual(len(legal_moves), 1)
        self.assertEqual(MoveParser.parse_move(g, 'fg2'), legal_moves.pop())

        # capture the queen
        g.apply_notation('fg2')
        # black captures pawn with bishop
        g.apply_notation('Bxg2')

        # now, white king is in check again by threat of black's g2 bishop
        # only move is to capture bishop
        legal_moves = g.get_all_legal_moves(Color.WHITE)
        self.assertEqual(len(legal_moves), 1)
        self.assertEqual(MoveParser.parse_move(g, 'Kxg2'), legal_moves.pop())
Beispiel #2
0
    def test_white_kingside_castle(self):
        g = castle.Game(PlayerType.HUMAN, PlayerType.HUMAN)

        # can't castle while obstructed
        self.assertFalse(g.can_castle(Color.WHITE, True))
        self.assertFalse(g.can_castle(Color.WHITE, False))

        g.apply_notation('e4')
        g.apply_notation('e5')
        g.apply_notation('Nf3')
        g.apply_notation('a5')
        g.apply_notation('Be2')
        g.apply_notation('b5')
        # white is now ready to kingside castle
        g.apply_notation('O-O')

        castled_king_square = g.board.square_from_notation('g1')
        self.assertIsNotNone(castled_king_square.occupant)
        self.assertTrue(castled_king_square.occupant.color == Color.WHITE)
        self.assertTrue(castled_king_square.occupant.type == PieceType.KING)

        castled_rook_square = g.board.square_from_notation('f1')
        self.assertIsNotNone(castled_rook_square.occupant)
        self.assertTrue(castled_rook_square.occupant.color == Color.WHITE)
        self.assertTrue(castled_rook_square.occupant.type == PieceType.ROOK)

        # can't castle twice
        self.assertFalse(g.can_castle(Color.WHITE, True))
        self.assertFalse(g.can_castle(Color.WHITE, False))
Beispiel #3
0
 def test_pawn_capture(self):
     g = castle.Game(PlayerType.HUMAN, PlayerType.HUMAN)
     g.apply_notation('e4')
     g.apply_notation('f5')
     # TODO(PT): once MoveParser returns a Move object, this should verify a capture was indicated
     g.apply_notation('exf5')
     self.check_contains_piece(g, 'f5', PieceType.PAWN, Color.WHITE)
Beispiel #4
0
 def test_bishop_capture(self):
     g = castle.Game(PlayerType.HUMAN, PlayerType.HUMAN)
     g.apply_notation('e4')
     g.apply_notation('e5')
     g.apply_notation('d4')
     g.apply_notation('a6')
     g.apply_notation('Bxa6')
     self.check_contains_piece(g, 'a6', PieceType.BISHOP, Color.WHITE)
Beispiel #5
0
    def test_ambiguous_knight_movement(self):
        g = castle.Game(PlayerType.HUMAN, PlayerType.HUMAN)
        g.apply_notation('e4')
        g.apply_notation('e5')
        g.apply_notation('Ne2')
        g.apply_notation('f6')
        with self.assertRaises(InvalidChessNotationError):
            g.apply_notation('Nc3')

        g.apply_notation('Nec3')
        self.check_contains_piece(g, 'c3', PieceType.KNIGHT, Color.WHITE)
Beispiel #6
0
    def test_simple(self):
        g = castle.Game(PlayerType.HUMAN, PlayerType.HUMAN)
        move = castle.MoveParser.parse_move(g, 'e4')
        self.assertEqual('e2', move.from_square.notation())
        self.assertEqual('e4', move.to_square.notation())

        # 1. e4 e5 2. Bd3
        g.apply_notation('e4')
        g.apply_notation('e5')
        move = castle.MoveParser.parse_move(g, 'Bd3')
        self.assertEqual('f1', move.from_square.notation())
        self.assertEqual('d3', move.to_square.notation())
Beispiel #7
0
def test_perft2():
    g = castle.Game(castle.PlayerType.HUMAN, castle.PlayerType.HUMAN)
    g.board.clear()
    # https://sites.google.com/site/numptychess/perft/position-3
    g.board.place_piece(
        castle.Piece(castle.PieceType.ROOK, castle.Color.BLACK), 'a8')
    g.board.place_piece(
        castle.Piece(castle.PieceType.KING, castle.Color.BLACK), 'e8')
    g.board.place_piece(
        castle.Piece(castle.PieceType.ROOK, castle.Color.BLACK), 'h8')
    g.board.place_piece(
        castle.Piece(castle.PieceType.PAWN, castle.Color.BLACK), 'a7')
    g.board.place_piece(
        castle.Piece(castle.PieceType.PAWN, castle.Color.BLACK), 'h7')
    g.board.place_piece(
        castle.Piece(castle.PieceType.BISHOP, castle.Color.WHITE), 'a5')
    g.board.place_piece(
        castle.Piece(castle.PieceType.PAWN, castle.Color.BLACK), 'b4')
    g.board.place_piece(
        castle.Piece(castle.PieceType.PAWN, castle.Color.BLACK), 'c4')
    g.board.place_piece(
        castle.Piece(castle.PieceType.PAWN, castle.Color.BLACK), 'e4')
    g.board.place_piece(
        castle.Piece(castle.PieceType.BISHOP, castle.Color.BLACK), 'd3')
    g.board.place_piece(
        castle.Piece(castle.PieceType.PAWN, castle.Color.WHITE), 'a2')
    g.board.place_piece(
        castle.Piece(castle.PieceType.PAWN, castle.Color.WHITE), 'h2')
    g.board.place_piece(
        castle.Piece(castle.PieceType.ROOK, castle.Color.WHITE), 'a1')
    g.board.place_piece(
        castle.Piece(castle.PieceType.KING, castle.Color.WHITE), 'e1')
    g.board.place_piece(
        castle.Piece(castle.PieceType.ROOK, castle.Color.WHITE), 'h1')
    g.board.pretty_print()
    for i in range(2):
        print(f'perft({i}) = {g.perft(i)}')
Beispiel #8
0
def test_perft():
    g = castle.Game(castle.PlayerType.HUMAN, castle.PlayerType.HUMAN)
    g.board.pretty_print()
    for i in range(10):
        print(f'perft({i}) = {g.perft(i)}')
Beispiel #9
0
def play_game():
    player1, player2 = select_player_types()
    g = castle.Game(player1, player2)
    play_constructed_game(g)
Beispiel #10
0
 def test_knight_movement(self):
     g = castle.Game(PlayerType.HUMAN, PlayerType.HUMAN)
     g.apply_notation('e4')
     g.apply_notation('e5')
     g.apply_notation('Ne2')
     self.check_contains_piece(g, 'e2', PieceType.KNIGHT, Color.WHITE)