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()