In this assignment, you are going to design and develop an interactive sliding puzzle game for both 8 and 15 numbers. An 8-number puzzle has a square-framed board consisting of 8 square tiles, numbered 1 to 8, initially placed in random order, while a 15-number puzzle there are 15 numbered square tiles, from 1 to 15, as shown above respectively. The game board has an empty space where one of adjacent tiles slides to. The objective of the game is to re-arrange the tiles into a sequential order by their numbers (left to right, top to bottom) by repeatedly making sliding moves (left, right, up or down). The following figure shows an example of one 8-number puzzle where “INITIAL” is the starting point of the game, and the player needs to repeatedly slide one adjacent tile, one at a time, to the unoccupied space (the empty space) until all numbers appear sequentially, ordered from left to right, top to bottom, shown as “FINAL”.
如何用python编写sliding puzzlz(帮写代码)
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
10条回答 默认 最新
关注 代码: #import the necessary libraries import random #define the size of the puzzle size = 3 #create a list of numbers from 0 to size^2 - 1 numbers = [i for i in range(size**2)] #shuffle the list random.shuffle(numbers) #create a 2D array of size x size puzzle = [[0 for i in range(size)] for j in range(size)] #fill the array with the shuffled numbers for i in range(size): for j in range(size): puzzle[i][j] = numbers[i*size + j] #function to check if the puzzle is solved def is_solved(puzzle): for i in range(size): for j in range(size): if puzzle[i][j] != i*size + j: return False return True #function to move the empty tile def move_tile(puzzle, direction): #find the empty tile for i in range(size): for j in range(size): if puzzle[i][j] == 0: empty_tile = (i, j) break #check if the move is valid if direction == 'up': if empty_tile[0] == 0: return False else: puzzle[empty_tile[0]][empty_tile[1]] = puzzle[empty_tile[0] - 1][empty_tile[1]] puzzle[empty_tile[0] - 1][empty_tile[1]] = 0 return True elif direction == 'down': if empty_tile[0] == size - 1: return False else: puzzle[empty_tile[0]][empty_tile[1]] = puzzle[empty_tile[0] + 1][empty_tile[1]] puzzle[empty_tile[0] + 1][empty_tile[1]] = 0 return True elif direction == 'left': if empty_tile[1] == 0: return False else: puzzle[empty_tile[0]][empty_tile[1]] = puzzle[empty_tile[0]][empty_tile[1] - 1] puzzle[empty_tile[0]][empty_tile[1] - 1] = 0 return True elif direction == 'right': if empty_tile[1] == size - 1: return False else: puzzle[empty_tile[0]][empty_tile[1]] = puzzle[empty_tile[0]][empty_tile[1] + 1] puzzle[empty_tile[0]][empty_tile[1] + 1] = 0 return True #function to print the puzzle def print_puzzle(puzzle): for i in range(size): for j in range(size): print(puzzle[i][j], end=' ') print() #main loop while not is_solved(puzzle): print_puzzle(puzzle) direction = input('Enter the direction to move the empty tile (up/down/left/right): ') if move_tile(puzzle, direction): print('Tile moved!') else: print('Invalid move!') print('Puzzle solved!')
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
- ¥15 ETLCloud 处理json多层级问题
- ¥15 matlab中使用gurobi时报错
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么