Ejemplo n.º 1
0
def get_enpassant_board():
    __ = BlankPiece()
    p1 = Pawn("black")
    p2 = Pawn("black")
    wp = Pawn("white")
    wp.has_never_moved = False

    #             0   1   2   3   4   5   6   7
    board = [
                [__, __, __, __, __, __, __, __],  # 0
                [__, __, __, __, __, p1, __, p2],  # 1
                [__, __, __, __, __, __, __, __],  # 2
                [__, __, __, __, __, __, wp, __],  # 3
                [__, __, __, __, __, __, __, __],  # 4
                [__, __, __, __, __, __, __, __],  # 5
                [__, __, __, __, __, __, __, __],  # 6
                [__, __, __, __, __, __, __, __]  # 7
            ]

    assert_length(board[3][6].get_attack_set(board, [3, 6], []), 0)
    assert_contains(
        board[3][6].get_move_set(board, [3, 6], []),
        [
            Move(MoveType.NORMAL, [3, 6], [2, 6])
        ]
    )
    return board
Ejemplo n.º 2
0
def test_pawn_enpassant():
    # Advance pawn by two spaces
    conducted_move_history = []
    board = get_enpassant_board()
    board, move_history_element = conduct_move(
        board, Move(MoveType.NORMAL, [1, 5], [3, 5]), "black")
    conducted_move_history.append(move_history_element)

    # Assert white pawn can only do regular attack, one space advance AND enpassant
    en_passant_move = Move.en_passant([3, 6], [2, 5])
    assert_contains(
        board[3][6].get_attack_set(board, [3, 6], conducted_move_history),
        [en_passant_move])

    # Actually execute en passant move
    board, move_history_element = conduct_move(board, en_passant_move, "white")
    conducted_move_history.append(move_history_element)

    __ = BlankPiece()
    p2 = Pawn("black")
    wp = Pawn("white")
    #             0   1   2   3   4   5   6   7
    expected_board = [
        [__, __, __, __, __, __, __, __],  # 0
        [__, __, __, __, __, __, __, p2],  # 1
        [__, __, __, __, __, wp, __, __],  # 2
        [__, __, __, __, __, __, __, __],  # 3
        [__, __, __, __, __, __, __, __],  # 4
        [__, __, __, __, __, __, __, __],  # 5
        [__, __, __, __, __, __, __, __],  # 6
        [__, __, __, __, __, __, __, __]  # 7
    ]
    assert_row_contain_same_type_elements(expected_board[1], board[1])
    assert_row_contain_same_type_elements(expected_board[2], board[2])
    assert_row_contain_same_type_elements(expected_board[3], board[3])
Ejemplo n.º 3
0
def test_white_pawn_movements():
    __ = BlankPiece()

    p1 = Pawn("white")
    p2 = Pawn("white")
    p3 = Pawn("white")
    p3.has_never_moved = False

    # Enemy rook
    rr = Rook("black")

    #             0   1   2   3   4   5   6   7
    board = [
                [__, __, __, __, __, __, __, __],  # 0
                [__, __, __, __, __, __, __, __],  # 1
                [__, __, __, __, __, __, __, __],  # 2
                [__, __, __, __, __, __, rr, rr],  # 3
                [__, __, __, __, __, __, __, p3],  # 4
                [__, __, __, __, rr, __, __, __],  # 5
                [p1, __, __, __, __, p2, __, __],  # 6
                [__, __, __, __, __, __, __, __]  # 7
            ]
    # Left-most pawn
    assert_length(board[6][0].get_attack_set(board, [6, 0], []), 0)
    assert_contains(board[6][0].get_move_set(board, [6, 0], []),
                    create_list_of_moves(MoveType.NORMAL, [6, 0], [[5, 0], [4, 0]]))

    assert_contains(board[6][5].get_attack_set(board, [6, 5], []),
                    create_list_of_moves(MoveType.NORMAL, [6, 5], [[5, 4]]))
    assert_contains(board[6][5].get_move_set(board, [6, 5], []),
                    create_list_of_moves(MoveType.NORMAL, [6, 5], [[5, 5], [4, 5]]))

    assert_contains(board[4][7].get_attack_set(board, [4, 7], []), create_list_of_moves(MoveType.NORMAL, [4, 7], [[3, 6]]))
    assert_length(board[4][7].get_move_set(board, [4, 7], []), 0)
Ejemplo n.º 4
0
def test_white_pawn_promotion():
    __ = BlankPiece()

    p1 = Pawn("white")
    p2 = Pawn("white")

    # Enemy rook
    rr = Rook("black")

    #             0   1   2   3   4   5   6   7
    board = [
        [__, __, __, __, __, rr, __, __],  # 0
        [__, __, p1, __, __, p2, __, __],  # 1
        [__, __, __, __, __, __, __, __],  # 2
        [__, __, __, __, __, __, __, __],  # 3
        [__, __, __, __, __, __, __, __],  # 4
        [__, __, __, __, __, __, __, __],  # 5
        [__, __, __, __, __, __, __, __],  # 6
        [__, __, __, __, __, __, __, __]  # 7
    ]
    # Left-most pawn
    assert_length(board[1][2].get_attack_set(board, [1, 2], []), 0)
    assert_contains(board[1][2].get_move_set(board, [1, 2], []), [
        Move.pawn_promotion([1, 2], [0, 2], Queen("white")),
        Move.pawn_promotion([1, 2], [0, 2], Bishop("white")),
        Move.pawn_promotion([1, 2], [0, 2], Knight("white")),
        Move.pawn_promotion([1, 2], [0, 2], Rook("white"))
    ])
    assert_length(board[1][5].get_attack_set(board, [1, 5], []), 0)
    assert_length(board[1][5].get_move_set(board, [1, 5], []), 0)
