0

Here, I suggested the following approach to implementation of a simple text adventure game (in Python). I think that this principle could be used to develop an adventure, similar to The Colossal Cave adventure, by storing a following rooms in a deque. Rooms (represented here by tuple of (function, params), but possibly also objects) have an executable part (in object-oriented approach, a method) that fills in the deque. When an room is prepended to the deque, it will come before all rooms stored before, if it is appended, it will come after all the rooms.

Do you think this approach provided a maintainable way to develop such game?

For simplicity, global variables were used. Also, the room representation by tuples could (and probably should) be replaced by objects. The input and output processing is also simplified. The code below is not intended to be a working program, it is just an presentation of this idea.

#
# State.
#

money = 100
life = 100

rooms = deque()


def next_room(room, **extras):
    rooms.append((room, extras))


#
# Rooms.
#

def r_welcome():
    print("Welcome to XyzzyGame.")
    if yesno("Do you want instructions?", True):
        next_room(r_instructions)
    next_room(r_main_hall)

def r_instructions():
    print("Lorem ipsum.")

def r_main_hall():
    print("You are in the main hall.")

    navigate({
        'out':   (r_pathway, {}),
        'north': (r_named_hall, {'name': 'Foo'}),
        'west': (r_named_hall, {'name': 'Bar'}),
    })

def r_named_hall(name='Some'):
    print(f'You are in a hall called “{name}”')

...

#
# Utility functions.
#

def question(text, default):
    line = input().lower()
    if len(strip(line)) == 0:
        line = default
    return line

def yesno(text, default):
    resp = question(text, 'yes' if default else 'no')
    return resp[0] == 'y'

def navigate(options):
    while True:
        direction = question("Where do you want to go now?", 'default')
        if direction not in options:
            print("I do not know this place. You can go to these places:")
            for option in options:
                print("  • " + option)
        else:
            (room, extras) = options[direction]
            next_room(room, **extras)
            break

#
# Entry point.
#

def main():
    next_room(r_welcome)  # Initial room.
    while len(rooms) > 0:
        (room, extras) = rooms.popleft()
        room(**extras)

main()
1
  • 3
    Does the deque only have one item in it? All the time? Why is it a deque and not just a variable?
    – user253751
    Jun 10 at 17:15

1 Answer 1

2

It seems like a bad design.

  1. The rooms will need state
  2. All rooms are going to share some functions, say "turn on light"
  3. There will be actions to take independent of the room. "fight monster" "check inventory"
  4. Rooms might need to reference other rooms. ie "pull this lever, elsewhere a door opens"
  5. Things might happen in rooms when the play is not present. ie "the monster moves closer"
  6. Hardcoding the actions and description of each room is going to mean a lot of code.

If you want my rough advice I would say you are going to want an array of identical Room objects, each populated from a data file

These should handle the generic functions, look, move to another room etc. You will also maybe want a GameState object to keep track of key stuff has the idol been picked up, does the player have the key etc. I guess its a big subject to cover

Not the answer you're looking for? Browse other questions tagged or ask your own question.