I got stuck in these 2 parts of my homework. its about \"maze\" exploring. and i
ID: 3641285 • Letter: I
Question
I got stuck in these 2 parts of my homework. its about "maze" exploring. and i couldnt get anyfurther with it :(
2.2.2 Getting an Adjacent Position
get_position_in_direction(position, direction) takes a row, column
pair representing the position of a square, and a direction character (one of
NSEW) and returns the position of the adjacent square in the given direction.
For this function you do not check if that would be a legal move.
For example :
>>> get_position_in_direction((2,3), 'E')
(2, 4)
>>> get_position_in_direction((2,3), 'S')
(3, 3)
>>>
2.2.3 Make a Move
move(maze, position, direction) takes, as arguments, the maze (as a
list of lists), a position of a square and a direction and returns a triple of the
form (can_move, is_finished, new_position) where can_move is True
i the move is legal; is_finished is True i this move changes to the nish
square; and new_position is the position after the move. If the move is
illegal then the triple returned should be (False, False, position).
For example:
>>> maze = load_maze('maze1.txt')
>>> move(maze, (1,1), 'S')
(True, False, (2, 1))
>>> move(maze, (1,1),'E')
(False, False, (1, 1))
>>> move(maze, (2,3),'N')
(True, True, (1, 3))
>>>
Explanation / Answer
I didn't have a maze to test it on but it should work
def get_position_in_direction(position,direction):
if direction=='E':
return (position[0],position[1]+1)
if direction=='W':
return (position[0],position[1]-1)
if direction=='N':
return (position[0]-1,position[1])
if direction=='S':
return (position[0]+1,position[1])
def move(maze,position,direction):
newpos=get_position_in_direction(position,direction)
if newpos in maze:
can_move=True
if newpos==maze[len(maze)-1]:
is_finished=True
else:
is_finished=False
return (can_move,is_finished,newpos)
else:
return (False,False,position)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.