Ejemplo n.º 5
0
def test_serialization():
    __ = BlankPiece()

    wp = Pawn("white")
    bp = Pawn("black")
    br = Rook("black")
    bh = Knight("black")
    bb = Bishop("black")
    bq = Queen("black")
    bk = King("black")

    #             0   1   2   3   4   5   6   7
    board = [
                [br, bh, bb, bq, bk, __, __, __],  # 0
                [bp, __, __, __, __, __, __, __],  # 1
                [__, __, __, __, __, __, __, __],  # 2
                [__, __, __, __, __, __, __, __],  # 3
                [__, __, __, __, __, __, __, __],  # 4
                [__, __, __, __, __, __, __, __],  # 5
                [wp, __, __, __, __, __, __, __],  # 6
                [__, __, __, __, __, __, __, __]   # 7
            ]

    serializable_gameboard = convert_to_string_gameboard(board)

    expected_gameboard = [
        ['br', 'bh', 'bb', 'bq', 'bk', '_', '_', '_'],
        ['bp', '_', '_', '_', '_', '_', '_', '_'],
        ['_', '_', '_', '_', '_', '_', '_', '_'],
        ['_', '_', '_', '_', '_', '_', '_', '_'],
        ['_', '_', '_', '_', '_', '_', '_', '_'],
        ['_', '_', '_', '_', '_', '_', '_', '_'],
        ['wp', '_', '_', '_', '_', '_', '_', '_'],
        ['_', '_', '_', '_', '_', '_', '_', '_']
    ]

    assert_length(serializable_gameboard, 8)
    assert_length(serializable_gameboard[3], 8)
    if expected_gameboard != serializable_gameboard:
        raise AssertionError("Expected gameboard" + str(expected_gameboard) + " but was " + str(serializable_gameboard))

    new_board = convert_from_string_gameboard(expected_gameboard)
    for i in range(0, len(board)):
        for j in range(0, len(board[0])):
            expected_piece = board[i][j]
            actual_piece = new_board[i][j]
            if expected_piece.type != actual_piece.type:
                raise AssertionError("Expected piece type at " + str(i) + ", " + str(j) + " was " + expected_piece.type
                                     + ", but got " + actual_piece.type)
            if expected_piece.col != actual_piece.col:
                raise AssertionError("Expected piece col at " + str(i) + ", " + str(
                    j) + " was " + expected_piece.col + " but got " + actual_piece.col)
Ejemplo n.º 6
0
def test_damiano_mate():
    """"""
    game = Game([
        [Empty() for _ in range(5)] +
        [Tower('black'), King('black'), Empty()],
        [Empty()
         for _ in range(6)] + [Pawn('black'), Queen('white')],
        [Empty() for _ in range(6)] + [Pawn('white'), Empty()],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(7)] + [King('white')],
    ])
    assert game.is_check('black'), print(game)
    assert game.is_checkmate('black'), print(game)
Ejemplo n.º 7
0
def test_back_rank_mate():
    """"""
    game = Game([
        [Empty() for _ in range(3)] +
        [Tower('white'),
         Empty(), Empty(),
         King('black'),
         Empty()],
        [Empty() for _ in range(5)] +
        [Pawn('black'), Pawn('black'),
         Pawn('black')],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
    ])
    assert game.is_check('black'), print(game)
    assert game.is_checkmate('black'), print(game)
Ejemplo n.º 8
0
def test_check():
    # Opposite col test omitted as it requires an alternate implementation *and* player color doesn't effect this test.

    _ = BlankPiece()

    k = King("white")
    k.has_never_moved = False

    p = Pawn("black")
    p.has_never_moved = False

    #            0  1  2  3  4  5  6  7
    board = [
        [_, _, _, _, _, _, _, _],  # 0
        [_, _, _, _, _, _, _, _],  # 1
        [_, _, _, _, _, p, _, _],  # 2
        [_, _, _, _, k, _, _, _],  # 3
        [_, _, _, _, _, _, _, _],  # 4
        [_, _, _, _, _, _, _, _],  # 5
        [_, _, _, _, _, _, _, _],  # 6
        [_, _, _, _, _, _, _, _]  # 7
    ]
    assert_true("King should be checked", is_being_checked(board, "white"))

    # Enemy rook
    r = Rook("black")
    q = Queen("black")

    #            0  1  2  3  4  5  6  7
    board = [
        [_, _, _, _, _, _, _, _],  # 0
        [_, _, _, _, _, _, _, _],  # 1
        [_, _, _, _, _, _, _, _],  # 2
        [_, _, _, _, _, _, _, _],  # 3
        [_, _, _, _, _, _, _, _],  # 4
        [_, _, _, _, _, _, _, _],  # 5
        [_, _, _, _, _, _, r, _],  # 6
        [_, _, k, _, _, _, _, q]  # 7
    ]
    assert_false("Should be checkmate",
                 can_player_leave_check_state(board, "white", []))
