先來展示效果圖:
我們先分析所需的函式庫:
既然是生成器,每次產生的迷宮一模一樣顯然是說不過去的。因此,我們不可避免地要使用隨機數(Random庫)。迷宮一定是要繪製的,所以需要有GUI庫或繪圖庫,這裡我使用Pygame(Tkinter或Turtle其實都可以做到,但畢竟Pygame比較順手)。在使用Pygame時似乎也需要Sys(用於退出程序),不過即使沒有使用,也不會有太大影響。下面是對原句的重寫: 接下來我們有Tkinter.filedialog,它主要用於詢問迷宮生成後的保存路徑。畢竟,生成好的迷宮需要保存下來。當然,用Time加上計時器似乎是錦上添花。
於是,就有:
#coding:utf-8 import contextlib with contextlib.redirect_stdout(None): import pygame import random import sys import time from tkinter.filedialog import *
這裡要說明的是,由於導入Pygame時會輸出版本資訊等很多內容(這很影響美感),我們需要使用Contextlib阻止它輸出。
接下來,我們需要問一些參數:
a=int(input("列数:")) b=int(input("行数:")) l=int(input("大小:")) saveit=input("是否保存:")
然後,就要執行生成迷宮的程式了。同時,我們有必要計錄時間(相當於開啟計時器):
print("生成中...") e = time.time()
然後就是正式生成迷宮。在介紹這部分程式碼之前,我們需要先了解演算法:
第一步,產生一個由迷宮單元(白格)和牆(黑格)組成的網格。迷宮單元數量在行中的數目等於迷宮的列數,而在列中的數目則等於迷宮的行數。讓左上角的迷宮單元作為起點,右下角的迷宮單元作為終點,並且拆除起點左側和終點右側的牆壁,如圖所示:
##第二步,參觀各迷宮單元。將起點標記為當前迷宮單元,當存在未被訪問的迷宮單元(凡是曾經成為過當前迷宮單元的迷宮單元,都視為已訪問)時,重複執行:alist = [] aa=0 need=[] for j in range(2*a+1): if aa==0: aa = 1 alistone = [] for i in range(2*b+1): alistone.append(1) alist.append(alistone) else: aa=0 alistone = [] bb=0 for i in range(2*b+1): if bb==0: bb=1 alistone.append(1) else: bb = 0 need.append((j,i)) alistone.append(0) alist.append(alistone) alist[0][1]=0 alist[-1][-2]=0
x=1 y=1 need.remove((1, 1)) listing=[] while len(need)>0: aroundit=[] try: if x-2<0: print(1+"1") alist[x-2][y]=0 if (x-2,y) in need: aroundit.append("alist[x-1][y],x=(0,x-2)") except: while False: print() try: alist[x+2][y]=0 if (x+2,y) in need: aroundit.append("alist[x+1][y],x=(0,x+2)") except: while False: print() try: alist[x][y+2]=0 if (x,y+2) in need: aroundit.append("alist[x][y+1],y=(0,y+2)") except: while False: print() try: if y-2<0: print(1+"1") alist[x][y-2]=0 if (x,y-2) in need: aroundit.append("alist[x][y-1],y=(0,y-2)") except: while False: print() if len(aroundit)>0: listing.append((x,y)) exec(random.choice(aroundit)) need.remove((x, y)) else: x,y=listing[-1] listing.pop()
print("完成!用时{}秒".format(time.time()-e))
if saveit=="1": ccc = askdirectory() h="" bbbbb=1 while True: try: open("{}/{}×{}迷宫{}.png".format(ccc,a,b,h),"r") h="({})".format(bbbbb) except: break bbbbb+=1
pygame.init() icon=pygame.image.load("迷宫.png") pygame.display.set_icon(icon) screen=pygame.display.Info() screen = pygame.display.set_mode((l*(2*a+1),l*(2*b+1))) pygame.display.set_caption('迷宫') screen.fill("white") c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') for i in range(2*a+1): for j in range(2*b+1): if alist[i][j]==0: screen.blit(c, (i*l, j*l)) elif alist[i][j]==1: screen.blit(d, (i*l, j*l)) pygame.display.flip() if saveit=="1": pygame.image.save(screen, "{}/{}×{}迷宫{}.png".format(ccc, a, b, h)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()
这里主要是Pygame的基本设置,并将表格内容图像化。方格中每个数字代表一个方块,数字的大小则决定了方块的颜色,数值在表格中的位置决定了方块放置的位置。就这样,我们呢将表格完全转化成了图像。当然,我们还需要使用pygame.image.save()函数将图像另存为图片文件。
这样,这个生成器似乎完成了。
它运行良好,但当迷宫比较复杂时,暴露出一个问题(下图是100×100的迷宫):
由于正确路径过于曲折,在复杂度较高时,这个迷宫的难度会变得极高!
难度高,在某方面上讲,的确是好事。如果你自己无法找到正确的路线,那么当你展示这个迷宫给朋友看时,感觉会很失落吧?
因此,一个寻路算法变得非常有必要。
寻路算法的大体思路:
在生成的迷宫中,白格为路,黑格为墙。将起点设置为当前位置,重复执行直到终点成为当前位置:
将当前位置标记为正确路径;
将周围未标记的路加入一个表格;
如果表格不空:
将当前位置入栈;
从表格中随机选择一条路,并将其设为当前位置;
如果表格是空的:
栈顶的路出栈;
将其设为当前位置;
通过这个算法,我们可以试出正确的路径(如图):
代码的实现:
x2=0 y2=1 listing2=[] while not(alist[-1][-2]==2): alist[x2][y2]=3 around2=[] try: if x2-1<0: print(1+"1") if alist[x2-1][y2]==0: around2.append("x2=x2-1") except: while False: print() try: if alist[x2+1][y2]==0: around2.append("x2=x2+1") except: while False: print() try: if alist[x2][y2+1]==0: around2.append("y2=y2+1") except: while False: print() try: if y2-1<0: print(1+"1") if alist[x2][y2-1]==0: around2.append("y2=y2-1") except: while False: print() if len(around2)>0: listing2.append((x2,y2)) exec(random.choice(around2)) else: alist[x2][y2]=2 x2,y2=listing2[-1] listing2.pop() alist[-1][-2]=3 for i in range(len(alist)): for j in range(len(alist[0])): if alist[i][j]==2: alist[i][j]=0
同时,图像绘制的过程也要作出一些改动,以显示正确路径:
if saveit=="1": ccc = askdirectory() h="" bbbbb=1 while True: try: open("{}/{}×{}迷宫{}.png".format(ccc,a,b,h),"r") open("{}/{}×{}迷宫(正确线路){}.png".format(ccc,a,b,h),"r") h="({})".format(bbbbb) except: break bbbbb+=1 pygame.init() icon=pygame.image.load("迷宫.png") pygame.display.set_icon(icon) screen=pygame.display.Info() screen = pygame.display.set_mode((l*(2*a+1),l*(2*b+1))) pygame.display.set_caption('迷宫') screen.fill("white") if saveit=="1": c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='white') for i in range(2 * a + 1): for j in range(2 * b + 1): if alist[i][j] == 0: screen.blit(c, (i * l, j * l)) elif alist[i][j] == 1: screen.blit(d, (i * l, j * l)) else: screen.blit(f, (i * l, j * l)) pygame.image.save(screen, "{}/{}×{}迷宫{}.png".format(ccc, a, b, h)) c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='red') for i in range(2 * a + 1): for j in range(2 * b + 1): if alist[i][j] == 0: screen.blit(c, (i * l, j * l)) elif alist[i][j] == 1: screen.blit(d, (i * l, j * l)) else: screen.blit(f, (i * l, j * l)) pygame.image.save(screen, "{}/{}×{}迷宫(正确线路){}.png".format(ccc, a, b, h)) c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='white') for i in range(2*a+1): for j in range(2*b+1): if alist[i][j]==0: screen.blit(c, (i*l, j*l)) elif alist[i][j]==1: screen.blit(d, (i*l, j*l)) else: screen.blit(f,(i*l, j*l)) pygame.display.flip() aaaaaaa = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if aaaaaaa == 1: aaaaaaa = 0 c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='white') for i in range(2 * a + 1): for j in range(2 * b + 1): if alist[i][j] == 0: screen.blit(c, (i * l, j * l)) elif alist[i][j] == 1: screen.blit(d, (i * l, j * l)) else: screen.blit(f, (i * l, j * l)) pygame.display.flip() else: aaaaaaa = 1 c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='red') for i in range(2 * a + 1): for j in range(2 * b + 1): if alist[i][j] == 0: screen.blit(c, (i * l, j * l)) elif alist[i][j] == 1: screen.blit(d, (i * l, j * l)) else: screen.blit(f, (i * l, j * l)) pygame.display.flip()
通过这些改动,显示正确路径的效果就实现了。当生成迷宫完成后,窗口显示的是没有正确路径的迷宫。但是,当单击窗口时,正确的路径便会显示出来,再次单击即可隐藏。
刚刚那张100×100的迷宫,其正确路径是:
完整的代码:
#coding:utf-8 import contextlib with contextlib.redirect_stdout(None): import pygame import random import sys import time from tkinter.filedialog import * a=int(input("列数:")) b=int(input("行数:")) l=int(input("大小:")) saveit=input("是否保存:") print("生成中...") e = time.time() alist = [] aa=0 need=[] for j in range(2*a+1): if aa==0: aa = 1 alistone = [] for i in range(2*b+1): alistone.append(1) alist.append(alistone) else: aa=0 alistone = [] bb=0 for i in range(2*b+1): if bb==0: bb=1 alistone.append(1) else: bb = 0 need.append((j,i)) alistone.append(0) alist.append(alistone) alist[0][1]=0 alist[-1][-2]=0 x=1 y=1 need.remove((1, 1)) listing=[] while len(need)>0: aroundit=[] try: if x-2<0: print(1+"1") alist[x-2][y]=0 if (x-2,y) in need: aroundit.append("alist[x-1][y],x=(0,x-2)") except: while False: print() try: alist[x+2][y]=0 if (x+2,y) in need: aroundit.append("alist[x+1][y],x=(0,x+2)") except: while False: print() try: alist[x][y+2]=0 if (x,y+2) in need: aroundit.append("alist[x][y+1],y=(0,y+2)") except: while False: print() try: if y-2<0: print(1+"1") alist[x][y-2]=0 if (x,y-2) in need: aroundit.append("alist[x][y-1],y=(0,y-2)") except: while False: print() if len(aroundit)>0: listing.append((x,y)) exec(random.choice(aroundit)) need.remove((x, y)) else: x,y=listing[-1] listing.pop() x2=0 y2=1 listing2=[] while not(alist[-1][-2]==2): alist[x2][y2]=3 around2=[] try: if x2-1<0: print(1+"1") if alist[x2-1][y2]==0: around2.append("x2=x2-1") except: while False: print() try: if alist[x2+1][y2]==0: around2.append("x2=x2+1") except: while False: print() try: if alist[x2][y2+1]==0: around2.append("y2=y2+1") except: while False: print() try: if y2-1<0: print(1+"1") if alist[x2][y2-1]==0: around2.append("y2=y2-1") except: while False: print() if len(around2)>0: listing2.append((x2,y2)) exec(random.choice(around2)) else: alist[x2][y2]=2 x2,y2=listing2[-1] listing2.pop() alist[-1][-2]=3 for i in range(len(alist)): for j in range(len(alist[0])): if alist[i][j]==2: alist[i][j]=0 print("完成!用时{}秒".format(time.time()-e)) if saveit=="1": ccc = askdirectory() h="" bbbbb=1 while True: try: open("{}/{}×{}迷宫{}.png".format(ccc,a,b,h),"r") open("{}/{}×{}迷宫(正确线路){}.png".format(ccc,a,b,h),"r") h="({})".format(bbbbb) except: break bbbbb+=1 pygame.init() icon=pygame.image.load("迷宫.png") pygame.display.set_icon(icon) screen=pygame.display.Info() screen = pygame.display.set_mode((l*(2*a+1),l*(2*b+1))) pygame.display.set_caption('迷宫') screen.fill("white") if saveit=="1": c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='white') for i in range(2 * a + 1): for j in range(2 * b + 1): if alist[i][j] == 0: screen.blit(c, (i * l, j * l)) elif alist[i][j] == 1: screen.blit(d, (i * l, j * l)) else: screen.blit(f, (i * l, j * l)) pygame.image.save(screen, "{}/{}×{}迷宫{}.png".format(ccc, a, b, h)) c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='red') for i in range(2 * a + 1): for j in range(2 * b + 1): if alist[i][j] == 0: screen.blit(c, (i * l, j * l)) elif alist[i][j] == 1: screen.blit(d, (i * l, j * l)) else: screen.blit(f, (i * l, j * l)) pygame.image.save(screen, "{}/{}×{}迷宫(正确线路){}.png".format(ccc, a, b, h)) c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='white') for i in range(2*a+1): for j in range(2*b+1): if alist[i][j]==0: screen.blit(c, (i*l, j*l)) elif alist[i][j]==1: screen.blit(d, (i*l, j*l)) else: screen.blit(f,(i*l, j*l)) pygame.display.flip() aaaaaaa = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if aaaaaaa == 1: aaaaaaa = 0 c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='white') for i in range(2 * a + 1): for j in range(2 * b + 1): if alist[i][j] == 0: screen.blit(c, (i * l, j * l)) elif alist[i][j] == 1: screen.blit(d, (i * l, j * l)) else: screen.blit(f, (i * l, j * l)) pygame.display.flip() else: aaaaaaa = 1 c = pygame.Surface((l, l), flags=pygame.HWSURFACE) c.fill(color='white') d = pygame.Surface((l, l), flags=pygame.HWSURFACE) d.fill(color='black') f = pygame.Surface((l, l), flags=pygame.HWSURFACE) f.fill(color='red') for i in range(2 * a + 1): for j in range(2 * b + 1): if alist[i][j] == 0: screen.blit(c, (i * l, j * l)) elif alist[i][j] == 1: screen.blit(d, (i * l, j * l)) else: screen.blit(f, (i * l, j * l)) pygame.display.flip()
以上是Python如何實現迷宮生成器的詳細內容。更多資訊請關注PHP中文網其他相關文章!