Exemple #1
0
    def initialize_board_solution(self):
        from utilities.rectangle import Rectangle
        initial_solution_state = [[-1 for c in range(self.cols)]
                                  for r in range(self.rows)]
        for i in range(len(self.blocks)):
            initial_solution_state[self.blocks[i].row][self.blocks[i].col] = i

        self.solution = deepcopy(initial_solution_state)

        # cover the solution with all possible rectangles for all blocks
        for k in range(len(self.blocks)):
            curr_row = self.blocks[k].row
            curr_col = self.blocks[k].col
            curr_value = self.blocks[k].value
            for factor in self.blocks[k].factors:
                for i in range(curr_value // factor):
                    for j in range(factor):
                        top_left = Point(
                            curr_col + i - curr_value // factor + 1,
                            curr_row - j)
                        bottom_right = Point(curr_col + i,
                                             curr_row + factor - 1 - j)
                        try:
                            rect = Rectangle(self, top_left, bottom_right, k)
                            rect.draw_rectangle()
                        except AssertionError:
                            continue
        self._update_final_cells_values()
        self.solution = initial_solution_state
Exemple #2
0
 def test_find_color(self):
     board = GameBoard('tests/test_puzzle.txt')
     board.read_board()
     rect = Rectangle(board, Point(0, 0), Point(0, 0), 3)
     rect.draw_rectangle()
     rect = Rectangle(board, Point(0, 0), Point(2, 2), 4)
     rect.find_color()
     self.assertEqual(rect.color, 3)
Exemple #3
0
 def test_draw_rect(self):
     board = GameBoard('tests/test_puzzle.txt')
     board.read_board()
     rect = Rectangle(board, Point(0, 0), Point(2, 1), 4)
     rect.draw_rectangle()
     for y in range(2):
         for x in range(3):
             self.assertEqual(board.solution[y][x], 4)
Exemple #4
0
    def update_solution(self, first_point: Point, second_point: Point):
        self.board.solution = deepcopy(self.solution_backup)
        for i in range(self.board.rows):
            for j in range(self.board.cols):
                self.squares[i][j].color = self.squares_backup[i][j]

        rect = Rectangle(self.board, first_point, second_point, 0)
        rect.find_color()
        color = (250, 190, 250) if rect.color == -1 \
            else COLORS[rect.color % (len(COLORS) - 1)][0:3]
        rect.draw_rectangle()

        for x in range(rect.top_left.x, rect.bottom_right.x + 1):
            for y in range(rect.top_left.y, rect.bottom_right.y + 1):
                self.squares[self.board.cols - y - 1][x].color = color
Exemple #5
0
    def backtrack(self, block_pointer: int = 0):
        # if all blocks are visited the solution is found
        if block_pointer > len(self.board.blocks) - 1:
            return True

        curr_block = self.board.blocks[block_pointer]

        # go through all block's factors
        while curr_block.factor_pointer < len(curr_block.factors):
            curr_factor = curr_block.factors[curr_block.factor_pointer]

            # go through all rectangles
            # curr_factor * curr_block.value // curr_factor
            for i in range(curr_block.value // curr_factor):
                for j in range(curr_factor):
                    top_left = Point(
                        curr_block.col + i - curr_block.value // curr_factor+1,
                        curr_block.row - j)
                    bottom_right = Point(
                        curr_block.col + i,
                        curr_block.row + curr_factor - 1 - j)
                    try:
                        rect = Rectangle(
                            self.board, top_left, bottom_right, block_pointer)
                        if not rect.check_conflicts():
                            rect.draw_rectangle()
                            if self._is_area_full_covered(block_pointer):
                                # true means correct solution is found
                                if self.backtrack(block_pointer + 1):
                                    return True
                            # incorrect state -> reset changes
                            rect.clear()
                            # colorize cell
                            self.board.solution[
                                curr_block.row][curr_block.col] = block_pointer
                    except AssertionError:
                        continue
            self.board.blocks[block_pointer].factor_pointer += 1
        # if no rectangles are found, reset pointer
        if block_pointer > 0:
            self.board.blocks[block_pointer].factor_pointer = 0