Ejemplo n.º 9
0
    def standard_configuration(cls):
        board = cls()

        padding = 2
        main_rank = seq(Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook)
        board[Rank.ONE][padding:-padding] = main_rank.map(lambda kls: kls(Color.WHITE)).list()
        board[Rank.EIGHT][padding:-padding] = main_rank.map(lambda kls: kls(Color.BLACK)).list()
        board[Rank.TWO][padding:-padding] = main_rank.map(lambda _: Pawn(Color.WHITE)).list()
        board[Rank.SEVEN][padding:-padding] = main_rank.map(lambda _: Pawn(Color.BLACK)).list()

        board.kings = {
            Color.BLACK: Position(Rank.EIGHT, File.E),
            Color.WHITE: Position(Rank.ONE, File.E),
        }

        board.player, board.enemy = Color.WHITE, Color.BLACK

        board.castling_perms[Color.WHITE] = CastlingPerm.ALL
        board.castling_perms[Color.BLACK] = CastlingPerm.ALL

        return board
Ejemplo n.º 10
0
def test_pieces_can_capture_opponent_pieces():
    board = Board(initial_pieces={
        'a8': King('black'),
        'e5': Pawn('black'),
        'f3': Knight('white')
    })
    assert board.pieces_quantity() == 3

    knight = board.get_piece('f3')
    board.move('f3', 'e5')
    assert board.get_piece('e5') is knight
    assert board.pieces_quantity() == 2
Ejemplo n.º 11
0
def test_fail_castling_when_king_already_moved():
    board = Board(initial_pieces={
        'a8': King('black'),
        'a7': Pawn('black'),
        'e1': King('white'),
        'h1': Rook('white')})
    board.move('e1', 'f1')  # king moves
    board.move('a7', 'a6')  # pawn moves
    board.move('f1', 'e1')  # king moves back
    board.move('a6', 'a5')  # pawn moves again
    with pytest.raises(ImpossibleMove):
        board.move('e1', 'g1')
Ejemplo n.º 12
0
def test_check():
    # Opposite col test omitted as it requires an alternate implementation *and* player color doesn't effect this test.

    _ = BlankPiece()

    k = King("white")
    k.has_never_moved = False

    p = Pawn("black")
    p.has_never_moved = False

    #            0  1  2  3  4  5  6  7
    board = [
        [_, _, _, _, _, _, _, _],  # 0
        [_, _, _, _, _, _, _, _],  # 1
        [_, _, _, _, _, p, _, _],  # 2
        [_, _, _, _, k, _, _, _],  # 3
        [_, _, _, _, _, _, _, _],  # 4
        [_, _, _, _, _, _, _, _],  # 5
        [_, _, _, _, _, _, _, _],  # 6
        [_, _, _, _, _, _, _, _]  # 7
    ]
    assert_true("King should be checked", is_being_checked(board, "white"))

    # Enemy rook
    r = Rook("black")
    q = Queen("black")

    #            0  1  2  3  4  5  6  7
    board = [
        [_, _, _, _, _, _, _, _],  # 0
        [_, _, _, _, _, _, _, _],  # 1
        [_, _, _, _, _, _, _, _],  # 2
        [_, _, _, _, _, _, _, _],  # 3
        [_, _, _, _, _, _, _, _],  # 4
        [_, _, _, _, _, _, _, _],  # 5
        [_, _, _, _, _, _, r, _],  # 6
        [_, _, k, _, _, _, _, q]  # 7
    ]
    assert_false("Should be checkmate", can_player_leave_check_state(board, "white", []))
Ejemplo n.º 13
0
 def test_fail_castling_when_king_already_moved(self):
     board = Board(
         initial_pieces={
             'a8': King('black'),
             'a7': Pawn('black'),
             'e1': King('white'),
             'h1': Rook('white')
         })
     board.move('e1', 'f1')
     board.move('a7', 'a6')  # pawn moves
     board.move('f1', 'e1')
     board.move('a6', 'a5')  # pawn moves
     self.assertRaises(ImpossibleMove, board.move, 'e1', 'g1')
Ejemplo n.º 14
0
def get_enpassant_board():
    __ = BlankPiece()
    p1 = Pawn("black")
    p2 = Pawn("black")
    wp = Pawn("white")
    wp.has_never_moved = False

    #             0   1   2   3   4   5   6   7
    board = [
        [__, __, __, __, __, __, __, __],  # 0
        [__, __, __, __, __, p1, __, p2],  # 1
        [__, __, __, __, __, __, __, __],  # 2
        [__, __, __, __, __, __, wp, __],  # 3
        [__, __, __, __, __, __, __, __],  # 4
        [__, __, __, __, __, __, __, __],  # 5
        [__, __, __, __, __, __, __, __],  # 6
        [__, __, __, __, __, __, __, __]  # 7
    ]

    assert_length(board[3][6].get_attack_set(board, [3, 6], []), 0)
    assert_contains(board[3][6].get_move_set(board, [3, 6], []),
                    [Move(MoveType.NORMAL, [3, 6], [2, 6])])
    return board
Ejemplo n.º 15
0
 def test_castling_left_black(self):
     board = Board(initial_pieces={
         'a2': Pawn('white'),
         'e8': King('black'),
         'a8': Rook('black')
     })
     king = board.squares['e8']
     rook = board.squares['a8']
     board.move('a2', 'a3')  # just because white have to move first
     board.move('e8', 'c8')
     self.assertIs(king, board.squares['c8'])
     self.assertIs(rook, board.squares['d8'])
     self.assertIsNone(board.squares['e8'])
     self.assertIsNone(board.squares['a8'])
