对递归回溯生成随机迷宫的演示

回顾: 

[python实现] 递归回溯(深度优先)构造随机迷宫_☆迷茫狗子的秘密基地☆-CSDN博客对递归回溯生成随机迷宫的演示https://blog.csdn.net/qq_39391544/article/details/121306611


在上次的基础上稍加改动,可以更加直观地欣赏整个过程

美中不足的是我想不停地原地输出并刷新,可惜找了很多文章都没能达到理想的效果,

希望有大佬有实现过类似的情况可以指点一二 hhh

对递归回溯生成随机迷宫的演示

 

对递归回溯生成随机迷宫的演示

 

# # -*- coding: utf-8 -*-
# """
# Created on Thu Nov 18 13:33:53 2021

# @author: Knight
# """

import sys
import time
import os
from enum import Enum
from random import randint, choice


class DIRECTOIN(Enum):
    UP = 0,
    LEFT = 1,
    DOWN = 2,
    RIGHT = 3,


class TYPE(Enum):
    EMPTY = 0,
    BLOCK = 1


class Map():
    def __init__(self, width, height):
        self.width = width
        self.height = height

        # 全部初始化成墙体
        self.map = [[1 for x in range(self.width)] for y in range(self.height)]

    def setMap(self, x, y, value):
        if value == TYPE.EMPTY:
            self.map[y][x] = 0
        elif value == TYPE.BLOCK:
            self.map[y][x] = 1

    def checkVisited(self, x, y):
        return self.map[y][x] != 1

    def showMap(self):
        for row in self.map:
            mp = ''
            for item in row:
                if item == 0:
                    mp += '  '
                elif item == 1:
                    mp += ' @'
            print(mp)


def checkPosition(map, x, y, w, h, waitingList):
    direction = []
    if y > 0:
        if not map.checkVisited(2 * x + 1, 2 * (y - 1) + 1):
            direction.append(DIRECTOIN.UP)
    if x > 0:
        if not map.checkVisited(2 * (x - 1) + 1, 2 * y + 1):
            direction.append(DIRECTOIN.LEFT)
    if y < h - 1:
        if not map.checkVisited(2 * x + 1, 2 * (y + 1) + 1):
            direction.append(DIRECTOIN.DOWN)
    if x < w - 1:
        if not map.checkVisited(2 * (x + 1) + 1, 2 * y + 1):
            direction.append(DIRECTOIN.RIGHT)

    if len(direction):
        # 随机选择方向
        direc = choice(direction)
        if direc == DIRECTOIN.UP:
            map.setMap(2 * x + 1, 2 * (y - 1) + 1, TYPE.EMPTY)
            map.setMap(2 * x + 1, 2 * y, TYPE.EMPTY)
            waitingList.append((x, y - 1));
        elif direc == DIRECTOIN.LEFT:
            map.setMap(2 * (x - 1) + 1, 2 * y + 1, TYPE.EMPTY)
            map.setMap(2 * x, 2 * y + 1, TYPE.EMPTY)
            waitingList.append((x - 1, y));
        elif direc == DIRECTOIN.DOWN:
            map.setMap(2 * x + 1, 2 * (y + 1) + 1, TYPE.EMPTY)
            map.setMap(2 * x + 1, 2 * y + 2, TYPE.EMPTY)
            waitingList.append((x, y + 1));
        elif direc == DIRECTOIN.RIGHT:
            map.setMap(2 * (x + 1) + 1, 2 * y + 1, TYPE.EMPTY)
            map.setMap(2 * x + 2, 2 * y + 1, TYPE.EMPTY)
            waitingList.append((x + 1, y));

        map.showMap()
        sys.stdout.flush()
        time.sleep(0.05)
        return True
    else:
        return False


def recursive(map, w, h):
    x0, y0 = (randint(0, w - 1)), (randint(0, h - 1))
    map.setMap(2 * x0 + 1, 2 * y0 + 1, TYPE.EMPTY)

    waitingList = []
    waitingList.append((x0, y0))
    cnt = 0
    while len(waitingList):
        if not checkPosition(map, waitingList[-1][0], waitingList[-1][1], w, h, waitingList):
            waitingList.remove(waitingList[-1])


# def setStartEnd(map):
#     x0, y0 =

# 开始构造迷宫
def startCreateMap(map):
    recursive(map, map.width // 2, map.height // 2)
    # setStartEnd(map)


def run():
    WIDTH = 31
    HEIGHT = 37
    map = Map(WIDTH, HEIGHT)
    startCreateMap(map)



if __name__ == "__main__":
    run()


上一篇:FWFT FIFO的读取


下一篇:86. Partition List