Esempio n. 1
0
def test_rook_valid_moves():
    board = Board()
    rook = Rook(4, 4, 'w')
    rook.valid_moves(board)
    actual = rook.move_list
    expected = [[3, 4], [2, 4], [1, 4], [0, 4], [5, 4], [6, 4], [7, 4], [4, 3],
                [4, 2], [4, 1], [4, 0], [4, 5], [4, 6], [4, 7]]
    assert actual == expected
Esempio n. 2
0
def test_check_checked_checking_true_two():
    board = Board()
    board.board[3][1] = King(3, 1, "w")
    rook = Rook(3, 7, "b")
    board.board[3][7] = rook
    rook.valid_moves(board)
    actual = board.check_status()
    expected = 'w'
    assert actual == expected
Esempio n. 3
0
def test_check_checked_checking_true():
    board = Board()
    board.board[1][1] = King(1, 1, "w")
    rook = Rook(1, 7, "b")
    board.board[1][7] = rook
    rook.valid_moves(board)
    actual1 = board.check_status()
    expected1 = 'w'
    actual2 = rook.attack_list
    expected2 = [[1, 1]]
    assert actual1 == expected1
    assert actual2 == expected2
Esempio n. 4
0
def test_rook_attack_list():
    board = Board()
    rook = Rook(4, 4, 'w')
    board.board[4][1] = Pawn(4, 1, 'b')
    board.board[4][2] = Pawn(4, 2, 'b')
    board.board[7][4] = Pawn(7, 4, 'b')
    board.board[0][4] = Pawn(0, 4, 'b')
    board.board[6][6] = Pawn(6, 6, 'b')
    rook.valid_moves(board)
    actual = rook.attack_list
    expected = [[0, 4], [7, 4], [4, 2]]
    assert actual == expected
Esempio n. 5
0
def test_pins():
    board = Board()
    king = King(7, 4, 'w')
    board.board[7][4] = king
    bishop = Bishop(6, 4, 'w')
    board.board[6][4] = bishop
    b_rook = Rook(5, 4, 'b')
    board.board[5][4] = b_rook
    bishop.valid_moves(board)
    b_rook.valid_moves(board)
    bishop.test_if_your_king_is_in_check(board)
    actual = bishop.move_list
    expected = []
    assert actual == expected
Esempio n. 6
0
def test_psuedo_move():
    board = Board()
    king = King(7, 0, 'w')
    board.board[7][0] = king
    queen = Queen(6, 0, 'b')
    board.board[6][0] = queen
    rook = Rook(5, 0, 'b')
    board.board[5][0] = rook
    queen.valid_moves(board)
    rook.valid_moves(board)
    king.valid_moves(board)

    actual = king.psuedo_move([6, 0], board)
    print('rook attacks', rook.attack_list)
    expected = False
    assert actual == expected
Esempio n. 7
0
def test_multiple_moves():
    board = Board()
    w_rook = Rook(7, 3, 'w')
    b_rook = Rook(0, 0, 'b')
    board.board[7][3] = w_rook
    board.board[0][0] = b_rook
    w_rook.valid_moves(board)
    b_rook.valid_moves(board)
    board.move([7, 3], [0, 3])
    actual1 = isinstance(board.board[0][3], Rook)
    expected1 = True
    board.move([0, 0], [0, 2])
    actual2 = isinstance(board.board[0][2], Rook)
    expected2 = True
    assert actual1 == expected1
    assert actual2 == expected2
Esempio n. 8
0
def test_checkmate_true():
    board = Board()
    king = King(7, 0, 'w')
    board.board[7][0] = king
    queen = Queen(6, 0, 'b')
    board.board[6][0] = queen
    rook = Rook(5, 0, 'b')
    board.board[5][0] = rook
    queen.valid_moves(board)
    rook.valid_moves(board)

    print('king position', queen.attack_list)
    king.valid_moves(board)
    actual = board.checkmate_status()
    print('king attacks', king.attack_list)
    print('rook attacks', rook.attack_list)
    expected = True
    assert actual == expected
Esempio n. 9
0
    def is_controlled_sq(self, req_pos, turn):
        '''
        Checks whether a square is controlled by a piece.
        'turn' means controlled by piece of which colour/turn
        '''

        # Along knight routes (L)
        knight = Knight(0)
        knight.set_pos(req_pos)
        # print(self.get_sq_notation(req_pos))
        # print()
        for move in knight.valid_moves():
            # print(self.get_sq_notation(move), end=': ')
            piece = self.fetch_piece(move)
            # print(piece)
            if piece and type(piece).__name__ == 'Knight' and piece.colour == turn:
                # self.pos_of_piece_causing_check = move
                # print('yeah')
                del knight
                return True

        # Along diagonals
        bishop = Bishop(0)
        bishop.set_pos(req_pos)
        for move in bishop.valid_moves():
            piece = self.fetch_piece(move)

            if piece and \
            (type(piece).__name__ == 'Bishop' or
            type(piece).__name__ == 'Queen') and \
            piece.colour == turn and \
            not piece.is_path_obstructed(self, req_pos, move):
                # print('yes')
                # self.pos_of_piece_causing_check = move
                del bishop
                # print('Obstruct')
                return True

        # Along ranks and files
        rook = Rook(0)
        rook.set_pos(req_pos)
        for move in rook.valid_moves():
            # print(self.get_sq_notation(move), end=": ")
            piece = self.fetch_piece(move)
            # print(piece)

            if piece and (type(piece).__name__ == 'Rook' or
            type(piece).__name__ == 'Queen') and \
            piece.colour == turn and \
            not piece.is_path_obstructed(self, req_pos, move):
                # self.pos_of_piece_causing_check = move
                # print('here')
                del rook
                return True

        # King opposition
        king = King()
        king.set_pos(req_pos)
        for move in king.valid_moves():
            piece = self.fetch_piece(move)
            if piece and type(piece).__name__ == 'King' and piece.colour == turn:
                if not self.is_controlled_sq(move, self.turn):
                    # print('here')
                    # self.pos_of_piece_causing_check = move
                    del king
                    return True

        # Pawns
        if turn == 'White':
            pawn = Pawn(colour='Black')
        else:
            pawn = Pawn()
        pawn.set_pos(req_pos)
        for move in pawn.valid_moves(self.is_flipped):
            # If directly in front/back
            if move[0] - req_pos[0] == 0:
                continue
            piece = self.fetch_piece(move)
            if piece and type(piece).__name__ == 'Pawn' and piece.colour == turn:
                # self.pos_of_piece_causing_check = move
                del pawn
                return True

        del knight, bishop, rook, king, pawn

        # print('Nope')
        return False
Esempio n. 10
0
    def valid_moves(self):
        valids = []
        valids = Bishop.valid_moves(self)
        valids.extend(Rook.valid_moves(self))

        return valids