Ejemplo n.º 16
0
def test_white_pawn_movements():
    __ = BlankPiece()

    p1 = Pawn("white")
    p2 = Pawn("white")
    p3 = Pawn("white")
    p3.has_never_moved = False

    # Enemy rook
    rr = Rook("black")

    #             0   1   2   3   4   5   6   7
    board = [
        [__, __, __, __, __, __, __, __],  # 0
        [__, __, __, __, __, __, __, __],  # 1
        [__, __, __, __, __, __, __, __],  # 2
        [__, __, __, __, __, __, rr, rr],  # 3
        [__, __, __, __, __, __, __, p3],  # 4
        [__, __, __, __, rr, __, __, __],  # 5
        [p1, __, __, __, __, p2, __, __],  # 6
        [__, __, __, __, __, __, __, __]  # 7
    ]
    # Left-most pawn
    assert_length(board[6][0].get_attack_set(board, [6, 0], []), 0)
    assert_contains(
        board[6][0].get_move_set(board, [6, 0], []),
        create_list_of_moves(MoveType.NORMAL, [6, 0], [[5, 0], [4, 0]]))

    assert_contains(board[6][5].get_attack_set(board, [6, 5], []),
                    create_list_of_moves(MoveType.NORMAL, [6, 5], [[5, 4]]))
    assert_contains(
        board[6][5].get_move_set(board, [6, 5], []),
        create_list_of_moves(MoveType.NORMAL, [6, 5], [[5, 5], [4, 5]]))

    assert_contains(board[4][7].get_attack_set(board, [4, 7], []),
                    create_list_of_moves(MoveType.NORMAL, [4, 7], [[3, 6]]))
    assert_length(board[4][7].get_move_set(board, [4, 7], []), 0)
Ejemplo n.º 17
0
def test_corner_mate():
    """"""
    game = Game([
        [Empty() for _ in range(7)] + [King('black')],
        [Empty() for _ in range(5)] +
        [Horse('white'), Empty(), Pawn('black')],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(6)] + [Tower('white'), Empty()],
    ])
    assert game.is_check('black'), print(game)
    assert game.is_checkmate('black'), print(game)
Ejemplo n.º 18
0
def test_black_pawn_movements():
    __ = BlankPiece()

    p1 = Pawn("black")
    p2 = Pawn("black")
    p3 = Pawn("black")

    # Enemy rook
    rr = Rook("white")

    #             0   1   2   3   4   5   6   7
    board = [
        [__, __, __, __, __, __, __, __],  # 0
        [p1, __, __, __, __, p2, __, __],  # 1
        [__, __, __, __, rr, __, __, __],  # 2
        [__, __, __, __, __, __, __, p3],  # 3
        [__, __, __, __, __, __, rr, rr],  # 4
        [__, __, __, __, __, __, __, __],  # 5
        [__, __, __, __, __, __, __, __],  # 6
        [__, __, __, __, __, __, __, __]  # 7
    ]
    # Left-most pawn
    assert_length(board[1][0].get_attack_set(board, [1, 0], []), 0)
    assert_contains(
        board[1][0].get_move_set(board, [1, 0], []),
        create_list_of_moves(MoveType.NORMAL, [1, 0], [[2, 0], [3, 0]]))

    assert_contains(board[1][5].get_attack_set(board, [1, 5], []),
                    create_list_of_moves(MoveType.NORMAL, [1, 5], [[2, 4]]))
    assert_contains(
        board[1][5].get_move_set(board, [1, 5], []),
        create_list_of_moves(MoveType.NORMAL, [1, 5], [[2, 5], [3, 5]]))

    assert_contains(board[3][7].get_attack_set(board, [3, 7], []),
                    create_list_of_moves(MoveType.NORMAL, [3, 7], [[4, 6]]))
    assert_length(board[3][7].get_move_set(board, [3, 7], []), 0)
Ejemplo n.º 19
0
def test_anastasia_s_mate():
    """"""
    game = Game([
        [Empty() for _ in range(8)],
        [Empty() for _ in range(4)] +
        [Horse('white'), Empty(),
         Pawn('black'), King('black')],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(7)] + [Tower('white')],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [King('white')] + [Empty() for _ in range(7)],
    ])
    assert game.is_check('black'), print(game)
    assert game.is_checkmate('black'), print(game)
Ejemplo n.º 20
0
def test_anderssen_s_mate():
    """"""
    game = Game([
        [Empty()
         for _ in range(6)] + [King('black'), Tower('white')],
        [Empty() for _ in range(6)] + [Pawn('white'), Empty()],
        [Empty() for _ in range(5)] +
        [King('white'), Empty(), Empty()],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
    ])
    assert game.is_check('black'), print(game)
    assert game.is_checkmate('black'), print(game)
Ejemplo n.º 21
0
def test_bodens_mate():
    """"""
    game = Game([
        [Empty(), Empty()] + [King('black'), Tower('black')] +
        [Empty() for _ in range(4)],
        [Empty()
         for _ in range(3)] + [Pawn('black')] + [Empty() for _ in range(4)],
        [Bishop('white')] + [Empty() for _ in range(7)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(5)] +
        [Bishop('white'), Empty(), Empty()],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
        [Empty() for _ in range(8)],
    ])
    assert game.is_check('black'), print(game)
    assert game.is_checkmate('black'), print(game)
Ejemplo n.º 22
0
    def test_knight_capture(self):
        board = Board(initial_pieces={
            'a8': King('black'),
            'e5': Pawn('black'),
            'f3': Knight('white')
        })
        pieces = [
            piece for piece in board.squares.values() if piece is not None
        ]
        self.assertEqual(3, len(pieces))

        knight = board.squares['f3']
        board.move('f3', 'e5')
        self.assertIs(knight, board.squares['e5'])

        pieces = [
            piece for piece in board.squares.values() if piece is not None
        ]
        self.assertEqual(2, len(pieces))
