Make sure to use this wisely. If you submit the code as-is, you won't get a passing grade. Instead, maybe remove the comments, make the code a little bit sloppier, and change the names of the rooms and items to the names of your room and items.
Pastebin (for syntax highlighting)
Code:
# Replace the room names with your own rooms
# and the items with your own items
rooms = {
'Great Hall' : { 'South' : 'Bedroom', 'North': 'Dungeon', 'East' : 'Kitchen', 'West' : 'Library' },
'Bedroom' : { 'North' : 'Great Hall', 'East' : 'Cellar', 'item' : 'Armor' },
'Cellar' : { 'West' : 'Bedroom', 'item' : 'Helmet' },
'Kitchen':{'North': 'Dining Room', 'West': 'Great Hall'},
'Dining Room' : { 'South' : 'Kitchen', 'item' : 'Dragon' } #villain
}
boss = "Dragon"
current_room = "Great Hall"
inventory = list()
def all_items():
items = []
for room in rooms.values():
if "item" in list(room.keys()):
items.append(room["item"])
return items
def directions():
pass
def list_actions():
for k, v in get_room().items():
if v in inventory:
continue
elif k == "item":
print("You can `get`", v)
continue
else:
print("You can ` move", k, "` to go to the", v)
def get_room():
return rooms[current_room]
def _win():
print("Congratulations! You managed to defeat the boss and win!")
quit()
def _not_win():
print("Sorry, you are still not strong enough to fight the boss.\nTry collecting more items first.")
return
def _handle_boss():
items = all_items()
items.remove(boss)
if inventory == items:
_win()
return
_not_win()
def _move(to, *args, **kwargs):
global current_room
current_room = get_room()[to]
def _get_item(*args, **kwargs):
name = get_room()["item"]
if name in inventory:
_error()
return
# special case handling for when the player
# tries to get the boss
if name == boss:
_handle_boss()
return
inventory.append(name)
def _error():
print("\n***Sorry, that is an invalid command or the argument doesn't exist.***\n")
def parse_cmd(cmd, *args, **kwargs):
# Smelly Code:
# Hardcoded inside of a function
# non-modular
cmds = {
"move": _move,
"get": _get_item
}
# test to see if the command is valid
print(cmd, "in", list(cmds.keys()), "is", cmd in list(cmds.keys()))
if not (cmd in list(cmds.keys())):
_error()
return
# test to see if the arguments are valid for room
if cmd == "move":
if not (args[0] in list(get_room().keys())):
_error()
return
cmds[cmd](*args, **kwargs)
def main():
# Tell the player the directions on how to play
directions()
# main game loop
while 1:
# show the player what they can do
list_actions()
# get input from the player
print("You are in the", current_room)
parse_cmd(*input("What would you like to do?").split())
if __name__ == "__main__":
main()