def generateSlidingBlockGrid(size): grid = None grid = convertTupleToList(generateGrid(size, 0)) goalBlock = generateGoalBlock(size) # grid = insertGoalBlockInEmptyGrid(goalBlock, grid) grid = insertGoalBlockInStartPosition(goalBlock, grid) #{DIFFICULT} i = 2 while getSpaceNumber(grid) > max(getSize(goalBlock)[0], getSize(goalBlock)[1]) + size[0]: #{DIFFICULT} # while getSpaceNumber(grid) > (size[0]+size[1]): newBlock = generateNonGoalBlock(size, i) if getInsertPosition(newBlock, grid) != (-1, -1): grid = insertNonGoalBlock(newBlock, grid, i) else: continue i += 1 # grid = performNSteps(grid, steps) if not checkIfGoalTileIsInUpperLeftHalfgrid(grid): return generateSlidingBlockGrid(size) else: return grid
def generateSlidingBlockGrid(size): grid = None grid = convertTupleToList(generateGrid(size, 0)) goalBlock = generateGoalBlock(size) # grid = insertGoalBlockInEmptyGrid(goalBlock, grid) grid = insertGoalBlockInStartPosition(goalBlock, grid) #{DIFFICULT} i = 2 while getSpaceNumber(grid) > max( getSize(goalBlock)[0], getSize(goalBlock)[1]) + size[0]: #{DIFFICULT} # while getSpaceNumber(grid) > (size[0]+size[1]): newBlock = generateNonGoalBlock(size, i) if getInsertPosition(newBlock, grid) != (-1, -1): grid = insertNonGoalBlock(newBlock, grid, i) else: continue i += 1 # grid = performNSteps(grid, steps) if not checkIfGoalTileIsInUpperLeftHalfgrid(grid): return generateSlidingBlockGrid(size) else: return grid
def insertGoalBlockInEmptyGrid(goalBlock, emptyGrid): emptyGrid = convertTupleToList(emptyGrid) for i in range(len(emptyGrid) - getSize(goalBlock)[0], len(emptyGrid)): for j in range (0, getSize(goalBlock)[1]): emptyGrid[i][j] = 1 return convertListToTuple(emptyGrid)
def insertGoalBlockInEmptyGrid(goalBlock, emptyGrid): emptyGrid = convertTupleToList(emptyGrid) for i in range(len(emptyGrid) - getSize(goalBlock)[0], len(emptyGrid)): for j in range(0, getSize(goalBlock)[1]): emptyGrid[i][j] = 1 return convertListToTuple(emptyGrid)
def insertGoalBlockInStartPosition(goalBlock, emptyGrid): emptyGrid = convertTupleToList(emptyGrid) goalSize = getSize(goalBlock) for i in range(0, goalSize[0]): for j in range (len(emptyGrid[i]) - goalSize[1], len(emptyGrid[i])): emptyGrid[i][j] = 1 return convertListToTuple(emptyGrid)
def insertGoalBlockInStartPosition(goalBlock, emptyGrid): emptyGrid = convertTupleToList(emptyGrid) goalSize = getSize(goalBlock) for i in range(0, goalSize[0]): for j in range(len(emptyGrid[i]) - goalSize[1], len(emptyGrid[i])): emptyGrid[i][j] = 1 return convertListToTuple(emptyGrid)
def exchangeBlock(grid, symbol1, symbol2): visitedPos = set() grid = convertTupleToList(grid) for i in range(len(grid)): for j in range(len(grid[i])): if grid[i][j] == symbol1: visitedPos.add((i, j)) grid[i][j] = symbol2 for i in range(len(grid)): for j in range(len(grid[i])): if grid[i][j] == symbol2 and (i, j) not in visitedPos: grid[i][j] = symbol1 return convertListToTuple(grid)
def insertNonGoalBlock(block, grid, symbol): (blockX, blockY) = getSize(block) # Get the position of the leftmost upper corner insertPos = getInsertPosition(block, grid) i = insertPos[0] j = insertPos[1] grid = convertTupleToList(grid) grid[i][j] = symbol # Fill it up for k in range(blockX): for l in range(blockY): if grid[i + k][j + l] == 0: grid[i + k][j + l] = symbol return convertListToTuple(grid)
return False def checkIfAnyTileIsInUpperLeftHalfgrid(grid, symbol): for i in range(0, int(floor(len(grid)/2))): for j in range(int(floor(len(grid)/2)), len(grid)): if grid[i][j] == symbol: return True return False #_______________________________EXTEND OPERATIONS (NOT USED GENERALLY)_____________________________________ def extendBottom(grid, (x,y), blockY, symbol): grid = convertTupleToList(grid) for j in range(y, y + blockY): grid[x + 1][j] = symbol return convertListToTuple(grid) def extendLeft(grid, (x,y), blockX, symbol): grid = convertTupleToList(grid) for i in range(x - blockX + 1, x + 1): grid[i][y - 1] = symbol return convertListToTuple(grid) def extendTop(grid, (x,y), blockX, blockY, symbol): grid = convertTupleToList(grid) for j in range(y, y + blockY): grid[x - blockX][j] = symbol return convertListToTuple(grid)
def checkIfAnyTileIsInUpperLeftHalfgrid(grid, symbol): for i in range(0, int(floor(len(grid) / 2))): for j in range(int(floor(len(grid) / 2)), len(grid)): if grid[i][j] == symbol: return True return False #_______________________________EXTEND OPERATIONS (NOT USED GENERALLY)_____________________________________ def extendBottom(grid, (x, y), blockY, symbol): grid = convertTupleToList(grid) for j in range(y, y + blockY): grid[x + 1][j] = symbol return convertListToTuple(grid) def extendLeft(grid, (x, y), blockX, symbol): grid = convertTupleToList(grid) for i in range(x - blockX + 1, x + 1): grid[i][y - 1] = symbol return convertListToTuple(grid) def extendTop(grid, (x, y), blockX, blockY, symbol): grid = convertTupleToList(grid) for j in range(y, y + blockY):