Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. # Ruchella Kock
  2. # 12460796
  3. # This file defines the Adventure class
  4.  
  5.  
  6. from room import Room
  7. from room import Route
  8. from items import Items
  9. from inventory import Inventory
  10. import sys
  11.  
  12.  
  13. class Adventure():
  14. """
  15. This is your Adventure game class. It should contains
  16. necessary attributes and methods to setup and play
  17. Crowther's text based RPG Adventure.
  18. """
  19.  
  20. def __init__(self, game):
  21. """
  22. Create rooms and items for the appropriate 'game' version.
  23. """
  24. self.items = self.load_items(f"data/{game}Items.txt")
  25. self.rooms = self.load_rooms(f"data/{game}Rooms.txt")
  26. self.current_room = self.rooms[0]
  27. rooms_visited = []
  28. self.rooms_visited = rooms_visited
  29. self.players_inventory = Inventory()
  30.  
  31. def load_items(self, filename):
  32. """
  33. Load items from filename.
  34. Returns a collection of item objects.
  35. """
  36. # open the right adventure file
  37. with open(filename, "r") as f:
  38. list_of_items = []
  39. # go over the file and make a new_item everytime
  40. while(True):
  41. item_name = f.readline().strip()
  42. if item_name == "":
  43. break
  44. item_description = f.readline().strip()
  45. initial_room_id = f.readline().strip()
  46. f.readline()
  47.  
  48. new_item = Items(item_name, item_description, initial_room_id)
  49. list_of_items.append(new_item)
  50. return list_of_items
  51.  
  52. def load_rooms(self, filename):
  53. """
  54. Load items from filename.
  55. Returns a collection of item objects.
  56. """
  57. with open(filename, "r") as f:
  58. list_of_rooms = []
  59. while(True):
  60. list_of_routes = []
  61. id = f.readline().strip()
  62. if id == "":
  63. break
  64. name = f.readline().strip()
  65. description = f.readline().strip()
  66. if description != "-----":
  67. f.readline()
  68. current_route = "initialize"
  69. # make a route class for every route and add it to a list of routes
  70. while(current_route != '' and current_route != "\n"):
  71. current_route = f.readline().strip()
  72. direction = current_route[:current_route.find(" ")]
  73. route_id = current_route[current_route.rfind(" ") + 1: current_route.rfind(" ") + 2]
  74. conditional_item = current_route[current_route.rfind(" ") + 3:]
  75. new_route = Route(direction, route_id, conditional_item)
  76. list_of_routes.append(new_route)
  77. del list_of_routes[-1]
  78.  
  79. room_inventory = Inventory()
  80. room = Room(id, name, description, list_of_routes, room_inventory)
  81. list_of_rooms.append(room)
  82.  
  83. for item in self.items:
  84. for room in list_of_rooms:
  85. if room.id == item.initial_room_id:
  86. room.inventory.add(item)
  87. print(room)
  88. return list_of_rooms
  89.  
  90. def won(self):
  91. """
  92. Check if the game is won.
  93. Returns a boolean.
  94. """
  95. return self.current_room.won()
  96.  
  97. def move(self, direction):
  98. """
  99. Moves to a different room in the specified direction.
  100. """
  101. # check if it is a valid move if it is then move to that room
  102. if self.current_room.valid_move(direction, self.players_inventory) != None:
  103. current_room_id = self.current_room.valid_move(direction, self.players_inventory)
  104. for room in self.rooms:
  105. if room.id == current_room_id:
  106. self.current_room = room
  107. self.rooms_visited.append(self.current_room.id)
  108. else:
  109. return False
  110.  
  111. # if its a forced movement then print description and move to the room
  112. if self.current_room.forced_movement() and not self.current_room.won():
  113. self.current_room.look(self.rooms_visited)
  114. adventure.move("FORCED")
  115. return self.current_room.name
  116.  
  117. def play(self):
  118. """
  119. Play an Adventure game
  120. """
  121. print(f"Welcome, to the Adventure games.\n"
  122. "May the randomly generated numbers be ever in your favour.\n")
  123. self.current_room.printing(self.rooms_visited)
  124.  
  125. # Prompt the user for commands until they've won the game.
  126. while not self.won():
  127. command = input("> ")
  128. command = command.upper()
  129. # Check if the command is a movement or not.
  130. if command in ["EAST", "WEST", "SOUTH", "NORTH", "UP", "DOWN", "IN", "OUT"]:
  131. # if its a valid move then move to that room
  132. if adventure.move(command):
  133. self.current_room.printing(self.rooms_visited)
  134. else:
  135. print("Invalid move")
  136. # will print the description of the room the player is in
  137. elif command == "LOOK":
  138. self.current_room.look(self.rooms_visited)
  139. # will remind player how to play
  140. elif command == "HELP":
  141. print("You can move by typing directions such as EAST/WEST/IN/OUT")
  142. # will show player what is in the inventory
  143. elif command == "INVENTORY":
  144. # checks if players's inventory is empty
  145. if self.players_inventory.is_empty():
  146. print("Your inventory is empty.")
  147. else:
  148. print(self.players_inventory)
  149. # player can take an item and add it to their inventory
  150. elif command[0:4] == "TAKE":
  151. command_item = command.split()
  152. adventure.take_or_drop(command_item[1], self.players_inventory, self.current_room.inventory, "taken.")
  153. # player can drop an item and it will go back to the intitial room
  154. elif command[0:4] == "DROP":
  155. command_item = command.split()
  156. adventure.take_or_drop(command_item[1], self.current_room.inventory, self.players_inventory, "droped.")
  157. # with the quit command the program will stop running thus ending the game
  158. elif command == "QUIT":
  159. print("Thanks for playing")
  160. sys.exit()
  161. else:
  162. print("Invalid command")
  163.  
  164. def take_or_drop(self, command, take_from_inventory, remove_from_inventory, string):
  165. """
  166. Take an item from the room inventory and add it to the players inventory
  167. Drop an item from the player inventory and add it to the room_inventory
  168. """
  169. # check if the item is in the inventory
  170. item = remove_from_inventory.find_item(command)
  171. # if it is then move it from one inventory to the other
  172. if item != None:
  173. take_from_inventory.add(item)
  174. remove_from_inventory.remove(item)
  175. print(f"{command} {string}")
  176. else:
  177. print("No such item.")
  178.  
  179.  
  180. if __name__ == "__main__":
  181. adventure = Adventure("Crowther")
  182.  
  183. adventure.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement