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 to -1)
  • int col: will contain the column location of the room (default to -1)
  • Room roomToNorth: contains a reference to the Room object reached by going north from this room, or null if no such Room exists.
  • Room roomToEast: contains a reference to the Room object reached by going east from this room, or null if no such Room exists.
  • Room roomToSouth: contains a reference to the Room object reached by going south from this room, or null if no such Room exists.
  • Room roomToWest: contains a reference to the Room object reached by going west from this room, or null if no such Room exists.
  • String description: a String containing a textual description of the room, e.g. "This is the kitchen."
  • Creature monster: a reference to the Creature object inhabiting this room, or null if the room is empty of monsters.
  • Weapon item: a reference to the Weapon object in this room, or null if the room has no weapon lying in it.

Methods

The Room class should have the following methods:

Constructors

  • Room(): A default constructor that sets description to "A nondescript room." and monster and item to null.
  • Room(String desc): A specific constructor that sets description to desc and monster and item to null.
  • Room(String desc, Creature occupant, Weapon weap): a specific constructor that sets the description, monster, and item fields.

Accessors/Mutators

  • get/set pairs of accessor/mutator methods for all the attributes.
  • setRowCol(int r, int c): a mutator to set the row and col attributes at the same time

Other methods

  • void printDescription(): This method prints description to System.out, 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(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 null). The method should:
  1. Call printDescription() to describe the room to the user
  2. Then enter a while loop that repeatedly:
      1. Calls Parser.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 Parser class, so you need to call them Parser.NORTH instead of NORTH.
        • Parser.NORTH: Check to see if the roomToNorth field is null. 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 Parser.EAST, Parser.SOUTH, and Parser.WEST.
        • Parser.LOOK: calls printDescription() to describe the room again.
        • Parser.GET: Check to see if the item field is null. If so, print "There is nothing to get". Otherwise:
          • If the player does not already have a weapon (i.e., player.getWeapon.getName() is "bare hands"), then set the player.weapon field to the weapon in this room, and set the weapon in this room to null. 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 this.weapon and print out an appropriate message). In other words, the this.weapon 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.
        • Parser.ATTACK: Check to see if the monster field is null. 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.java from HW J6 for an example of how to do this.. 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 null.
        • Parser.QUIT: return null from the enterRoom() method.
      3. Checks whether the monster is angry (i.e., monster.getAngry() == 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 null.

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 Parser classes via the Media:Game.java file. The Game class provides the main() method for the game program, which 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.