Guest User

Solution for the "Text Based Game" project (IT140/COP100+)

a guest
Feb 21st, 2021
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1.  
  2. # Replace the room names with your own rooms
  3. # and the items with your own items
  4. rooms = {
  5.  'Great Hall' : { 'South' : 'Bedroom', 'North': 'Dungeon', 'East' : 'Kitchen', 'West' : 'Library' },
  6.  'Bedroom' : { 'North' : 'Great Hall', 'East' : 'Cellar', 'item' : 'Armor' },
  7.  'Cellar' : { 'West' : 'Bedroom', 'item' : 'Helmet' },
  8.  'Kitchen':{'North': 'Dining Room', 'West': 'Great Hall'},
  9.  'Dining Room' : { 'South' : 'Kitchen', 'item' : 'Dragon' } #villain
  10. }
  11.  
  12. boss = "Dragon"
  13.  
  14. current_room = "Great Hall"
  15. inventory = list()
  16.  
  17. def all_items():
  18.     items = []
  19.     for room in rooms.values():
  20.         if "item" in list(room.keys()):
  21.             items.append(room["item"])
  22.     return items
  23.    
  24.  
  25. def directions():
  26.     pass
  27.  
  28. def list_actions():
  29.     for k, v in get_room().items():
  30.         if v in inventory:
  31.             continue
  32.         elif k == "item":
  33.             print("You can `get`", v)
  34.             continue
  35.         else:
  36.             print("You can ` move", k, "` to go to the", v)
  37.            
  38. def get_room():
  39.     return rooms[current_room]
  40.  
  41. def _win():
  42.     print("Congratulations! You managed to defeat the boss and win!")
  43.     quit()
  44.  
  45. def _not_win():
  46.     print("Sorry, you are still not strong enough to fight the boss.\nTry collecting more items first.")
  47.     return
  48.  
  49.  
  50. def _handle_boss():
  51.     items = all_items()
  52.     items.remove(boss)
  53.     if inventory == items:
  54.         _win()
  55.         return
  56.     _not_win()
  57.    
  58.    
  59.  
  60. def _move(to,  *args, **kwargs):
  61.     global current_room
  62.     current_room = get_room()[to]
  63.  
  64. def _get_item(*args, **kwargs):
  65.     name = get_room()["item"]
  66.     if name in inventory:
  67.         _error()
  68.         return
  69.     # special case handling for when the player
  70.     # tries to get the boss
  71.     if name == boss:
  72.         _handle_boss()
  73.         return
  74.    
  75.     inventory.append(name)
  76.  
  77. def _error():
  78.     print("\n***Sorry, that is an invalid command or the argument doesn't exist.***\n")
  79.    
  80.  
  81.  
  82. def parse_cmd(cmd, *args, **kwargs):
  83.     # Smelly Code:
  84.     # Hardcoded inside of a function
  85.     # non-modular
  86.     cmds = {
  87.         "move": _move,
  88.         "get": _get_item
  89.     }
  90.    
  91.    
  92.     # test to see if the command is valid
  93.     print(cmd, "in", list(cmds.keys()), "is", cmd in list(cmds.keys()))
  94.     if not (cmd in list(cmds.keys())):
  95.         _error()
  96.         return
  97.    
  98.     # test to see if the arguments are valid for room
  99.     if cmd == "move":
  100.         if not (args[0] in list(get_room().keys())):
  101.             _error()
  102.             return
  103.    
  104.     cmds[cmd](*args, **kwargs)
  105.  
  106.  
  107.  
  108. def main():
  109.    
  110.     # Tell the player the directions on how to play
  111.     directions()
  112.    
  113.     # main game loop
  114.     while 1:
  115.         # show the player what they can do
  116.         list_actions()
  117.        
  118.         # get input from the player
  119.         print("You are in the", current_room)
  120.         parse_cmd(*input("What would you like to do?").split())
  121.        
  122.        
  123. if __name__ == "__main__":
  124.     main()
Advertisement
Add Comment
Please, Sign In to add comment