Python - Room class assignment

From WLCS

Objective

  • You will learn to create a class comprised of variables and methods

Purpose

  • The purpose of this class is to encapsulate all the attributes and methods of a single Room.
  • The Room class implements most of the actual game functionality.

Attributes

The Room class should use variables to represent the following attributes:

  • int row: will contain the row location of the room
    • Default: -1
  • int col: will contain the column location of the room
    • Default: -1
  • Room roomToNorth: contains a reference to the Room object reached by going north from this room, or None if no such Room exists.
    • Default: None
  • Room roomToEast: contains a reference to the Room object reached by going east from this room, or None if no such Room exists.
    • Default: None
  • Room roomToSouth: contains a reference to the Room object reached by going south from this room, or None if no such Room exists.
    • Default: None
  • Room roomToWest: contains a reference to the Room object reached by going west from this room, or None if no such Room exists.
    • Default: None
  • String description: a String containing a textual description of the room, e.g. "This is the kitchen."
    • Default: "A nondescript room"
  • Creature monster: a reference to the Creature object inhabiting this room, or None if the room is empty of monsters.
    • Default: None
  • Weapon item: a reference to the Weapon object in this room, or None if the room has no weapon lying in it.
    • Default: None

Methods

The Room class should have the following methods:

  • __init__(self, String desc="A nondescript room.", Creature occupant=None, Weapon weap=None)
    • Set the internal description variable to be the same as the input parameter desc
    • Set the internal creature variable to be the same as the input parameter occupant
    • Set the internal weap variable to be the same as the input parameter weap
  • void setRowCol(self, int r, int c): a mutator to set the row and col attributes at the same time
  • void printDescription(self): Print blank lines at the beginning and end of the method. This method prints the room's description, then prints a line describing the exits of the room. This line should take the form, "You may exit to the north" or "You may exit to the east, south, and west". If a room has no exits the line should read, "There are no exits". If there is a weapon or a creature in the room, it should print these out as well. Note that you are welcome to print the exits on separate lines (one line says, "There is an exit to the north", the next line says, "there is an exit to the south", etc.)
  • Room enterRoom(self, Creature player): This method is called when the player enters a room, and handles all user input and actions until the player leaves that room (at which point enterRoom() returns a reference to the Room object that the player enters next) or quits (at which point enterRoom() returns None). The method should:
  1. Call printDescription() to describe the room to the user
  2. Then enter a while loop that repeatedly:
    1. Calls menu.parse() to process the next line from the keyboard.
    2. Uses an if statement on the result of parse() to run the appropriate code for the action that the user typed. Note that these values are all defined in the menu module, so you need to call them menu.NORTH instead of NORTH. (Example: if result==menu.NORTH)
      • menu.NORTH: Check to see if the roomToNorth field is None. If so, print "There is no exit to the north". Otherwise, print "You walk to the north" and return roomToNorth as the return value from this method. Note that you are not returning a new Room object -- you are returning the value of the roomToNorth.
      • Similarly for menu.EAST, menu.SOUTH, and menu.WEST.
      • menu.LOOK: calls printDescription() to describe the room again.
      • menu.GET: Check to see if the item field is None. If so, print "There is nothing to get". Otherwise:
        • If the player does not already have a weapon (i.e., player.weapon.name is "bare hands"), then set the player.weapon field to the weapon in this room, and set the weapon in this room to None. Print a message like "You pick up the <weapon name>". Note that when a monster is initially created, it has a default weapon ("bare hands") created.
        • If the player is already holding a weapon (other than the default "bare hands" weapon) then the player should "drop" it first (i.e., swap the reference stored in player.weapon with that stored in self.item and print out an appropriate message). In other words, the self.item field should be set to the contents of player.weapon, and vice-versa. You will need to use a temporary Weapon variable to perform this swap.
      • menu.ATTACK: Check to see if the monster field is None. If so, print "There is nothing to attack". Otherwise, have the player try to attack the monster using the various methods in the Creature class (e.g., tryToAttack(), takeDamage(), constructHitString(), etc.) -- see the CombatSimulation.py for an example of how to do this. Be sure to print out the amount of hit points for both the player and the monster. Whether or not the attack succeeds in causing damage, set the monster's angry field to True to indicate that the monster will now attack the player. If the monster is no longer alive after the attack, print a message to indicate that the player is victorious and set the monster field to None.
      • menu.QUIT: return None from the enterRoom() method.
    3. Check whether the monster is angry (i.e., monster.angry == True) and if so, has the monster attack the player. If the player is no longer alive after the attack, print a message such as "You died." and return None.

Testing

Note: Notice that unlike the CombatSimulation class of the prior assignment, the combat is not carried through to completion when the user types "attack". Instead the combat happens one round at a time, requiring the player to type "attack" every turn if they want to keep attacking, but also allowing the player to leave the room if combat is going badly. Once a monster is angry, however, it will continue attacking the player as long as the player is in the same room as the monster.

You can test your Room, Creature, Weapon, and menu files via the RoomTest.py file. This test game sets up several Room, Creature, and Weapon objects (including the Creature object that represents the player), prints out an introduction, and enters a loop that repeatedly calls the enterRoom() method of the Room object in which the player is located.