Question: Q: Tests: Code: class Position: def __init__(self, north = False, east = False, south = False, west = False, exit = False): self.north = north

Q:Q:Tests: Code: class Position: def __init__(self, north = False, east = False,Tests:

south = False, west = False, exit = False): self.north = northCode:

class Position:

def __init__(self, north = False, east = False, south = False, west = False, exit = False): self.north = north self.east = east self.south = south self.west = west self.exit = exit

def can_escape(maze): return False

def escape_route(maze): return []

def print_maze(maze): height,width = len(maze),len(maze[0]) cell_width = 3 result = "+" + ((('-' * cell_width) + '+') * width) + ' ' for row in range(height): result += "|" for col in range(width): currPos = maze[row][col] if currPos.exit: result += " E " else: result += " " * cell_width result += " " if currPos.east else "|" result += " +" for col in range(width): currPos = maze[row][col] result += (' ' if currPos.south else '-') * cell_width result += "+" result += " " print (result)

if __name__ == "__main__": pass

Get Me Out of Here! Introduction Now we get to the hard (but maybe fun one). In this we solve a simple but important problem path-finding. We'll do this one in Python. The goal here is to complete two functions: can_escape and escape_route, both of which take a single parameter, which will be a grid-based maze, the format of which is indicated below. To help this, we have a simple class Position already implemented. You can add things to Position If you like, but there's not a lot of point to it. There is also a matn section, in which you can perform your own tests. Maze Format The maze format is just a List containing List s of Posttion s. Each position contains a varlable (publicly accessible) that indicates whether this is a path in each of the four directions ("north" is up and (0,0) is in the top left, although there is no visual element here), and also contains a public varlable that indicates whether the position is an exit or not. Mazes will obey the following rules: - (0,0) will never be an exit. - If you can go in a direction from one location, then you can go back from where that went (e.g. If you can go "east" from here, you can go "west" from the location that's to the east of here, arriving back here.) - When testing escape_route, there will always be at least one valld path to each exit, and there will be at least one exit (tests for can_escape may include mazes that have no way to exit). To give an example, if a maze consisted of four locations in a 22 grid: The maze would consist of a list of two lists, each containing two positions: [[Pos1, Pos2], [Pos3, Pos4]] . Each of those positions has boolean variables indicating which way one could move from that position. 50 pos 2 at position (0,1) (l.e. maze [0][1]) would have its west and south variables set to true and all others to false. Pos 3 ( maze[1] [0]) would have east set to true and exit set to true, and all others to false. can_escape The function can_escape takes a single parameter in format describe above representing the maze, otherwise. (0,0) will never be an exit. For the example maze above, can_escape should return true. escape_route The function escape_route also takes a single parameter representing a maze, in the format as described, and returns a sequence of directions ( "north", "east", "south" , "west" ) giving a route from (0,0) to some exit. It does not have to be the best route and can double back, but it does have to be a correct sequence that can be successfully followed step by step. You do not have to worry about mazes with no escape. For the example maze above, a correct answer would be [east, south, west]. This is also the shortest answer for this particular maze, but [east, west, east, south, north, south, west] would also be acceptable as an answer. [south] would not, as this is not a valid path. Advice and Answers Keeping track of where you have been is really handy. The L1st method pop is also really handy. can_escape can be solved with less finesse than escape_route - you don't have to worry about dead ends etc, whereas escape_route needs to return a proper route - no teleporting. "Cannot escape the 11 maze." "Cannot escape a maze if there's no paths." "Cannot escape a maze with no exits." "Can escape a maze with exits False is not true : The maze has exits and paths to them, there should be a way out! The maze tested was: "Can you find a path in several mazes

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!