0

I have this task where I must print a maze that also provides the user's position. However this position can be dependent on the generated position from another function. What I mean is that the position can have many different possibilites.

I tried to use slicing with a combination of the .replace method to be able to change the character to the user's position symbolled as 'A'.

See the code below, am I doing something wrong here?

def print_maze(maze, position):
"""
Returns maze string from text file and position of the player

print_maze(str, int) -> object
"""

p1 = position[0]
p2 = position[1]
position = position_to_index((p1,p2), len(maze))

for line in maze:
    maze = maze[:position].replace(' ', 'A') + maze[position:]

    for line in maze:
        maze.strip().split('\n')

print(maze)

For my result so far, all I get is:

>>> maze = load_maze('maze1.txt')
>>> print_maze(maze, (1,1))
#####
#AAZ#
#A###
#AAP#
#####
Pfraylen
  • 1
  • 1

1 Answers1

0

It seems like you're making it harder than it need be. Rather than load the maze as one string, read it into an array. Do your .strip() one in load_maze rather than every call to print-maze():

def load_maze(filename):
    """
    Returns maze string from text file

    load_maze(str) -> list
    """
    maze = []
    with open(filename) as file:
        for line in file:
            maze.append(line.strip())
    return maze

def print_maze(maze, position):
    """
    Prints maze with position of the player

    print_maze(str, (int, int)) -> None
    """

    (x, y) = position

    for row, line in enumerate(maze):
        if row == y:
            print(line[:x] + 'A' + line[x + 1:])
        else:
            print(line)

maze = load_maze('maze1.txt')

print_maze(maze, (1, 1))
cdlane
  • 40,441
  • 5
  • 32
  • 81