adventure.game
Class GameState

java.lang.Object
  extended by adventure.game.GameState
All Implemented Interfaces:
java.awt.event.ActionListener, java.util.EventListener

public class GameState
extends java.lang.Object
implements java.awt.event.ActionListener

The GameState class is the heart of the adventure game. It contains code for setting up and managing the rooms and items, for handling user commands, and for updating the display.

The adventure game works on a three-tier system.

  1. At the top level, the GameState class represents the state of the entire game. It uses Room objects as locations, and Item objects as things. It keeps track of all locations and objects, and it gets the first opportunity to handle user commands. It is also responsible for updating the display when needed.

    Students can add additional commands to the GameState, but they should do so only if that command is universal. If the command is specific to a room or item, it should be handled in the relevant room or item class.

  2. Next, Room objects (locations) take care of commands that are specific to rooms. They also keep track of any room-specific state. Finally, room objects are responsible for printing / drawing their own descriptions.

    In my design every room in the game is a separate class. Every room class will have exactly one room object created from it. Each room object has a name (as a String) that can be used to identify that room. Given a room name as a String, you can get that Room object by calling the appropriate method in this class.

    Room objects get to process commands immediately after the GameState object. Only the current room object gets a chance to process the user's commands. Rooms are responsible for movement commands, and students should add other commands to rooms only if the command will change some state in that room (such as opening a door).

    Room objects also have a method for redrawing the view. This is entirely optional.

  3. Finally, Item objects (things) represent things the user can carry. They always exist somewhere (in a room). Typically they are placed in some starting room. There is a special room named "Inventory" - this special room represents the player's backpack. When an item is in the "Inventory" room, the player is carrying it.

    In my design every item in the game is also a separate class. Every item class will have exactly item object created from it. Each item object has a name (as a String) that can be used to identify that item. Given a item name as a String, you can get that Item object by calling the appropriate method in this class.

    Item objects process commands last. Only items in the current room and items in the user's inventory get a chance to process the user's commands.

    Item objects are responsibile for handling interactions between items and describing and drawing themselves.

The GameState object that represents the adventure game has useful variables and methods:

On the contest take-home problem web page is a tutorial for adding a room and an item to the game. This is a great way to learn more about this application.


Field Summary
 Room currentRoom
          The current location of the player, as a Room object.
 OutputPanel output
          Contains the OutputPanel object that corresponds to the text area of the GUI display.
 
Constructor Summary
GameState(javax.swing.JTextField input, OutputPanel output)
          The adventure game GameState constructor.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent arg0)
          This is an event listener method that listens to the user's text field.
 void addNewItem(Item newItemObject, java.lang.String roomName)
          A method to add an item to the game.
 void addNewRoom(Room newRoomObject)
          A method to add a room to the game.
 void draw(java.awt.Graphics g, int windowWidth, int windowHeight)
          The method to draw the game.
 java.util.List<Item> getInventoryItems()
          Returns a list of item objects that the player is carrying.
 Item getItem(java.lang.String itemName)
          A method to get an Item (as an Item object) given its name.
 Room getLocation(Item item)
          A method to get the location of an Item.
 Room getRoom(java.lang.String roomName)
          A method to get a room (as a Room object) from its name.
 java.util.List<Item> getRoomItems(Room room)
          Returns a list of item objects that exist in some particular room.
 void setCurrentRoom(Room newCurrentRoom)
          A method to set the current room.
 void setCurrentRoom(java.lang.String roomName)
          A method to set the current room.
 void setItemLocation(Item item, Room room)
          A method to set the location of an item.
 void setItemLocation(Item item, java.lang.String roomName)
          A method to set the location of an item.
 void setItemLocation(java.lang.String itemName, Room room)
          A method to set the location of an item.
 void setItemLocation(java.lang.String itemName, java.lang.String roomName)
          A method to set the location of an item.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

output

public OutputPanel output
Contains the OutputPanel object that corresponds to the text area of the GUI display. Use the addText method on this object, followed by the updated method on this object, to display text for the user.


currentRoom

public Room currentRoom
The current location of the player, as a Room object.

