Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In simple python: this is for Hunt the Wampus game. For every class it must have

ID: 3824042 • Letter: I

Question

In simple python: this is for Hunt the Wampus game. For every class it must have appropriate .__init__() and .__repr__()methods. Create a class called Cave, for storing information about the locations that the player and monster will be moving through. Caves have two public attributes for storing the name of the cave (e.g., "the docking bay" or "a long hallway") and the cave number. The constructor should set both a name and a cave number. You should be able to create a cave with a command like Cave("the throne cave",1)or Cave("a caveroom",17). Cave objects should also have a non-public attribute storing the list of other Caves that this Cave is connected to. When the constructor creates a new Cave, the list of exits should start out as empty.

To add connections between cave, create a method called .connect_to(), which takes another Cave or an iterable of Caves and adds connections between them. Note that in addition to adding the other Cave to the current Cave's exit list, you will also need to add the current Cave to the other Cave's exit list. Since the exit list is not public, you should also create a getter for it. Write a method called .is_connected_to() which determines whether or not two Caves are connected. Write a method called .description() which returns a string representing a description of the Cave. The description should include the name of the Cave and a list of the cave numbers of adjacent caves.

After you create the Cave class, create the Cave objects that you will use in the game; must have at least 6 Caves, and each Cave must be connected to at least 3 other Caves. You should also create a constant variable to store a list (or set) of all the Caves that are in the game's map. Remember that the names of constant variables should be in all uppercase. This should not be inside of any class definition.

Create a very simple class clalled Command which has two public attributes: one storing the action type (either "move" or "shoot") and a destination (a Cave). No methods are needed beyond the obvious .__init__() and .__repr__().

