Wyrmbeard
Wyrmbeard

Reputation: 45

Adding and Dropping Inventory Items in Java

I'm making a text based adventure game.

Here's the parts of the question I'm stuck on;

  1. Add a getter method (called getItems()), so that other parts of your game, like the Game class, can get access to the items that are inside each room. (We do not need a setter method, because the Java ArrayList objects are mutable, so we can modify the contents of a list by using its add and remove methods).
  2. Extend the Room.describe() method so that it prints all the items in the room. Hint: use the special 'for' loop that was shown in the lecture:

    for (String s:items) {System.out.println(" You can see: " + s);
    
  3. In your Game constructor, add one or two interesting items to each room, like this:

    lounge.getItems().add("balloon");
    

    Make sure that all your items have lowercase names.

Here's the Game Class:

package TextGame;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Game {

private Room hallway;
private Room lounge;
private Room bedroom;
private Room door;
private Room porch;
private Room kitchen;
private Room bathroom;
private Room basement;
private Room letter;
private Room current;

List<String> inventory = new ArrayList<String>();

public Game() {
    hallway = new Room("You are in a dark hallway.\n"
        + "There is a table beside you, and a light to the west.\n");
    lounge = new Room("You are in a brightly-lit lounge, with two red sofas.\n"
        + "A stereo is playing soft music.\n");
    bedroom = new Room("You are in a bedroom.\n"
        + "You can see a bunk bed, two cricket bats, and an aquarium.\n");
    door = new Room("There is a large door in front of you that leads into the house\n"
            + "You need a key to open it");
    porch = new Room("You are standing outside the house.\n"
            + "There is a front door to the west and a letter box to the south");
    kitchen = new Room("You're in a kitchen and make a sandwhich");
    basement = new Room("You go down stairs to find a large dim-lit basement");
    letter = new Room("You look inside the letterbox, there are keys in here!\n");
    letter.items.add("house keys");
    current = porch;
    porch.connectWest(door);
    letter.connectNorth(porch);
    door.connectWest(hallway);
    hallway.connectWest(lounge);
    bathroom.connectNorth(hallway);
    lounge.connectNorth(kitchen);
    lounge.connectWest(bedroom);
    basement.connectNorth(bedroom);
}
/**
 * Play the interactive text game.
 *
 * Reads commands from the given input, line by line.
 *
 * @param in
 */
public void play(Scanner in) {
    current.describe();
    while (current != porch) {
        String line = in.nextLine().toLowerCase();
        if (line.equals("quit")) {
            System.out.println("You gave up!");
            break;
        } else if (line.startsWith("go ")) {
            current = current.move(line.substring(3));
            current.describe();
        } else if (line.startsWith("take ")) {
            take(line.substring(5));
        } else {
            System.out.println("Unknown command '" + line + "'.  Try go/take/quit.");
        }
    }
}

public void take(String item) {
    if(item.room == True) {
        inventory.add(item);        
    }
}

public void drop(String item) {
    if(item.inventory == True) {
        inventory.remove(item);
    }
}

/** Starts the whole game. */
public static void main(String[] args) {
    Game game = new Game();
    game.play(new Scanner(System.in));
}
}

and here's the room class;

package TextGame;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Room {
private String description;
private Room north;
private Room east;
private Room south;
private Room west;
private ArrayList<String> items = new ArrayList<>();

public getItems() {     
}

public Room(String desc) {
    description = desc;
}

public void connectWest(Room other) {
    this.west = other;
    other.east = this;
}

public void connectNorth(Room other) {
    this.north = other;
    other.south = this;
}

public void describe() {
    System.out.println(description);
}

public Room move(String dir) {
    Room nextRoom = null;
    if (dir.equals("north")) {
        nextRoom = north;
    } else if (dir.equals("east")) {
        nextRoom = east;
    } else if (dir.equals("south")) {
        nextRoom = south;
    } else if (dir.equals("west")) {
        nextRoom = west;
    } else {
        System.out.println("Error: unknown direction " + dir);
    }
    if (nextRoom == null) {
        System.out.println("You cannot go " + dir + " from here.");
        nextRoom = this;
    } else {

    }
    return nextRoom;
}   
}

Upvotes: 2

Views: 2425

Answers (1)

Les
Les

Reputation: 241

These are methods for the Game class. Some of your tasks' descriptions are a bit vague, but I tried to fit them.

Add a getter method.

public List<String> getItems() {
   return items;
}

Extend the Room.describe() method so that it prints all the items in the room.

public void describe() {
    final StringBuilder itemsStr = new StringBuilder("");       
    for (String item : items) {
       itemsStr.append(" ").append(item);
    }        
    System.out.println("You can see:" + itemsStr.toString());
}

In your Game constructor, add one or two interesting items to each room, like this: lounge.getItems().add("balloon"); Make sure that all your items have lowercase names.

public boolean addItem(String item) {
    if (item != null) {
         items.add(item.toLowerCase());
         return true;
    } else {
         return false;
    }      
}

Upvotes: 2

Related Questions