Following instructions to solve a maze problem, I need to read from a file and then convert it into a matrix. Once this is done I need to replace every "#" (represents a wall) with -1, every " " (representing a corridor) with 0 and every "M" (represents a minotaur) with -2. I've managed to convert the file into a matrix but the latter half concerning the replace I am running into problems.
I am a beginner and have only been learning python for a couple months now(hence why my following code is probably painful to read). Thanks so much for any help!
The file being read contains the following:
###########
# # #
# ### # # #
# # # # #
# ##### # #
# # #
# ####### #
# # #
# ####### #
#
###########
Here is my code:
def loadmaze(filename):
f = open(filename)
lines = f.readlines()
COR = "0"
WALL = "-1"
MINO = "2"
new_maze = []
for i in range(len(lines)):
lines[i] = "*" + lines[i]
lines[i] = lines[i].strip("*").strip("\n")
new_maze.append(lines[i])
x = new_maze
con_maze = []
for i in range(len(x)):
z = list(x[i])
con_maze.append(z)
for i in range(len(con_maze)):
for j in range(len(con_maze[i])):
con_maze[i][j].replace("'#'", COR)
con_maze[i][j].replace(" ", WALL)
con_maze[i][j].replace("M", MINO)
return con_maze
print loadmaze("maze.txt")
import re
from pprint import pprint
def loadmaze(filename):
maze = []
with open(filename) as f:
for line in f:
with_delimiters = ''
for i,ch in enumerate(line):
with_delimiters += 'S' + ch
line = with_delimiters
inchars = '# M'
outchars = ['-1','0','-2']
for i,ch in enumerate(inchars):
line = line.replace(ch,outchars[i])
line = line.replace('\n','')
maze_col = line.split('S')
for i in maze_col:
if i == '':
maze_col.remove(i)
maze.append(map(int,maze_col))
return maze
pprint (loadmaze('maze.txt'))
output:
[[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[0, 0, 0, 0, 0, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1],
[-1, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1],
[-1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1],
[-1, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1],
[-1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1],
[-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]]
Following instructions to solve a maze problem, I need to read from a file and then convert it into a matrix. Once this is done I need to replace every "#" (represents a wall) with -1, every " " (representing a corridor) with 0 and every "M" (represents a minotaur) with -2. I've managed to convert the file into a matrix but the latter half concerning the replace I am running into problems.
I am a beginner and have only been learning python for a couple months now(hence why my following code is probably painful to read). Thanks so much for any help!
The file being read contains the following:
###########
# # #
# ### # # #
# # # # #
# ##### # #
# # #
# ####### #
# # #
# ####### #
#
###########
Here is my code:
def loadmaze(filename):
f = open(filename)
lines = f.readlines()
COR = "0"
WALL = "-1"
MINO = "2"
new_maze = []
for i in range(len(lines)):
lines[i] = "*" + lines[i]
lines[i] = lines[i].strip("*").strip("\n")
new_maze.append(lines[i])
x = new_maze
con_maze = []
for i in range(len(x)):
z = list(x[i])
con_maze.append(z)
for i in range(len(con_maze)):
for j in range(len(con_maze[i])):
con_maze[i][j].replace("'#'", COR)
con_maze[i][j].replace(" ", WALL)
con_maze[i][j].replace("M", MINO)
return con_maze
print loadmaze("maze.txt")
import re
from pprint import pprint
def loadmaze(filename):
maze = []
with open(filename) as f:
for line in f:
with_delimiters = ''
for i,ch in enumerate(line):
with_delimiters += 'S' + ch
line = with_delimiters
inchars = '# M'
outchars = ['-1','0','-2']
for i,ch in enumerate(inchars):
line = line.replace(ch,outchars[i])
line = line.replace('\n','')
maze_col = line.split('S')
for i in maze_col:
if i == '':
maze_col.remove(i)
maze.append(map(int,maze_col))
return maze
pprint (loadmaze('maze.txt'))
output:
[[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[0, 0, 0, 0, 0, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1],
[-1, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1],
[-1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1],
[-1, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1],
[-1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1],
[-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]]
0 commentaires:
Enregistrer un commentaire