Ejemplo n.º 23
0
def get_fifty_move_rule_board():
    """
    Helper function to run through the first 3 moves in a "four move rule" (use a smaller deque for sanity here -- 50
    is a lot),
    """
    #
    conducted_move_history = deque([], 4)

    __ = BlankPiece()
    wk = King("white")
    wp = Pawn("white")
    br = Rook("black")
    bk = King("black")

    #     0   1   2   3   4   5   6   7
    board = [
        [__, __, __, bk, __, __, __, __],  # 0
        [__, __, __, __, __, __, __, __],  # 1
        [__, __, __, __, __, __, __, __],  # 2
        [__, br, __, __, __, __, __, __],  # 3
        [__, __, __, __, __, __, __, __],  # 4
        [__, __, __, __, __, __, __, __],  # 5
        [__, wp, __, __, __, __, __, __],  # 6
        [__, __, __, __, wk, __, __, __]  # 7
    ]

    msg = "Should NOT be 50-move rule draw"

    board, move_history_element = conduct_move(
        board, Move(MoveType.NORMAL, [7, 4], [7, 1]), "white")
    conducted_move_history.append(move_history_element)
    assert_false(msg, is_fifty_move_rule_draw(conducted_move_history))

    board, move_history_element = conduct_move(
        board, Move(MoveType.NORMAL, [7, 1], [7, 2]), "white")
    conducted_move_history.append(move_history_element)
    assert_false(msg, is_fifty_move_rule_draw(conducted_move_history))

    board, move_history_element = conduct_move(
        board, Move(MoveType.NORMAL, [7, 2], [7, 3]), "white")
    conducted_move_history.append(move_history_element)
    assert_false(msg, is_fifty_move_rule_draw(conducted_move_history))
    return board, conducted_move_history
Ejemplo n.º 24
0
def get_castling_board():
    # Home rank is just a constant in the castling implementation
    # so it's asserted unit testing only white piece's castling is sufficient
    __ = BlankPiece()

    p = Pawn("white")
    k = King("white")
    r1 = Rook("white")
    r2 = Rook("white")

    #     0   1   2   3   4   5   6   7
    return [
        [__, __, __, __, __, __, __, __],  # 0
        [__, __, __, __, __, __, __, __],  # 1
        [__, __, __, __, __, __, __, __],  # 2
        [__, __, __, __, __, __, __, __],  # 3
        [__, __, __, __, __, __, __, __],  # 4
        [__, __, __, __, __, __, __, __],  # 5
        [__, __, __, p, p, p, __, __],  # 6
        [r1, __, __, __, k, __, __, r2]  # 7
    ]
Ejemplo n.º 25
0
def print_board(player_color, board):
    # Direction of board depends on player color
    player_dir_increment = Pawn(player_color).forward_dir
    row_number = 8
    if player_color == "black":
        board = board.copy()
        list.reverse(board)
        row_number = 1

    print('  a b c d e f g h')
    for row in board[:]:
        print(row_number, end=' ')
        for piece in row[:]:
            if not piece.is_blank_piece:
                print(piece.unicode_symbol.decode("utf-8", "ignore"), end=' ')
            else:
                # just for prettiness
                print('_', end=' ')
        row_number = row_number + player_dir_increment
        print(end='\n')  # next row
    print('  a b c d e f g h')
Ejemplo n.º 26
0
def convert_from_string_gameboard(serializable_gameboard):
    """
    Get gameboard from the serializable entity.

    Note: This information is only useful for display purposes. (information such as whether castling is still a
    valid move does not exist here).

    :param game_board: list of lists containing "wp" or "bk" to represent white pawn or black knight.
    :return: list of list containing instances of the Piece class
    """
    gameboard = []
    for row in serializable_gameboard:
        gameboard_row = []
        for piece in row:
            piece_col = ""
            if piece == "_":
                gameboard_row.append(BlankPiece())
            else:
                if piece[0] == "w":
                    piece_col = "white"
                elif piece[0] == "b":
                    piece_col = "black"
                if piece[1] == "r":
                    gameboard_row.append(Rook(piece_col))
                elif piece[1] == "h":
                    gameboard_row.append(Knight(piece_col))
                elif piece[1] == "b":
                    gameboard_row.append(Bishop(piece_col))
                elif piece[1] == "q":
                    gameboard_row.append(Queen(piece_col))
                elif piece[1] == "k":
                    gameboard_row.append(King(piece_col))
                elif piece[1] == "p":
                    gameboard_row.append(Pawn(piece_col))
        gameboard.append(gameboard_row)
    return gameboard
Ejemplo n.º 27
0
def test_pawn_cant_move_back():
    board = Board(initial_pieces={'b4': Pawn('white')})
    with pytest.raises(ImpossibleMove):
        assert board.move('b4', 'b3')
Ejemplo n.º 28
0
 def test_pawn_move(self):
     pawn = Pawn('white')
     self.assertTrue(pawn.can_move('b4', 'b5'))
     self.assertTrue(pawn.can_move('e7', 'e8'))
Ejemplo n.º 29
0
 def test_fail_pawn_move(self):
     pawn = Pawn('white')
     self.assertFalse(pawn.can_move('e2', 'e8'))
