如何在python中打印保存为字典的迷宫?
我有一个函数,可以从.txt文件中读取迷宫并将其转换为字典。字典的键是迷宫的单元,值是基点。“True”值表示墙位于北面,以此类推。 这就是我的函数所做的:如何在python中打印保存为字典的迷宫?,python,dictionary,printing,maze,Python,Dictionary,Printing,Maze,我有一个函数,可以从.txt文件中读取迷宫并将其转换为字典。字典的键是迷宫的单元,值是基点。“True”值表示墙位于北面,以此类推。 这就是我的函数所做的: def importa(maze): lab=open(maze, 'r') l=list(lab.readlines()) if len(l)==0: return None righe=len(l) colonne=(len(l[0])-1) maze=dict()
def importa(maze):
lab=open(maze, 'r')
l=list(lab.readlines())
if len(l)==0:
return None
righe=len(l)
colonne=(len(l[0])-1)
maze=dict()
for r in range(1,righe-1,2):
for c in range(1,colonne-1,2):
nord=l[r-1][c]=='*'
sud=(l[r+1][c]=='*')
est=(l[r][c+1]=='*')
ovest=(l[r][c-1]=='*')
maze[(r//2,c//2)]=[{'N':nord,'S':sud,'E':est,'O':ovest},'']
#la stringa vuota è lo stato della cella
return maze
例如,迷宫是:
{(0, 0): [{'N': True, 'S': False, 'E': True, 'O': True}, ''], (0, 1): [{'N': True, 'S': False, 'E': True, 'O': True}, ''], (0, 2): [{'N': True, 'S': True, 'E': False, 'O': True}, ''], (0, 3): [{'N': True, 'S': False, 'E': False, 'O': False}, ''], (0, 4): [{'N': True, 'S': True, 'E': True, 'O': False}, ''], (1, 0): [{'N': False, 'S': True, 'E': False, 'O': True}, ''], (1, 1): [{'N': False, 'S': False, 'E': False, 'O': False}, ''], (1, 2): [{'N': True, 'S': False, 'E': False, 'O': False}, ''], (1, 3): [{'N': False, 'S': True, 'E': True, 'O': False}, ''], (1, 4): [{'N': True, 'S': False, 'E': False, 'O': True}, ''], (2, 0): [{'N': True, 'S': True, 'E': False, 'O': True}, ''], (2, 1): [{'N': False, 'S': False, 'E': True, 'O': False}, ''], (2, 2): [{'N': False, 'S': False, 'E': False, 'O': True}, ''], (2, 3): [{'N': True, 'S': True, 'E': False, 'O': False}, ''], (2, 4): [{'N': False, 'S': False, 'E': True, 'O': False}, ''], (3, 0): [{'N': True, 'S': False, 'E': True, 'O': True}, ''], (3, 1): [{'N': False, 'S': True, 'E': True, 'O': True}, ''], (3, 2): [{'N': False, 'S': True, 'E': False, 'O': True}, ''], (3, 3): [{'N': True, 'S': False, 'E': True, 'O': False}, ''], (3, 4): [{'N': False, 'S': False, 'E': True, 'O': True}, ''], (4, 0): [{'N': False, 'S': True, 'E': False, 'O': True}, ''], (4, 1): [{'N': True, 'S': True, 'E': False, 'O': False}, ''], (4, 2): [{'N': True, 'S': True, 'E': False, 'O': False}, ''], (4, 3): [{'N': False, 'S': True, 'E': True, 'O': False}, ''], (4, 4): [{'N': False, 'S': True, 'E': True, 'O': True}, '']}
我需要编写一个函数来打印迷宫,如下所示:
有人能帮我吗
我试图建立一个功能,但它似乎很长
def stampa(L):
labirinto=[]
for r in range(righe_colonne(L)[0]):
riga=[]
for cella in celle(L):
if cella[0]==r:
if L[cella][0]['O']==True:
riga.append('| ')
if L[cella][0]['O']==False:
riga.append(' ')
if cella[1]==righe_colonne(L)[1]-1:
if L[cella][0]['E']==True:
riga.append('|')
if L[cella][0]['E']==False:
riga.append(' ')
labirinto.append(''.join(str(x) for x in riga))
#cosi ho le righe pari, quelle che contengono le celle.
#ora voglio le righe fra le celle
labirinto2=[]
for r in range(righe_colonne(L)[0]):
riga=['+']
for cella in celle(L):
if cella[0]==r:
if L[cella][0]['N']==True:
riga.append('---+')
if L[cella][0]['N']==False:
riga.append(' +')
labirinto2.append(''.join(str(x) for x in riga))
m=''
for e in range(0, len(labirinto)):
m+=labirinto2[e]+'\n'+labirinto[e]+'\n'
#aggiungo l'ultima riga
riga=[]
for cella in celle(L):
if cella[0]==righe_colonne(L)[0]-1:
if L[cella][0]['S']==True:
riga.append('+---')
if L[cella][0]['S']==False:
riga.append(' ')
riga.append('+')
m+=(''.join(str(x) for x in riga))
m+='\n'
print(m)
为此,您可以使用
matplotlib
:
import matplotlib.pyplot as plt
for i, j in maze:
walls, _ = maze[i, j]
if walls['N']:
plt.plot([i, i+1], [j+1, j+1], 'b-')
if walls['E']:
plt.plot([i+1, i+1], [j, j+1], 'b-')
if walls['S']:
plt.plot([i, i+1], [j, j], 'b-')
if walls['O']:
plt.plot([i, i], [j, j+1], 'b-')
plt.show()
对于您的示例,这给出了:
到目前为止,您有什么?您想用什么类型的媒体来打印它?在浏览器中使用HTML/CSS?在控制台中作为纯文本?另外:如果两个相邻单元格有边框,如何处理这种情况?例如,
{(0,0):[{“E”:True}],(0,1):[{“W”:True}]}
。在这种情况下,边界应该是双边界吗?不,边界是一。我需要写一个函数,在pythonOther解决方案中打印迷宫,而不使用matplotlib?