def performNSteps(grid, n): temp = SlidingBlock(grid) for _ in range(n): validActions = getValidActions(grid) tempBool = False # Consider moving the red block first. for action in validActions: if action[0] == 1 and action[1] != 'down' and action[1] != 'left': grid = temp.result(grid, action) tempBool = True break # Consider moving a yellow block down or left. if not tempBool: for action in validActions: if action[0] != 1 and (action[1] == 'down' or action[1] == 'left'): grid = temp.result(grid, action) tempBool = True break if validActions != [] and not tempBool: action = random.choice(validActions) grid = temp.result(grid, action) return grid
def performNRandomSteps(grid, n): temp = SlidingBlock(grid) count = 0 while True: count += 1 validActions = getValidActions(grid) if validActions != []: action = random.choice(validActions) grid = temp.result(grid, action) if checkIfGoalTileIsInUpperLeftHalfgrid(grid): break else: if count > 1000: for symbol in getBlockSymbolsList(grid): if checkIfAnyTileIsInUpperLeftHalfgrid(grid, symbol): grid = exchangeBlock(grid, 1, symbol) continue return grid