求助,程序运行错误,不能得到想要的结果,即随机生成迷宫,再遍历的GUI界面。麻烦前辈指点 2C
 import sys
from random import randrange, shuffle
from tkSimpleDialog import askstring
from Tkinter import *
sys.setrecursionlimit(100000)
BOTTOM_WALL = 0
RIGHT_WALL = 1
VISITED = 2
VISITE = 2
E,S,W,N = 0, 1, 2, 3
DIRECTION = [(0,1),(1,0),(0,-1),(-1,0)]

class Maze:
    def __init__(self, row, col):
        self.row , self.col = row, col
        self.maze = [[[1, 1, 0] for c in range(col)] for r in range(row)]
        self.makepath(randrange(row), randrange(col))
        self.walk(0,0)
        self.s = []


    def makepath(self, r, c, direct = None):

        maze = self.maze

        maze[r][c][VISITED] = 1

        if direct == 3: maze[r][c][BOTTOM_WALL] = 0
        if direct == 1: maze[r-1][c][BOTTOM_WALL] = 0
        if direct == 2: maze[r][c][RIGHT_WALL] = 0
        if direct == 0: maze[r][c-1][RIGHT_WALL] = 0

        directs = []
        if r > 0: directs.append(3)
        if r < self.row - 1: directs.append(1)
        if c > 0: directs.append(2)
        if c < self.col - 1: directs.append(0)

        shuffle(directs)

        for d in directs:

            dr, dc = DIRECTION[d]

            if not maze[r+dr][c+dc][VISITED]:
                self.makepath(r+dr, c+dc, d)


    def draw(self, size, canvas):
        d = 5

        canvas.config(width = d*2+self.col*size, height = d*2+self.row*size)
        #canvas.itemconfig(canvas, fill = 'red')
        #canvas.config(text = 'Please input the maze size')
        #canvas = Button(None, text = 'exit', command = sys.exit)
        line = canvas.create_line
        box = canvas.create_rectangle

        line(d,d,self.col*size+d,d)
        line(d,d,d,self.row*size+d)

        for r in range(self.row):
            for c in range(self.col):

                if self.maze[r][c][BOTTOM_WALL]:
                    line(c*size+d, r*size+size+d, c*size+size+d, r*size+size+d)

                if self.maze[r][c][RIGHT_WALL]:
                    line(c*size+size+d, r*size+d, c*size+size+d, r*size+size+d)
                if self.maze[r][c][VISITE]:
                    box(c*size+size+d, r*size+d, c*size+size+d, r*size+size+d,fill = 'red')

    def walk(self, r, c, direct = None):
        maze = self.maze
        maze[r][c][VISITE] = 1
        directs = []
        if maze[r][c][BOTTOM_WALL] == 1:
            directs.append(1)
        if maze[r-1][c][BOTTOM_WALL] == 1:
            directs.append(3)
        if maze[r][c][RIGHT_WALL] == 1:
            directs.append(0)
        if maze[r][c-1][RIGHT_WALL] == 1:
            directs.append(2)
        shuffle(directs)
        for d in directs:
            dr, dc = DIRECTION[d]
            if not maze[r+dr][c+dc][VISITE]:
                self.makepath(r+dr, c+dc, d)

root = Tk()

root.title("Maze")
canvas = Canvas(root)
canvas.pack(expand = YES, fill = BOTH)
size = askstring("Maze size", "Please Maze size", initialvalue="25 25")
size = [int(x) for x in size.split()]
Maze(*size).draw(10, canvas)
root.mainloop()

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问