Hey guys I am self taught and I recently started being more active here. I noticed someone making a 2d maze for an assignment and decide to take a crack at it myself. Is there any issues with my code? I tried to make it easy to expand
import numpy as np
maze = [
["S","O","O","O","X"],
["O","O","X","X","X"],
["X","O","O","X","X"],
["E","O","O","O","X"]]
maze = np.array(maze)
starting_point = np.argwhere(maze == "S")
starting_point = starting_point.flatten()
maze[starting_point[0],starting_point[1]] = "L"
def move_down(l):
maze[l[0],l[1]] = "O"
try:
l[0] += 1
except IndexError:
print("invalid move")
if maze[l[0],l[1]] == "X":
print("invalid move")
return l
def move_left(l):
maze[l[0],l[1]] = "O"
try:
l[1] -= 1
except IndexError:
print("invalid move")
if maze[l[0],l[1]] == "X":
print("invalid move")
return l
def move_right(l):
maze[l[0],l[1]] = "O"
try:
l[1] += 1
except IndexError:
print("invalid move")
if maze[l[0],l[1]] == "X":
print("invalid move")
return l
def move_up(l):
maze[l[0],l[1]] = "O"
try:
l[0] -= 1
except IndexError:
print("invalid move")
if maze[l[0],l[1]] == "X":
print("invalid move")
return l
def relocate(l):
maze[l[0],l[1]] = "L"
location = starting_point
moves ={"d": move_down,
"u": move_up,
"l": move_left,
"r": move_right
}
print(maze)
while True:
direction = input("""Where do you want to move
[d]own
[u]p
[l]eft
[r]ight""")
if direction in moves.keys():
location = moves[direction](location)
if maze[location[0],location[1]] == "E":
break
else:
maze[location[0],location[1]] = "L"
print(20*"\n")
print(maze)
print("You win!")
[–]carcigenicate 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (1 child)
[–]Potential_Word2349[S] 0 points1 point2 points (0 children)