Advertisement
lukec99

Untitled

Apr 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.97 KB | None | 0 0
  1. import random
  2. class Room:
  3. """Stores information about the locations that the player and monster
  4. will be moving through.
  5.  
  6. attributes: name (str), room_number (int)"""
  7.  
  8.  
  9. def __init__(self, name, room_number):
  10. """creates a Room object with a set name, room number, and list
  11. of other rooms this Room is connected to.
  12.  
  13. Room, str, int, list_of_Rooms -> None"""
  14.  
  15. self.name = name
  16. self.room_number = room_number
  17. self.__other_rooms = []
  18.  
  19. def connect_to(self, other_room):
  20. """takes another room or an iterable of Rooms and adds connections
  21. between them.
  22.  
  23. Room, Room -> None"""
  24.  
  25. if isinstance(other_room, Room):
  26. self.__other_rooms.append(other_room)
  27. other_room.__other_rooms.append(self)
  28. elif isinstance(other_room, tuple):
  29. for room in other_room:
  30. self.__other_rooms.append(room)
  31. room.__other_rooms.append(self)
  32. elif isinstance(other_room, list):
  33. for room in other_room:
  34. self.__other_rooms.append(room)
  35. room.__other_rooms.append(self)
  36.  
  37. def get_exit_list(self):
  38. """returns the list of the current Room's exits.
  39.  
  40. Room -> list_of_str"""
  41. exitlist = []
  42. for room in self.__other_rooms:
  43. exitlist.append(room.room_number)
  44. return exitlist
  45.  
  46. def is_connected_to(self, other_room):
  47. """determines whether or not two Rooms are connected.
  48.  
  49. Room, Room -> bool"""
  50.  
  51. return other_room in self.__other_rooms
  52.  
  53. def description(self):
  54. """returns a string representing a description of the Room. The
  55. description includes the name of the Room and a list of the room
  56. numbers of adjacent rooms.
  57.  
  58. Room -> str"""
  59.  
  60. room_numbers = []
  61. for room in self.__other_rooms:
  62. room_numbers.append(str(room.room_number))
  63.  
  64. return "You are in " + str(self.name) + ". Valid exits: " + ", ".join(room_numbers)
  65.  
  66.  
  67. def __repr__(self):
  68. """returns a string representing an object in Room form.
  69.  
  70. Room -> str"""
  71.  
  72. return "Room(" + repr(self.name) + ", " + repr(self.room_number) + ")"
  73.  
  74.  
  75.  
  76. lab = Room("the lab", 0)
  77. bedroom = Room("the bedroom", 1)
  78. bathroom = Room("the bathroom", 2)
  79. studio = Room("the studio", 3)
  80. basement = Room("the basement", 4)
  81. garage = Room("the garage", 5)
  82.  
  83. lab.connect_to((bedroom, bathroom, basement))
  84. bedroom.connect_to((bathroom, garage))
  85. bathroom.connect_to(studio)
  86. studio.connect_to((basement, garage))
  87. basement.connect_to(garage)
  88.  
  89. Roomlist = [lab, bedroom, bathroom, studio, basement, garage]
  90.  
  91. class Command:
  92. """Stores types of commands.
  93.  
  94. attributes: action (str), destination (Room)"""
  95.  
  96.  
  97. def __init__(self, action, destination):
  98. """creates a new Command object with a set action and destination
  99.  
  100. Command, str, Room -> None"""
  101.  
  102. self.action = action
  103. self.destination = destination
  104.  
  105. def __repr__(self):
  106. """returns a string representing an object in Command form
  107.  
  108. Command -> str"""
  109.  
  110. return "Command(" + repr(self.action) +", " + repr(self.destination) + ")"
  111.  
  112. class Movable(Command):
  113. """Represents objects and characters that have changeable locations.
  114.  
  115. setters: .move_to()"""
  116.  
  117. def move_to(self, location):
  118. """serves as a setter for the location of the Movable object.
  119.  
  120. Movable -> None"""
  121.  
  122. self.__location = location
  123.  
  124. def __init__(self, location):
  125. """Creates a new instance of Movable in a given location
  126.  
  127. Movable, Room -> None"""
  128.  
  129. self.move_to(location)
  130.  
  131. def get_location(self):
  132. """Returns the location of the Movable object.
  133.  
  134. Movable -> Room"""
  135.  
  136. return self.__location
  137.  
  138. def update(self):
  139. """Updates a parameter of the location.
  140.  
  141. Movable -> None"""
  142.  
  143. pass
  144.  
  145. def __repr__(self):
  146. """returns a string representing an object in Movable form
  147.  
  148. Movable -> str"""
  149.  
  150. return "Movable(" + repr(self.get_location()) + ")"
  151.  
  152. class Wanderer(Movable):
  153. """This class is used for things that move around randomly on their own
  154.  
  155. additional attributes: is_awake (boolean)"""
  156.  
  157. def __init__(self, location = random.choice(Roomlist), is_awake = True):
  158. """Creates a new instance of Wanderer at a certain location with
  159. an optional boolean determining whether it is awake or not.
  160.  
  161. Wanderer, [Room, bool] -> None"""
  162.  
  163. super().__init__(location)
  164. self.is_awake = is_awake
  165.  
  166. def update(self):
  167. """Checks if the Wanderer is awake, moving it to a random adjacent
  168. room and making it go back to sleep if it was originally awake.
  169.  
  170. Wanderer -> None"""
  171.  
  172. if self.is_awake == True:
  173. self.move_to(random.choice(Roomlist))
  174. self.is_awake = False
  175.  
  176. def __repr__(self):
  177. """returns a string representing an object in Wanderer form
  178.  
  179. Wanderer -> str"""
  180.  
  181. return "Wanderer( " + repr(self.__location) + ", " + repr(self.is_awake) + ")"
  182.  
  183. monster = Wanderer()
  184.  
  185. IS_MONSTER_SHOT = False
  186.  
  187. class Player(Movable):
  188. """This class is used for the player character.
  189.  
  190. Attributes: num_darts (int)"""
  191.  
  192.  
  193. def __init__(self, num_darts, location = random.choice(Roomlist)):
  194. """Creates a new Player object with a set number of darts and
  195. an optional room
  196.  
  197. Player, int, [Room] -> None"""
  198.  
  199. super().__init__(location)
  200. self.num_darts = num_darts
  201.  
  202.  
  203. def shoot_into(self, destination):
  204. """Shoots at a monster, decreasing the number of darts by one
  205. and changing the variable of whether it is shot or not depending
  206. on whether it was in the right room"""
  207.  
  208. self.num_darts -= 1
  209.  
  210. global IS_MONSTER_SHOT
  211.  
  212. if monster.__location() == destination:
  213. IS_MONSTER_SHOT = True
  214. return True
  215. return False
  216.  
  217. def get_command(self):
  218. """Communicates with the user to figure out what they want to do
  219. and returns an appropriate Command object once a valid command
  220. has been entered.
  221.  
  222. Player -> Command"""
  223.  
  224.  
  225.  
  226. while True:
  227. print("What do you want to do?")
  228. user_input = input(">")
  229. if "shoot into" in user_input.lower():
  230. if int(user_input[11]) in self.get_location().get_exit_list():
  231. if Roomlist[int(user_input[11])].is_connected_to(self.get_location()):
  232. if self.num_darts > 0:
  233. command1 = Command("shoot", Roomlist[int(user_input[11])])
  234. return command1
  235. break
  236. else:
  237. print("You are out of darts.")
  238. else:
  239. print("You are not connected to that room.")
  240. else:
  241. print("That is not a valid room.")
  242.  
  243. elif "go to" in user_input.lower():
  244. if int(user_input[6]) in self.get_location().get_exit_list():
  245. if Roomlist[int(user_input[6])].is_connected_to(self.get_location()):
  246. command1 = Command("move", Roomlist[int(user_input[6])])
  247. return command1
  248. break
  249. else:
  250. print("You are not connected to that room.")
  251. else:
  252. print("That is not a valid room.")
  253.  
  254. else:
  255. print("That is not a valid command.")
  256.  
  257. def execute_command(self, command):
  258. """Takes a Command object and carries out the corresponding action.
  259.  
  260. Command -> str"""
  261.  
  262. if command.action == "shoot":
  263. self.shoot_into(command.destination)
  264. print("You shoot a dart into room " + command.destination.room_number + ".")
  265. if IS_MONSTER_SHOT == False:
  266. print("It doesn't sound like you hit anything.")
  267. print("You can hear the monster moving about.")
  268. else:
  269. print("You've shot the monster!")
  270.  
  271. elif command.action == "move":
  272. self.move_to(command.destination)
  273. print("You walk into room number " + str(command.destination.room_number) + ".")
  274. if monster.__location.is_connected_to(self.__location):
  275. print("The monster is close, you can smell it.")
  276. elif monster.__location == self.__location:
  277. print("You are in the same room as the monster!")
  278.  
  279. def update(self):
  280. """Displays a description of the Player's room, obtains a command,
  281. and uses execute_command to carry it out.
  282.  
  283. Player -> str"""
  284.  
  285. print(self.get_location().description())
  286. command1 = self.get_command()
  287. self.execute_command(command1)
  288.  
  289. def __repr__(self):
  290. """returns a string representing an object in Player form
  291.  
  292. Player -> String"""
  293.  
  294. return "Player(" + repr(self.num_darts) + ", " + repr(self.__location) + ")"
  295.  
  296.  
  297. player1 = Player(2)
  298.  
  299. UPDATE_LIST = [player1, monster]
  300.  
  301. print("Welcome to Monster Capture!\n")
  302.  
  303. print("""A wild monster is loose in the area, and it's your job to capture it without
  304. getting eaten. Travel around the map, and when you have figured out which
  305. room the monster is in, go to an adjacent room and shoot a tranquilizer
  306. dart into that room. If you hit the monster, you win! But be careful; you
  307. only have two darts!""")
  308.  
  309. print("You begin the game in room " + str(player1.get_location().room_number) + ".")
  310.  
  311. player1.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement