CS 116 Lab 06

Lab Overview

For this lab you'll be adding an inventory system to your interactive fiction engine. A new Item class will be used to store name and description information about items, which will in turn be maintained in a separate list for each room, and one for the player's current inventory.

When look-ing at a room, the names (not detailed descriptions) of all available items should be listed.

You will be adding the following new commands/actions to the REPL:

take itemNamePrefix
Removes an item from the current room and adds it to the user's inventory. Note that any starting substring of the item's name can be used to specify the item to be picked up — case should be ignored in the comparison, too.
drop itemNamePrefix
Removes an item from the user's inventory and adds it to the current room. As above, the name can be abbreviated.
x itemNamePrefix
"Examines" an item in the user's inventory by printing out its detailed description. As above, the name can be abbreviated.
i
Lists the names of all items in the user's inventory.

See the lab screencast for a live walkthrough of the updated REPL. Note that coverage of the lab begins at around 23:20 (before that I recap material on the list implementation, predicates, and anonymous classes).

To make your lab easier to test, you should include a boolean flag in your REPL that allows us to toggle monster battles — when set to false, monsters should never spawn.

Implementation Details

You must make use of the List interface and one of the implementations we started working on together in class — the most recent changes were carried out on the ExpandingArray implementation, so that might be the better implementation choice, but if you're feeling ambitious you can carry out the same changes on the LinkedList implementation. Either way, you should maintain a List<Item> in each room and in the REPL itself to keep track of the user's inventory. You should also update the REPL so that the vanquished monsters list is a List<Monster>.

As described in the screencast, you should implement the following new methods in the List interface and make use of them in your IF engine updates:

public boolean contains(Predicate<T> pred);

public T get(Predicate<T> pred);

public T remove(Predicate<T> pred);

The Predicate class used in the screencast is given below, for your convenience:

public interface Predicate<T> {
    public boolean evaluate(T o); 
}

Feel free to come up with your own updated format for the rooms data file — it must contain a section for each room wherein each item initially placed in that room is given a name and description. This is in addition, of course, to the monster data added in the previous lab.

The following is the file I use in the screencast, for reference:

5
.
1
Treasure room
a huge room, filled with gold coins and gems of all sorts imaginable. 
In the corner of the room there is a dragon chained to a radiator.
,
Sack of gold : A huge sack of gold
Bag of diamonds : A massive bag of shiny diamonds
Rucksack of rubies : An immense rucksack of red rubies
,
N 2 a small door
.
2
Cubby
a tiny cubby. There's nothing of consequence here.
,
,
N 3 a looming entrance to a cave
S 1 a small door
E 4 a wardrobe
,
Pterodactyl
0.5
.
3
Cavern
an enormous, pitch-black cavern. You can't see anything, but you hear a
distinct rumbling from the far side. It's probably a bad idea to stick 
around to find out what's producing it.
,
Ring : One Ring to rule them all, One ring to find them; One ring to bring them all and in the darkness bind them.
,
S 2 the cave entrance
,
Vampire Pterodactyl
0.3
.
4
Forest
a lush forest. The wardrobe clearly functions as a magical gateway 
between worlds. A narrow path through the forest stretches before you.
,
,
W 2 a shimmering wall
E 5 the forest path
.
5
Middle of the forest
a densely forested passage. It stretches out interminably before you.
,
,
W 4 the way back
E 5 a meandering path
.

Scoring

  • 5 points for the updated Room class (needed to read in the new item information)
  • The following are contingent on making use of an implementation of our own List interface:
    • 5 points for correctly listing items in each room
    • 5 points for a working take
    • 5 points for a working drop
    • 5 points for a working inspect
  • 5 points for maintaining all previous functionality!