Ejemplo n.º 30
0
 def test_fail_pawn_move_back(self):
     pawn = Pawn('white')
     self.assertFalse(pawn.can_move('b4', 'b3'))
Ejemplo n.º 31
0
    def __init__(self, initial_pieces=None):
        self.check = None
        self.__turn = 'white'
        self.__squares = {}
        self.__initial_pieces = initial_pieces or {
            'a1': Rook('white'),
            'b1': Knight('white'),
            'c1': Bishop('white'),
            'd1': Queen('white'),
            'e1': King('white'),
            'f1': Bishop('white'),
            'g1': Knight('white'),
            'h1': Rook('white'),
            'a2': Pawn('white'),
            'b2': Pawn('white'),
            'c2': Pawn('white'),
            'd2': Pawn('white'),
            'e2': Pawn('white'),
            'f2': Pawn('white'),
            'g2': Pawn('white'),
            'h2': Pawn('white'),
            'a8': Rook('black'),
            'b8': Knight('black'),
            'c8': Bishop('black'),
            'd8': Queen('black'),
            'e8': King('black'),
            'f8': Bishop('black'),
            'g8': Knight('black'),
            'h8': Rook('black'),
            'a7': Pawn('black'),
            'b7': Pawn('black'),
            'c7': Pawn('black'),
            'd7': Pawn('black'),
            'e7': Pawn('black'),
            'f7': Pawn('black'),
            'g7': Pawn('black'),
            'h7': Pawn('black'),
        }

        for _x in x:
            for _y in y:
                self.__squares[_x + _y] = None
                if _x + _y in self.__initial_pieces:
                    self.__squares[_x + _y] = self.__initial_pieces[_x + _y]
Ejemplo n.º 32
0
 def pawn_row(self, color):
     return [Pawn(color, self) for j in range(len(self))]
Ejemplo n.º 33
0
def test_teleporting_pieces(player_col, opponent_col):
    _ = BlankPiece()

    k = King(player_col)
    k.has_never_moved = False
    h = Knight(player_col)

    # Enemy rook
    e = Rook(opponent_col)
    # Friendly Pawn
    f = Pawn(player_col)
    f.has_never_moved = False

    #        0  1  2  3  4  5  6  7
    board = [
        [h, _, _, _, _, _, _, _],  # 0
        [_, _, _, _, _, _, _, _],  # 1
        [_, _, _, f, _, e, _, _],  # 2
        [_, _, _, _, _, _, _, _],  # 3
        [_, _, _, _, h, _, _, _],  # 4
        [_, _, _, _, _, _, _, _],  # 5
        [_, h, _, _, _, _, _, _],  # 6
        [_, _, _, _, _, _, _, k]  # 7
    ]
    # Top-left knight
    assert_length(board[0][0].get_move_set(board, [0, 0], []), 0)
    assert_contains(
        board[0][0].get_attack_set(board, [0, 0], []),
        create_list_of_moves(MoveType.NORMAL, [0, 0], [
            [1, 2],
            [2, 1],
        ]))

    # Knight near bottom
    assert_length(board[6][1].get_move_set(board, [6, 1], []), 0)
    assert_contains(
        board[6][1].get_attack_set(board, [6, 1], []),
        create_list_of_moves(MoveType.NORMAL, [6, 1],
                             [[4, 0], [4, 2], [5, 3], [7, 3]]))

    # Middle knight
    assert_length(board[4][4].get_move_set(board, [4, 4], []), 0)
    assert_contains(
        board[4][4].get_attack_set(board, [4, 4], []),
        create_list_of_moves(
            MoveType.NORMAL, [4, 4],
            [[2, 5], [6, 5], [6, 3], [3, 2], [5, 2], [3, 6], [5, 6]]))

    # Bottom-right king
    assert_length(board[7][7].get_move_set(board, [7, 7], []), 0)
    assert_contains(
        board[7][7].get_attack_set(board, [7, 7], []),
        create_list_of_moves(
            MoveType.NORMAL,
            [7, 7],
            [  # Up
                [6, 7],
                # Left
                [7, 6],
                # north-east
                [6, 6]
            ]))
Ejemplo n.º 34
0
    def __init__(self):
        self.classic = ([
            Rook.Rook(0, "White", 0, 0),
            Knight.Knight(1, "White", 1, 0),
            Bishop.Bishop(2, "White", 2, 0),
            Queen.Queen(3, "White", 3, 0),
            King.King(4, "White", 4, 0),
            Bishop.Bishop(5, "White", 5, 0),
            Knight.Knight(6, "White", 6, 0),
            Rook.Rook(7, "White", 7, 0),
            Pawn.Pawn(8, "White", 0, 1),
            Pawn.Pawn(9, "White", 1, 1),
            Pawn.Pawn(10, "White", 2, 1),
            Pawn.Pawn(11, "White", 3, 1),
            Pawn.Pawn(12, "White", 4, 1),
            Pawn.Pawn(13, "White", 5, 1),
            Pawn.Pawn(14, "White", 6, 1),
            Pawn.Pawn(15, "White", 7, 1)
        ], [
            Pawn.Pawn(16, "Black", 0, 6),
            Pawn.Pawn(17, "Black", 1, 6),
            Pawn.Pawn(18, "Black", 2, 6),
            Pawn.Pawn(19, "Black", 3, 6),
            Pawn.Pawn(20, "Black", 4, 6),
            Pawn.Pawn(21, "Black", 5, 6),
            Pawn.Pawn(22, "Black", 6, 6),
            Pawn.Pawn(23, "Black", 7, 6),
            Rook.Rook(24, "Black", 0, 7),
            Knight.Knight(25, "Black", 1, 7),
            Bishop.Bishop(26, "Black", 2, 7),
            King.King(27, "Black", 4, 7),
            Queen.Queen(28, "Black", 3, 7),
            Bishop.Bishop(29, "Black", 5, 7),
            Knight.Knight(30, "Black", 6, 7),
            Rook.Rook(31, "Black", 7, 7)
        ])

        self.table = Table.Table(self.classic[0], self.classic[1])
        self.shift = "White"
        self.player1 = ("Player 1", "White")
        self.player2 = ("Player 2", "Black")
        self.winner = None
        self.finish = False
