m0_75106116 2023-02-21 15:49 采纳率: 100%
浏览 129
已结题

如何用python编写sliding puzzlz(帮写代码)

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”.

img

  • 写回答

10条回答 默认 最新

  • 特创数字科技 阿克苏市特创数字科技中心官方账号 2023-02-22 23:50
    关注
    
    代码:
    #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!')
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

问题事件

  • 系统已结题 3月8日
  • 已采纳回答 2月28日
  • 修改了问题 2月21日
  • 修改了问题 2月21日
  • 展开全部

悬赏问题

  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