Question: Creat a Class Called Game (JAVA) Class Fields Declare meaningful names with appropriate data types for each of these private instance fields: Multiple Rooms and
Creat a Class Called Game (JAVA)
Class Fields
Declare meaningful names with appropriate data types for each of these private instance fields:
Multiple Rooms and Items defined in the game (e.g. Kitchen, Basement, Candy)
an ArrayList of Items for what the player picks up along the way an optional item (Item)
a Room to store the players current location
a String to maintain the current message
Constructor
A constructor is a special method with the same name as the class and generally initializes the fields to appropriate starting values. Refer to section 3.7.
public Game () create all of the rooms by invoking the helper method defined below. Instantiate the ArrayList of Items. Set the current location to the starting location of the game.
Accessor Methods
An accessor method does not modify class fields. The names for these methods, which simply return the current value of a field, often begin with the prefix get. Refer to section 3.6 in the textbook.
public String getMessage () return the games message for another object to display. In this case, it will be the GUI object. DO NOT print the message.
Helper Methods
Helper methods are private and are only invoked from other instance methods.
private void createRooms () this private helper method instantiates the Rooms and Items. Identify all of the room neighbors. See the section below for more information about creating rooms.
private void setWelcomeMessage () this private helper method initializes the games message with a description of the game. DO NOT print the message.
private Item checkForItem (String name) this private helper method checks the ArrayList for the requested Item name. If found, return the Item. If not found, return null.
Mutator Methods
A mutator method performs tasks that may modify class fields. The names for these methods, which simply set a field with the parameter value, often begin with the prefix set. As a good programming practice, provide set methods for each class field. Refer to section 3.6 in the textbook.
public void help () update the games message with hints, suggestions and reminders about the game objective. DO NOT print the message.
public void look () update the games message with the current rooms long description. DO NOT print the message.
public void move (String direction) if appropriate, update the current location with the neighbor in the requested location. If not possible, the message should explain the player cannot move in that direction. See the sample code below for moving from one room to another.
public void list () update the games message with a list of all items the player is holding. If appropriate, the message should display the player is not holding anything.
public void pickup () if appropriate, remove the item from the room and add it to the ArrayList. Update the games message with one of the following options: 1) there is not item in the room to take, 2) the item is too heavy to take, or 3) the player is now holding the item.
public void drop (String item) if appropriate, remove the item from the ArrayList and add it to the current room. Update the games message with one of the following options: 1) the player is not holding that item, 2) the room already has an item, or 3) the player has successfully dropped the item in the room.
public void eat (String item) Update the games message with one of the following options: 1) the player is not holding that item, 2) the item is not edible, or 3) the player successfully eats the item and is no longer holding it.
public boolean gameOver () determine if the game has been won or lost. If so, update the games message with the news and return true. Otherwise, return false with no message. DO NOT print the message. This method is invoked in the GUI class after every command to determine if the game is over or not.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
