コード例 #1
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
コード例 #2
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
コード例 #3
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
コード例 #4
0
 def __init__(self, grid):
     super().__init__()
     self._grid = MatrixUtil.copy(grid)