Constructor Detail

GameState

public GameState(javax.swing.JTextField input,
                 OutputPanel output)
The adventure game GameState constructor. Creates all the Room objects, Item objects, adds them to the GameState, and prints a greeting message for the player.

Students should not call this constructor directly (or create GameState objects), the GUI creates a GameState object when it is built.

Parameters:
input - the text field from which the user's input will come.
output - the panel where all output text goes.
Method Detail

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent arg0)
This is an event listener method that listens to the user's text field. It is called automatically whenever the return key is pressed in the input text field.

Students should not call this method directly.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
arg0 - unused, ignored

addNewRoom

public void addNewRoom(Room newRoomObject)
A method to add a room to the game. It is very important that each room object have a different name, so at most one Room object of a type should be added.

This method is currently only called from the GameState constructor, but it could be called at other times.

Parameters:
newRoomObject - the room to be added

addNewItem

public void addNewItem(Item newItemObject,
                       java.lang.String roomName)
A method to add an item to the game. It is very important that each item object have a different name, so at most one Item object of a type should be added.

Parameters:
newItemObject - the item to be added
roomName - the room in which the item is to be placed

setCurrentRoom

public void setCurrentRoom(java.lang.String roomName)
A method to set the current room. This method changes the game state, it records the current room that the player is in. This method should be called whenever you want to change which room the player is in.

Use the name of the room (as a String) as the parameter to this method.

Parameters:
roomName - the name of the room that is to be set as the current room

setCurrentRoom

public void setCurrentRoom(Room newCurrentRoom)
A method to set the current room. This method changes the game state, it records the current room that the player is in. This method should be called whenever you want to change which room the player is in.

Use a room object as the parameter to this method.

Parameters:
newCurrentRoom - the Room object that will become the current room

setItemLocation

public void setItemLocation(java.lang.String itemName,
                            java.lang.String roomName)
A method to set the location of an item. Use this method to pick up items by setting the location to "Inventory". You can also drop items or make them move between rooms by specifying a room name.

Parameters:
itemName - the name of the item
roomName - the name of the room in which the item is to be placed

setItemLocation

public void setItemLocation(java.lang.String itemName,
                            Room room)
A method to set the location of an item. Similar to the method above, but the room is specified as a Room object.

Parameters:
itemName - the name of the item
room - the room object in which the item is to be placed

setItemLocation

public void setItemLocation(Item item,
                            java.lang.String roomName)
A method to set the location of an item. Similar to the method above, but the item is specified as an item object.

Parameters:
item - the item to be placed.
roomName - the name of the room in which the item is to be placed.

setItemLocation

public void setItemLocation(Item item,
                            Room room)
A method to set the location of an item. Similar to the method above, but both the item and the room are specified as objects.

Parameters:
item - the item to be placed
room - the room in which the item is to be placed

getRoomItems

public java.util.List<Item> getRoomItems(Room room)
Returns a list of item objects that exist in some particular room.

Parameters:
room - any room, including the inventory room
Returns:
a list of Item objects

getInventoryItems

public java.util.List<Item> getInventoryItems()
Returns a list of item objects that the player is carrying.

Returns:
a list of Item objects

getRoom

public Room getRoom(java.lang.String roomName)
A method to get a room (as a Room object) from its name.

Parameters:
roomName - the name of the room requested
Returns:
the Room object, null if room does not exist

getItem

public Item getItem(java.lang.String itemName)
A method to get an Item (as an Item object) given its name.

Parameters:
itemName - the name of the item
Returns:
the Item itself, null if item does not exist

getLocation

public Room getLocation(Item item)
A method to get the location of an Item. The location is returned as a room object. Note that the name of the room object will be "Inventory" if the player is carrying the item.

Parameters:
item - the item in question
Returns:
the room (as a Room object) where the item is located

draw

public void draw(java.awt.Graphics g,
                 int windowWidth,
                 int windowHeight)
The method to draw the game. It simply draws the room, then draws the items in the room.

Parameters:
g - the Graphics object to do the drawing
windowWidth - the width of the window
windowHeight - the height of the window