Advertisement
Guest User

Untitled

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