所以我正在尋找代碼迷宮解決程序,我已經失敗了導入迷宮。這是我的代碼:從if語句中刪除IndexError - 迷宮解決軟件
def import_maze(filename):
temp = open(filename, 'r')
x, y = temp.readline().split(" ")
maze = [[0 for x in range(int(y))] for x in range(int(x))]
local_counter, counter, startx, starty = 0, 0, 0, 0
temp.readline()
with open(filename) as file:
maze = [[letter for letter in list(line)] for line in file]
for i in range(1, int(y)):
for z in range(0, int(x)):
if maze[i][z] == '#':
local_counter += 1
if local_counter < 2 and maze[i][z] == " ":
counter += 1
if maze[i][z] == 'K':
startx, starty = i, z
local_counter = 0
return maze, startx, starty, counter
maze, startx, starty, counter = import_maze("kassiopeia0.txt")
print(counter, "\n", startx, ":", starty, "\n", maze)
解釋一下:local_counter是「顯示」迷宮的邊界。所以我可以計算數組中的空白元素。他們的數量將被保存在櫃檯上,至於我需要爲我補充基礎。 以及錯誤消息我revieve是:
C:\Python34\python.exe C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py
Traceback (most recent call last):
File "C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py", line 27, in <module>
maze, startx, starty, counter = import_maze("kassiopeia0.txt")
File "C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py", line 16, in import_maze
if maze[i][z] == '#':
IndexError: list index out of range
Process finished with exit code 1
最後這裏是kassiopeia0.txt文件:
6 9
#########
# # #
# # # #
# K # #
# # #
#########
Sry基因,我的英語。
嘿,非常感謝你,也感謝@Luke! = d –