Ejemplo n.º 35
0
def test_pawn():
    piece = Pawn(Color.WHITE, Position(x=3, y=3))
    assert piece.can_move_to(Position(x=3, y=4))
    assert piece.can_move_to(Position(x=3, y=5))
    piece.move_to(Position(x=3, y=4))
    assert not piece.can_move_to(Position(x=3, y=6))

    piece = Pawn(Color.BLACK, Position(x=3, y=3))
    assert piece.can_move_to(Position(x=3, y=2))
    assert piece.can_move_to(Position(x=3, y=1))
    piece.move_to(Position(x=3, y=2))
    assert not piece.can_move_to(Position(x=3, y=0))

    # Knight
    assert not piece.can_move_to(Position(x=4, y=5))
    assert not piece.can_move_to(Position(x=2, y=5))
    assert not piece.can_move_to(Position(x=4, y=1))
    assert not piece.can_move_to(Position(x=2, y=1))
    assert not piece.can_move_to(Position(x=1, y=2))
    assert not piece.can_move_to(Position(x=1, y=4))
    assert not piece.can_move_to(Position(x=5, y=2))
    assert not piece.can_move_to(Position(x=5, y=4))

    # Queen
    assert not piece.can_move_to(Position(x=7, y=3))
    assert not piece.can_move_to(Position(x=3, y=7))
    assert not piece.can_move_to(Position(x=1, y=1))
    assert not piece.can_move_to(Position(x=5, y=5))
    assert not piece.can_move_to(Position(x=1, y=5))
Ejemplo n.º 36
0
def test_teleporting_pieces(player_col, opponent_col):
    _ = BlankPiece()

    k = King(player_col)
    k.has_never_moved = False
    h = Knight(player_col)

    # Enemy rook
    e = Rook(opponent_col)
    # Friendly Pawn
    f = Pawn(player_col)
    f.has_never_moved = False

    #        0  1  2  3  4  5  6  7
    board = [
                [h, _, _, _, _, _, _, _],  # 0
                [_, _, _, _, _, _, _, _],  # 1
                [_, _, _, f, _, e, _, _],  # 2
                [_, _, _, _, _, _, _, _],  # 3
                [_, _, _, _, h, _, _, _],  # 4
                [_, _, _, _, _, _, _, _],  # 5
                [_, h, _, _, _, _, _, _],  # 6
                [_, _, _, _, _, _, _, k]   # 7
            ]
    # Top-left knight
    assert_length(board[0][0].get_move_set(board, [0, 0], []), 0)
    assert_contains(board[0][0].get_attack_set(board, [0, 0], []),
                    create_list_of_moves(MoveType.NORMAL, [0, 0],
                                         [
                                      [1, 2],
                                      [2, 1],
                                  ]))

    # Knight near bottom
    assert_length(board[6][1].get_move_set(board, [6, 1], []), 0)
    assert_contains(board[6][1].get_attack_set(board, [6, 1], []),
                    create_list_of_moves(MoveType.NORMAL, [6, 1],
                                         [
                                      [4, 0],
                                      [4, 2],
                                      [5, 3],
                                      [7, 3]
                                  ]))

    # Middle knight
    assert_length(board[4][4].get_move_set(board, [4, 4], []), 0)
    assert_contains(board[4][4].get_attack_set(board, [4, 4], []),
                    create_list_of_moves(MoveType.NORMAL, [4, 4],
                                         [
                                      [2, 5],
                                      [6, 5],
                                      [6, 3],
                                      [3, 2],
                                      [5, 2],
                                      [3, 6],
                                      [5, 6]
                                  ]))

    # Bottom-right king
    assert_length(board[7][7].get_move_set(board, [7, 7], []), 0)
    assert_contains(board[7][7].get_attack_set(board, [7, 7], []),
                    create_list_of_moves(MoveType.NORMAL, [7, 7],
                                         [   # Up
                                      [6, 7],
                                      # Left
                                      [7, 6],
                                      # north-east
                                      [6, 6]
                                  ]))
