Exemple #1
0
    def _find_bounds(self):
        pruned_row_count, pruned_col_count = self.get_pruned_dimensions()

        # Upper Bound
        for i in range(pruned_row_count):
            if any(block != 0 for block in self._pruned[i]):
                self._upper_bound = i
                break

        # Lower Bound
        for i in range(pruned_row_count - 1, -1, -1):
            if any(block != 0 for block in self._pruned[i]):
                self._lower_bound = i
                break

        # Left Bound
        for i in range(pruned_col_count):
            if any(block != 0
                   for block in MatrixUtil.get_column(self._pruned, i)):
                self._left_bound = i
                break

        # Right Bound
        for i in range(pruned_col_count - 1, -1, -1):
            if any(block != 0
                   for block in MatrixUtil.get_column(self._pruned, i)):
                self._right_bound = i
                break
Exemple #2
0
    def check(self, variables, assignments):
        # Question 2, your check solution goes here.

        # This method returns True iff the given variables and their assignments satisfy the Tetromino Puzzle
        # Constraint. Recall this constraint is that all tetromino pieces are placed onto the grid without
        # colliding with any present pieces on the grid or any other pieces to be placed.

        # As you will be manipulating Tetromino pieces here you should be manipulating _copies_ of the pieces
        # instead of the original. Same goes with the grid being worked with (self._grid).

        grid_copy = MatrixUtil.copy(self._grid)

        for variable in variables:
            if assignments[variable] is not None:
                tetromino_copy = TetrominoUtil.copy(variable)
                col = assignments[variable][0][1]
                row = assignments[variable][0][0]
                tetromino_copy.rotate(assignments[variable][1])
                varx = tetromino_copy.get_pruned_dimensions()[0]
                vary = tetromino_copy.get_pruned_dimensions()[1]
                grid = tetromino_copy.get_pruned_grid()

                if MatrixUtil.valid_position(grid_copy, row + varx - 1,col + vary - 1):
                    for x in range(varx):
                        for y in range(vary):
                            if (grid_copy[row + x][col + y] != 0) and (grid[x][y] != 0):
                                return False
                    TetrominoUtil.place(grid_copy, tetromino_copy, (row, col))
                else:
                    return False
        return True
Exemple #3
0
  def rotation_limit(tetromino):
    rotated = MatrixUtil.rotate(MatrixUtil.copy(tetromino.get_pruned_grid()))
    limit = 1
    
    while (rotated != tetromino.get_pruned_grid()):
      limit += 1
      rotated = MatrixUtil.rotate(rotated)

    return limit
Exemple #4
0
 def place(grid, tetromino, pos):
   block_grid = tetromino.get_pruned_grid()
   rows, cols = tetromino.get_pruned_dimensions()
   for r in range(rows):
     for c in range(cols):
       dr, dc = pos[0] + r, pos[1] + c
       if MatrixUtil.valid_position(grid, dr, dc) and block_grid[r][c] > 0:
         grid[dr][dc] = block_grid[r][c]
Exemple #5
0
    def __init__(self, name, copy=None):

        if not copy:
            self._build()
            self._pruned = MatrixUtil.prune(self._original, 0)
        else:
            self._original = copy.get_original_grid()[:]
            self._pruned = copy.get_pruned_grid()[:]

        self._name = name
        self._find_bounds()
Exemple #6
0
    def check(self, variables, assignments):
        grid_copy = MatrixUtil.copy(self._grid)

        for var in variables:
            move = assignments[var]
            var_copy = TetrominoUtil.copy(var)
            if move is not None:
                pos = move[0]
                var_copy.rotate(move[1])
                rows, cols = var_copy.get_pruned_dimensions()
                pruned_grid = var_copy.get_pruned_grid()

                if not MatrixUtil.valid_position(grid_copy, pos[0] + rows - 1,
                                                 pos[1] + cols - 1):
                    return False
                for r in range(rows):
                    for c in range(cols):
                        dr, dc = pos[0] + r, pos[1] + c
                        if grid_copy[dr][dc] > 0 and pruned_grid[r][c] > 0:
                            return False
                        grid_copy[dr][dc] = pruned_grid[r][c]

        return True
Exemple #7
0
    def _can_move_left(self, pos):
        pruned_row_count, pruned_col_count = self._tetromino.get_pruned_dimensions(
        )

        # Check left bound
        if not self._state.is_valid_position(pos[0], pos[1]):
            return False

        for c in range(pruned_col_count):
            column = MatrixUtil.get_column(self._state.get_grid(), pos[1] + c)
            col_section = column[pos[0]:(pos[0] + pruned_row_count)]

            # Check if you can drop (needs to be only for the parts which are blocks
            for r in range(pruned_row_count):
                if self._tetromino.get_pruned_grid(
                )[r][c] > 0 and col_section[r] != 0:
                    return False

        return True
 def is_valid_position(self, r, c):
   return MatrixUtil.valid_position(self._grid, r, c)
Exemple #9
0
 def __init__(self, grid):
     super().__init__()
     self._grid = MatrixUtil.copy(grid)
Exemple #10
0
 def rotate(self, n=1):
     self._original = MatrixUtil.rotate(self._original, n)
     self._pruned = MatrixUtil.prune(self._original, 0)
     self._find_bounds()
Exemple #11
0
 def set_original_grid(self, grid):
     self._original = grid
     self._pruned = MatrixUtil.prune(self._original, 0)
     self._find_bounds()