Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
6th Edition
ISBN: 9780134477367
Author: David J. Barnes, Michael Kolling
Publisher: PEARSON
Question
Book Icon
Chapter 8, Problem 29E
Program Plan Intro

Program plan:

The variables used in the program are given below:

  1. Player: a class representing the player in the game
  2. Room: a class representing a room in the game
  3. Item: a class representing an item in the game
  4. Command: a class representing a command that can be executed by the player in the game.

The methods used in the program are as follows:

  1. execute(): executes the take command, which allows the player to pick up an item from the current room and add it to their inventory
  2. hasSecondWord(): a method inherited from the Command class, which checks whether the player provided a second word (i.e. an item name) with the command
  3. getRoom(): a method in the Player class, which retrieves the current room that the player is in
  4. getItem(): a method in the Room class, which retrieves an item from the room by name
  5. pickUpItem(): a method in the Player class, which attempts to add an item to the player's inventory. It returns true if the item is successfully added, and false if it is too heavy to carry.
  6. removeItem(): a method in the Room class, which removes an item from the room.
  7. addItem(): a method in the Room class, which adds an item to the room

  8. LookCommand(): a constructor in the LookCommand class, which creates a new LookCommand object with a reference to the current player. This command is used to display the room description and contents after an item has been dropped.

Program Description:

To implement an extension that allows the player to pick only 1 item.

Program

TakeCommand.java

public class TakeCommand extends Command
{
    private Player player;

    /&
     * Constructor for objects of class TakeCommand
     */
    public TakeCommand(Player player)
    {
        this.player = player;
    }

    public void execute()
    {
        if (!hasSecondWord()) {
            System.out.println("Take what");
            return;
        }
        
        Room room = player.getRoom();
        Item item = room.getItem(getSecondWord());
        if (item != null) {
            if (player.pickUpItem(item)) {
                // the item is no longer in the room
                room.removeItem(item);
                Command command = new ItemsCommand(player);
                command.execute();
            }
            else {
                System.out.println("You cannot possibly pick this up!");
            }
        }
        else {
            System.out.println("Take what?");
        }        
    }
}

DropCommand.java

/&
 * The player gets an item out of his bag and drops it in to the room.
 
 */
public class DropCommand extends Command
{
    private Player player;

    /&
     * Constructor for objects of class DropCommand
     */
    public DropCommand(Player player)
    {
        this.player = player;
    }

    public void execute()
    {
        if (!hasSecondWord()) {
            System.out.println("Drop what");
            return;
        }     
        
        Item item = player.dropItem(getSecondWord());
        if (item != null) {
            Room room = player.getRoom();
            room.addItem(item);
            Command command = new LookCommand(player);
            command.execute();
        }
        else {
            System.out.println("Drop what?");
        }        
    }
}

Explanation:

The modified "Zuul" game is a text-based adventure game in which the player navigates through a series of rooms, interacting with items and solving puzzles to progress through the game. The game includes two new commands, "take" and "drop," which allow the player to pick up and carry one item at a time. The game is implemented using object-oriented programming principles in Java, with separate classes for the player, rooms, items, and commands. The player moves through the game by typing commands into the console, which are then interpreted and executed by the program. The game includes several features, including a command parser, a random number generator for item placement, and an inventory system for the player.

TakeCommand and DropCommand are two classes in the Zuul project that extend the Command class and allow the player to pick up and drop items in the game. TakeCommand is executed when the player types the "take" command followed by the name of an item in the current room. The command checks if the item is in the room and if it is not too heavy for the player to carry. If both conditions are met, the item is removed from the room and added to the player's inventory. DropCommand is executed when the player types the "drop" command followed by the name of an item in their inventory. The command checks if the item is in the inventory and if so, removes it and adds it to the current room. Both commands use methods in the Player, Room, and Item classes to interact with the game world and modify the state of the player and the game.

OUTPUT:

You are in the living room.
Exits: north, east, west
Items: couch, lamp, remote control

> take lamp
You took the lamp.

> take remote
You took the remote control.

> take book
Take what?

> go north
You are in the kitchen.
Exits: south
Items: fridge, stove, knife

> take fridge
You cannot possibly pick this up!

> take knife
You took the knife.

> go south
You are in the living room.
Exits: north, east, west
Items: couch, lamp, remote control, knife

> drop remote
You dropped the remote control.

> look
You are in the living room.
Exits: north, east, west
Items: couch, lamp, knife

> drop lamp
You dropped the lamp.

> go east
You are in the dining room.
Exits: west
Items: table, chair

> drop chair
You cannot drop something that you don't have!

> drop knife
You dropped the knife.

> look
You are in the dining room.
Exits: west
Items: table, chair, knife

Blurred answer
Students have asked these similar questions
create an array of "Opinion" objects called "poll" that will represent five students opinions of a class.
Create a VisualCounter class that supports both increment and decrement operations. Take the constructor inputs N and max, where N indicates the maximum number of operations and max specifies the maximum absolute value for the counter. Create a plot that shows the value of the counter each time its tally changes as a side effect.
The first application you will complete simulates a fight with the mythical greek hydra. As legend goes, if you were to chop off the head of a hydra, two smaller heads would grow back in its place. In order for our fight to have an end, we will assume that once the size of the targeted head is small enough, no new heads will grow back in its place. The goal of this application is to determine the amount of work required to kill a hydra with a single head, when the size of the head is given as input. Final output would look like this The head bag is Bag[ 3 ]The head bag is Bag[ 2 2 ]The head bag is Bag[ 2 1 1 ]The head bag is Bag[ 2 1 ]The head bag is Bag[ 2 ]The head bag is Bag[ 1 1 ]The head bag is Bag[ 1 ]The head bag is Bag[ ]The number of chops required is 7Run the program again using 4 for the size of the initial head. The work done should be 15.Run the program again using 5 for the size of the initial head. You should get the computation ended early.Run the program again using 6…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education