我正在编写一个程序来读取由文件中的符号组成的迷宫,该文件将每个符号存储在一个对象中,然后将该对象存储在一个2D数组中,在从文件中提取符号的位置,然后存储在另一个不同类型的对象中。然而,当它到达应该读取文件并进行比较的部分时,它忽略了代码。我知道这一点,因为我已经注释掉了在数组中存储对象的函数,代码运行并产生相同的输出。这两个类分别称为Position和Maze。
def set_maze_map(self, position, rows, columns):
self.maze_map[rows][columns] = position
def read_maze(filename):
rows_current = 0
columns_current = 0
lines_list = []
chars_list = []
with open(filename, 'r') as f1:
for line in f1:
lines_list.append(line)
for char in line:
chars_list.append(char)
rows_total = len(lines_list)
if (len(lines_list) == 0):
columns_total = 0
elif (len(lines_list) == 1):
columns_total = (len(chars_list)/len(lines_list))
else:
columns_total = (len(chars_list)/len(lines_list)) - 1
index_last_line = rows_total - 1
index_last_char = columns_total - 1
maze_obj = Maze(int(rows_total), int(columns_total))
for line in f1:
for char in line:
match (char):
case " ":
maze_obj.set_maze_map(Position(False, False, False, False, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "╴":
if (columns_current == 0):
p = Position(False, True, False, False, True)
maze_obj.maze_map[rows_current][columns_current] = p
else:
p = Position(False, True, False, False, False)
maze_obj.maze_map[rows_current][columns_current] = p
columns_current = columns_current + 1
break
case "╷":
if (rows_current == int(index_last_line)):
maze_obj.set_maze_map(Position(False, False, True, False, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(False, False, True, False, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "┐":
if (columns_current == 0) or (rows_current == int(index_last_line)):
maze_obj.set_maze_map(Position(False, False, True, True, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(False, False, True, True, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "╶":
if (columns_current == int(index_last_char)):
maze_obj.set_maze_map(Position(False, True, False, False, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(False, True, False, False, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "─":
if (columns_current == int(index_last_char)) or (columns_current == 0):
maze_obj.set_maze_map(Position(False, True, False, True, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(False, True, False, True, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "┌":
if (columns_current == int(index_last_char)) or (rows_current == int(index_last_line)):
maze_obj.set_maze_map(Position(False, True, True, False, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(False, True, True, False, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "┬":
if (columns_current == 0) or (columns_current == int(index_last_char)) or (rows_current == int(index_last_line)):
maze_obj.set_maze_map(Position(False, True, True, True, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(False, True, True, True, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "╵":
if (rows_current == 0):
maze_obj.set_maze_map(Position(True, False, False, False, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(True, False, False, False, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "┘":
if (rows_current == 0) or (columns_current == 0):
maze_obj.set_maze_map(Position(True, False, False, True, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(True, False, False, True, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "│":
if (rows_current == 0) or (rows_current == int(index_last_line)):
maze_obj.set_maze_map(Position(True, False, True, False, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(True, False, True, False, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "┤":
if (columns_current == 0) or (rows_current == 0) or (rows_current == int(index_last_line)):
maze_obj.set_maze_map(Position(True, False, True, True, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(True, False, True, True, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "└":
if (rows_current == 0) or (columns_current == int(index_last_char)):
maze_obj.set_maze_map(Position(True, True, False, False, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(True, True, False, False, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "┴":
if (rows_current == 0) or (columns_current == 0) or (columns_current == int(index_last_char)):
maze_obj.set_maze_map(Position(True, True, False, True, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(True, True, False, True, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "├":
if (rows_current == 0) or (columns_current == int(index_last_char)) or (rows_current == int(index_last_line)):
maze_obj.set_maze_map(Position(True, True, True, False, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(True, True, True, False, False), rows_current, columns_current)
columns_current = columns_current + 1
break
case "┼":
if (rows_current == 0) or (rows_current == int(index_last_line)) or (columns_current == 0) or (columns_current == int(index_last_char)):
maze_obj.set_maze_map(Position(True, True, True, True, True), rows_current, columns_current)
else:
maze_obj.set_maze_map(Position(True, True, True, True, False), rows_current, columns_current)
columns_current = columns_current + 1
break
rows_current = rows_current + 1
columns_current = 0
return maze_obj
它没有忽略任何东西。您的文件迭代器已耗尽。
与其尝试多次遍历for line in f1
,不如尝试在第一次遍历文件
for line in lines_list
你甚至可以取消缩进chars_list.append(char)
之后的每一行,因为你不再需要这个文件了