Ejemplo n.º 37
0
def test_sliding_pieces(player_col, opponent_col):
    _ = BlankPiece()
    r = Rook(player_col)
    b = Bishop(player_col)
    q = Queen(player_col)

    # Enemy rook
    e = Rook(opponent_col)
    # Friendly Pawn
    f = Pawn(player_col)
    f.has_never_moved = False

    #            0  1  2  3  4  5  6  7
    board = [
        [r, _, _, e, _, _, _, _],  # 0
        [_, _, _, _, _, _, r, _],  # 1
        [_, _, q, _, _, _, f, _],  # 2
        [_, _, _, _, _, _, _, _],  # 3
        [_, _, _, _, _, _, _, _],  # 4
        [_, _, _, b, _, _, _, _],  # 5
        [_, _, _, _, _, _, _, _],  # 6
        [_, _, e, _, _, _, _, f]  # 7
    ]

    # Top-left rook
    assert_length(board[0][0].get_move_set(board, [0, 0], []), 0)
    assert_contains(
        board[0][0].get_attack_set(board, [0, 0], []),
        create_list_of_moves(
            MoveType.NORMAL,
            [0, 0],
            [  # Down
                [1, 0],
                [2, 0],
                [3, 0],
                [4, 0],
                [5, 0],
                [6, 0],
                [7, 0],
                # Right
                [0, 1],
                [0, 2],
                [0, 3]
            ]))

    # Second rook
    assert_length(board[1][6].get_move_set(board, [1, 6], []), 0)
    assert_contains(
        board[1][6].get_attack_set(board, [1, 6], []),
        create_list_of_moves(
            MoveType.NORMAL,
            [1, 6],
            [  # Up
                [0, 6],
                # Left
                [1, 5],
                [1, 4],
                [1, 3],
                [1, 2],
                [1, 1],
                [1, 0],
                # Right
                [1, 7]
            ]))

    # Bishop
    assert_length(board[5][3].get_move_set(board, [5, 3], []), 0)
    assert_contains(
        board[5][3].get_attack_set(board, [5, 3], []),
        create_list_of_moves(
            MoveType.NORMAL,
            [5, 3],
            [  # North-west
                [4, 2],
                [3, 1],
                [2, 0],
                # North-east
                [4, 4],
                [3, 5],
                # South-west
                [6, 2],
                [7, 1],
                # South-east
                [6, 4],
                [7, 5]
            ]))

    # Queen
    assert_length(board[2][2].get_move_set(board, [2, 2], []), 0)
    assert_contains(
        board[2][2].get_attack_set(board, [2, 2], []),
        create_list_of_moves(
            MoveType.NORMAL,
            [2, 2],
            [  # Down
                [3, 2],
                [4, 2],
                [5, 2],
                [6, 2],
                [7, 2],
                # Up
                [1, 2],
                [0, 2],
                # Left
                [2, 0],
                [2, 1],
                # Right
                [2, 3],
                [2, 4],
                [2, 5],
                # North-west
                [1, 1],
                # North-east
                [1, 3],
                [0, 4],
                # South-west
                [3, 1],
                [4, 0],
                # South-east
                [3, 3],
                [4, 4],
                [5, 5],
                [6, 6]
            ]))
Ejemplo n.º 38
0
def test_sliding_pieces(player_col, opponent_col):
    _ = BlankPiece()
    r = Rook(player_col)
    b = Bishop(player_col)
    q = Queen(player_col)

    # Enemy rook
    e = Rook(opponent_col)
    # Friendly Pawn
    f = Pawn(player_col)
    f.has_never_moved = False

    #            0  1  2  3  4  5  6  7
    board = [
                [r, _, _, e, _, _, _, _],  # 0
                [_, _, _, _, _, _, r, _],  # 1
                [_, _, q, _, _, _, f, _],  # 2
                [_, _, _, _, _, _, _, _],  # 3
                [_, _, _, _, _, _, _, _],  # 4
                [_, _, _, b, _, _, _, _],  # 5
                [_, _, _, _, _, _, _, _],  # 6
                [_, _, e, _, _, _, _, f]   # 7
            ]

    # Top-left rook
    assert_length(board[0][0].get_move_set(board, [0, 0], []), 0)
    assert_contains(board[0][0].get_attack_set(board, [0, 0], []),
                    create_list_of_moves(MoveType.NORMAL, [0, 0],
                                         [   # Down
                                      [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0],
                                      # Right
                                      [0, 1], [0, 2], [0, 3]
                                  ]))

    # Second rook
    assert_length(board[1][6].get_move_set(board, [1, 6], []), 0)
    assert_contains(board[1][6].get_attack_set(board, [1, 6], []),
                    create_list_of_moves(MoveType.NORMAL, [1, 6],
                                         [   # Up
                                      [0, 6],
                                      # Left
                                      [1, 5], [1, 4], [1, 3], [1, 2], [1, 1], [1, 0],
                                      # Right
                                      [1, 7]
                                  ]))

    # Bishop
    assert_length(board[5][3].get_move_set(board, [5, 3], []), 0)
    assert_contains(board[5][3].get_attack_set(board, [5, 3], []),
                    create_list_of_moves(MoveType.NORMAL, [5, 3],
                                         [   # North-west
                                      [4, 2], [3, 1], [2, 0],
                                      # North-east
                                      [4, 4], [3, 5],
                                      # South-west
                                      [6, 2], [7, 1],
                                      # South-east
                                      [6, 4], [7, 5]
                                  ]))

    # Queen
    assert_length(board[2][2].get_move_set(board, [2, 2], []), 0)
    assert_contains(board[2][2].get_attack_set(board, [2, 2], []),
                    create_list_of_moves(MoveType.NORMAL, [2, 2],
                                         [   # Down
                                      [3, 2], [4, 2], [5, 2], [6, 2], [7, 2],
                                      # Up
                                      [1, 2], [0, 2],
                                      # Left
                                      [2, 0], [2, 1],
                                      # Right
                                      [2, 3], [2, 4], [2, 5],
                                      # North-west
                                      [1, 1],
                                      # North-east
                                      [1, 3], [0, 4],
                                      # South-west
                                      [3, 1], [4, 0],
                                      # South-east
                                      [3, 3], [4, 4], [5, 5], [6, 6]
                                  ]))