Create a class called Movable for representing objects and characters that have changeable locations. Movable objects should have a non-public attribute for storing their location. Create a method for Movable objects called .move_to() to serve as a setter for the location. All other commands that set the location of a Movable object should use this method and not use the location attribute directly. You should be able to create a new Movable instance with a command like Movable(hall)(assuming that hall stores a Cave instance. Create a method for Movable objects called .get_location() to serve as a getter for the location. All Movable objects should have a method called .update(), which we will call once every turn. For subclasses of Movable, the .update() function will do things like move the object or get input from the user to decide what to do. Create a subclass of Movable called Wanderer which will be used for things that move around randomly on their own, such as the monster.

In addition to the attributes inherited from Movable, all Wanderers have a public attribute-a bool- indicating whether they are awake or not. You should be able to create Wanderers with commands like Wanderer(lab) just like ordinary Movables, but you should also be able to use the Wanderer constructor with no arguments (e.g., just write Wanderer()), and in that case, the new Wanderer should be placed in a Cave chosen randomly from the list of caves you created earlier. Newly created Wanderers always start out as awake. Remember to use the .move_to() method whenever you change or set the location.

Override the .update() method so that it will move the Wanderer to a random Cave that is adjacent to its current location, unless the Wanderer is not awake, and in that case, it shouldn't move at all. Create a Wanderer in a random cave and store it in a variable called monster. Create a subclass of Movable called Player which will be used for the player character. In addition to any attributes inherited from Movable, Player objects should also have a public attribute storing the number of darts that the player has.

You should be able to create new instances of the Player class with commands like Player(5,hall) (putting the player in the hall with 5 darts) or Player(3) (putting the player in a randomly chosen Cave with 3 darts). Make sure you choose a random Cave that is different from the one that the monster (stored in the variable monster) is currently in. Create a method called .shoot_into() which takes one argument: the destination cavebeing fired into. The method should first decrease the number of darts by 1. If the monster (stored in the variable monster) is in the destination Cave, the method should change the monster's awake status to False (indicating that the monster is asleep) and then .shoot_into() should return True (indicating that the shot was successful). This method does not need to worry about whether or not there are enough darts left.

Create a method called .get_command() which will communicate with the user to figure out what the user wants to do and will return an appropriate Command object, once a valid command has been entered. This method should display (yes, you should be using print() here) a request for a command and then get input from the user. If the command the user types in is invalid, then it should say why it is not valid (e.g., No darts left. or There's no exit to that cave here.) and ask for another command. This should repeat until the user enters a valid command, at which point the method should return a Command object with an appropriate action type and destination. Create a method called .execute_command() which takes a Command object and carries out the corresponding action. Note that this method does not need to check that the command is valid. This method should first determine whether the action type is "move" or "shoot" and then call either .move_to() or .shoot_into() as appropriate. Afterwards, .execute_command() should print a message indicating what action was taken.

If the action is moving to a new cave, the printed message should include the number or name of the new cave. If the monster is in the same cave, the message should say so. If the monster is in an adjacent cave, it should print a message letting the player know that they can smell the monster nearby. If the action is shooting into another cave, then the method should display whether or not the shot was successful. Override the .update() method so that it will display the description of the Player's current Cave, use .get_command() to obtain a Command from the uer, and then use .execute_command() to carry out that Command.

Create a Player object in a random location and store it in a variable called player1. Create a constant variable called Update_list which stores a list of all of the Movable objects currently in the game. We will use this list to make sure that every object gets updated appropriately once every turn of the game. Put the monster (monster) and the player (player1) into update_list.

Finally, it's time to write the main game loop. Display a message indicating where the player is starting the game and also whether they can smell the monster. (Remember that they can smell the monster if the monster is in an adjacent cave.) As usual with games like, this, there's a loop that repeats until the player wins or loses. Each time through the loop, we should: Use the .description() method of the player's current Cave to get a description of the cave and then display that description. If the player is currently in the same cave as the monster and the monster is awake, display a message indicating that the player has lost, and then end the game. Loop through each of the Movable objects in update_list and carry out that object's .update() method. If the monster is asleep, display a message indicating that the player has won, and then end the game.

Explanation / Answer

import sys import random class Game: "Hunt the Wumpus Game Class" # The cave map CAVES = { "A":["B","E","F"], "B":["A","C","H"], "C":["B","D","J"], "D":["C","E","L"], "E":["A","D","N"], "F":["A","G","O"], "G":["F","H","T"], "H":["B","G","I"], "I":["H","J","S"], "J":["C","I","K"], "K":["J","L","R"], "L":["D","K","M"], "M":["L","N","Q"], "N":["E","M","O"], "O":["F","N","P"], "P":["O","Q","T"], "Q":["M","P","R"], "R":["K","Q","S"], "S":["I","R","T"], "T":["G","P","S"] } output = sys.stdout.write; # start for the player player = "A" # the standard environment (will be randomised) wumpus = "T" NUM_PITS = 2 NUM_BATS = 2 pits = ["I","O"] # should be wiped by random positions bats = ["G","M"] # should also be wiped by random positions shots = 5 def __init__(self): pass def run(self): "Runs the game of Hunt the Wumpus" self.printWelcome() self.output("Do you need instructions (y/N)? ") if self.readInput() == "Y": self.printInstructions() loop = True while (loop): self.setEnvironment() self.mainLoop() self.output("Do you want to play another game (Y/n)? ") if self.readInput() == "N": loop = False self.output("Fare thee well, mighty hunter! ") def mainLoop(self): "The main loop that runs the hunt" isGameActive = True # loop until we win, lose or quit while (isGameActive): self.output(" ") self.describeChamber() self.output("Do you want to (M)ove or (S)hoot? ") action = self.readInput() # get the input letter if action == "M": # MOVE self.output("Which chamber to move to (") # print the options of neighbours neighbours = self.CAVES[self.player] for chamber in neighbours[:-1]: self.output("%s, " % chamber) self.output("%s)? " % neighbours[-1]) chamber = self.readInput() if chamber in neighbours: self.player = chamber self.output("Tread carefully... ") else: self.output("That's not a valid move... ") elif action == "S": # SHOOT self.output("Which chamber to shoot into (") # print the options of neighbours neighbours = self.CAVES[self.player] for chamber in neighbours[:-1]: self.output("%s, " % chamber) self.output("%s)? " % neighbours[-1]) chamber = self.readInput() if chamber in neighbours: self.output("You fire your trusty blunderbuss! BLAM!! ") self.shots = self.shots - 1 # was it a hit? if chamber == self.wumpus: self.output("THUWMP!! The wumpus has been slain! Well done, mighty hunter! ") isGameActive = False else: if self.shots > 0: self.output("The echo of the shot cannons through the caverns ") # should move the wumpus now self.output("You hear the wumpus flumphing about. It might have moved... ") wumpus_choices = list(self.CAVES[self.wumpus]) wumpus_choices.append(self.wumpus) self.wumpus = random.choice(wumpus_choices) if self.shots > 1: self.output("You've got %d shots left " % self.shots) else: self.output("Last shot. Make it count! ") else: self.output("AARGHH! You've caused a cave in!! Rocks fall, everybody dies!! ") isGameActive = False else: self.output("You can't shoot there, silly... ") elif action == "Q": # QUIT self.output("Do you want to end this game? (y/N) ") if self.readInput == "Y": isGameActive = False # check to see if this room is safe moveLoop = isGameActive while moveLoop: moveLoop = False if self.player == self.wumpus: self.output("AARGHH!! You've bumped into the wumpus! ") self.output("It seems the hunter is now the hunted... or rather, lunch. ") isGameActive = False elif self.player in self.pits: self.output("AARGHH!! You've fallen into a bottomless pit! ") self.output("When you fall into a bottomless pit, you die of starvation... ") isGameActive = False elif self.player in self.bats: self.output("OH NO!! You've run into some giant bats! ") self.player = random.choice(self.CAVES.keys()) self.output("They've carried you to Chamber %s. Hope it's not too dangerous... " % self.player) moveLoop = True def describeChamber(self): "Describes the players chamber" neighbours = self.CAVES[self.player] self.output("You are currently in Chamber %s. " % self.player) self.output("Passages lead to Chambers ") for cavern in neighbours[:-1]: self.output("%s, " % cavern) self.output("and %s. " % neighbours[-1]) # need to check for breezes, stenches and bat chatter chatter = False breeze = False stench = False if self.wumpus in neighbours: stench = True for cavern in self.pits: if cavern in neighbours: breeze = True for cavern in self.bats: if cavern in neighbours: chatter = True if stench: self.output("You smell a wumpus! ") if chatter: self.output("You hear the chatter of bats. ") if breeze: self.output("You feel a nearby breeze. ") def printWelcome(self): "Prints the welcome message to the screen" self.output(" HUNT THE WUMPUS ".center(50, "-")) self.output(" ") def printInstructions(self): "Prints out the instructions for Hunt the Wumpus" self.output("""There's a wumpus loose in the underground caverns! As the great wumpus hunter, it is your job to hunt it down with your trusty blunderbuss! On your turn, you can move or shoot into one of three caverns. You have five shots with which to hunt the wumpus. You can sense when a wumpus is nearby by its tell-tale stench. Look out for pits; you'll feel a breeze when nearby. Also watch for the bats; they delight in randomly dropping hunters into caverns. Good luck, mighty hunter! """); def readInput(self): "Reads in a line of imput, and returns the first character (all that's needed for the game)" i = sys.stdin.readline() return i[0].upper() def setEnvironment(self): """Sets the environment: the player to pos. A, and randomise everything else Random position of the wumpus Random bats (for the number of the caves) Random pits (for the number of pits) """ cave_list = self.CAVES.keys() self.player = "A" # remove the player position from the list cave_list.remove("A") self.wumpus = random.choice(cave_list) num_random_pos = self.NUM_PITS + self.NUM_BATS random_list = random.sample(cave_list, num_random_pos) # need to make sure nothing is in the same pos to begin with self.pits = random_list[:self.NUM_PITS] self.bats = random_list[-self.NUM_BATS:] self.shots = 5 if __name__ == "__main__": wumpusGame = Game(); wumpusGame.run() # run the main game function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote