A bit improved.

Originally Posted by
Vox
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()
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"}
}
boss = "Dragon"
current_room = "Great Hall"
inventory = []
def directions():
print("""
Move commands: move North, move South, move East, move West
Add items to inventory: get <item>
Collect all items before fighting the Dragon!
""")
def get_room():
return rooms[current_room]
def list_actions():
room = get_room()
for direction, place in room.items():
if direction != "item":
print(f"You can move {direction} to {place}")
if "item" in room:
item = room["item"]
if item != boss:
print(f"You can get {item}")
def move(direction):
global current_room
room = get_room()
if direction not in room:
print("You can't go that way.")
return
current_room = room[direction]
print("You moved to", current_room)
check_boss()
def get_item():
room = get_room()
if "item" not in room:
print("There is nothing here.")
return
item = room["item"]
if item == boss:
print("You cannot pick up the Dragon!")
return
inventory.append(item)
del room["item"]
print("You picked up:", item)
print("Inventory:", inventory)
def check_boss():
room = get_room()
if room.get("item") == boss:
if len(inventory) == count_items():
print("You defeated the Dragon! You win!")
quit()
else:
print("The Dragon defeated you. You needed more items.")
quit()
def count_items():
count = 0
for r in rooms.values():
if "item" in r and r["item"] != boss:
count += 1
return count
def parse_cmd(command):
parts = command.split()
if len(parts) == 0:
return
cmd = parts[0]
if cmd == "move" and len(parts) > 1:
move(parts[1])
elif cmd == "get":
get_item()
else:
print("Invalid command.")
def main():
directions()
while True:
print("\nYou are in", current_room)
list_actions()
command = input("What would you like to do? ")
parse_cmd(command)
if __name__ == "__main__":
main()