Editor

Text Adventure Map

#638 · Graphs · Advanced · Mini-project · 220 XP · about 90 min

Build the map of a text adventure game. The map is a dictionary of rooms, and each room maps a direction to the room it leads to: {'Hall': {'north': 'Kitchen'}, 'Kitchen': {'south': 'Hall'}}. The six directions are north, south, east, west, up and down.

Parts

  • Part 1 parse_map(lines): read a map written as lines such as 'Hall: north=Kitchen, east=Great Library': the room, a colon, then its exits separated by commas. A room with no exits is written 'Cellar:'. Remove the spaces around every name, and keep the rooms and exits in the order they are written.
  • Part 2 add_return_paths(rooms): map makers often forget the way back. For every exit, give the room it leads to an exit in the opposite direction back to where it came from (north and south, east and west, up and down are opposites), unless that room already has an exit in that direction. A room that appears only as an exit is added to the map. Change rooms itself and return it.
  • Part 3 parse_command(command): the direction a typed command means. Commands are not case sensitive and may have extra spaces. 'go north', 'north' and the one-letter short form 'n' all mean 'north' (the short forms are n, s, e, w, u and d, also after go). Anything else returns None.
  • Part 4 walk(rooms, start, commands): follow the commands from start. A command that is not a direction, or a direction with no exit, leaves the player where they are. Return the list of rooms the player was in: start, then each room they walked into.
  • Part 5 shortest_route(rooms, start, goal): the directions of a shortest route from start to goal, as a list. Return [] when start is goal, and None when there is no way to get there. In the tests there is only ever one shortest route.

Examples

rooms = parse_map(['Hall: north=Kitchen', 'Kitchen: down=Cellar', 'Cellar:'])
# {'Hall': {'north': 'Kitchen'}, 'Kitchen': {'down': 'Cellar'}, 'Cellar': {}}
add_return_paths(rooms)
# {'Hall': {'north': 'Kitchen'}, 'Kitchen': {'down': 'Cellar', 'south': 'Hall'}, 'Cellar': {'up': 'Kitchen'}}
parse_command('  Go   N ')                        # returns 'north'
walk(rooms, 'Hall', ['n', 'dance', 'go down', 'west'])
# returns ['Hall', 'Kitchen', 'Cellar']
shortest_route(rooms, 'Cellar', 'Hall')           # returns ['up', 'south']

Constraints

  • A map has at most 2,000 rooms. Room names are made of letters, digits and spaces.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.