예제 #1
0
def is_collided(at: Tuple[int, int], board: Board) -> bool:
    """check collision for the given queen
    Args:
        at (Tupe[int, int]): the place of queen (row, column)
        boad (Board): Board
    Returns:
        (bool): True if the given queen can collide to the other one, else False
    """
    n = board.n

    # return False constantly if no queen exists at the given place
    if not board.has_queen(at=at):
        return False

    given_row, given_column = at
    for i in range(n):
        check_columns = [given_column]
        if i != given_row:
            # check diagonal
            distance = abs(i - given_row)
            if given_column - distance >= 0:
                check_columns.append(given_column - distance)
            if given_column + distance < n:
                check_columns.append(given_column + distance)
        else:
            # check all items in the given row except for itself
            check_columns = [k for k in range(n)]
            check_columns.remove(given_column)
        # check
        for column in check_columns:
            if board.has_queen(at=(i, column)):
                return True
    return False
예제 #2
0
def test_validate():
    """test for validate
    """
    # True
    b = Board(n=3)
    b.set_queen(at=(0, 0))
    b.set_queen(at=(1, 2))
    assert validate(board=b)

    # False
    b = Board(n=3)
    b.set_queen(at=(0, 0))
    b.set_queen(at=(1, 2))
    b.set_queen(at=(2, 1))
    assert not validate(board=b)
예제 #3
0
 def solve(self) -> List[Board]:
     """solve problem using greedy search algolithm
     Args:
         board (Board): Board
     Returns:
         results (List[Board]): results
     """
     # greedy search
     for seq in permutations([i for i in range(self.n)]):
         b = Board(n=self.n)
         for j in range(self.n):
             b.set_queen(at=(j, seq[j]))
         if validate(board=b):
             self.results.append(b)
             # return early if requires taking a solution
             if self.take_one_solution:
                 break
     return self.results
예제 #4
0
def test_is_collided():
    """test for is_collided
    """
    # case 1: collided
    b = Board(n=2)
    b.set_queen(at=(0, 0))
    b.set_queen(at=(0, 1))
    b.set_queen(at=(1, 0))
    assert is_collided(at=(0, 0), board=b)
    assert is_collided(at=(0, 1), board=b)
    assert is_collided(at=(1, 0), board=b)

    # case 2: no collided
    b = Board(n=2)
    b.set_queen(at=(0, 0))
    assert not is_collided(at=(0, 0), board=b)
    assert not is_collided(at=(0, 1), board=b)
    assert not is_collided(at=(1, 0), board=b)
예제 #5
0
    def convert_to_boards(self, enable_print: bool) -> List[Board]:
        """convert current state to Board

        Returns:
            boards (List[Board]): current state descirbed as Board
        Note:
            it returns a list but its length is always 1
        """
        # for debug
        self.debug_end_time = time.time()
        if self.debug_start_time is not None:
            self.debug_duration_seconds = self.debug_end_time - self.debug_start_time

        if enable_print:
            b = Board(n=self.n)
            for k, v in self.queen_is.items():
                b.set_queen(at=(k, v))
            return [b]
        else:
            return None
    def convert_to_boards(self) -> List[Board]:
        """convert current state to Board

        Returns:
            boards (List[Board]): current state descirbed as Board
        Note:
            it returns a list but its length is always 1
        """
        # for debug
        self.debug_end_time = datetime.datetime.now()
        if self.debug_start_time is not None:
            self.debug_duration_seconds = (self.debug_end_time -
                                           self.debug_start_time).seconds

        b = Board(n=self.n)
        for i in range(self.n):
            for j in range(self.n):
                if self.current_state[i][j] is True:
                    b.set_queen(at=(i, j))
        return [b]