CS 116 Lab 03

Overview

In this lab we'll continue work on the implementation of our interactive fiction engine.

The primary improvement will be to support an arbitrary number of rooms, as specified in the data file. A large number of rooms will be much too tedious to navigate sequentially, so we'll also build in support for a two-dimensional map, along with non-linear navigation.

Data file

In order to convey all this information to the IF engine, we'll have to enrich our data file format a bit. The following is an updated example (you can also download it):

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.
,
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
.
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.
,
S 2 the cave entrance
.
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
.

The specification has changed as follows:

  • the first line is now a number which indicates how many rooms are specified in the data file – it is followed by a '.' on a line by itself.
  • each room now has between 1 and 4 exits — each line describing an exit starts with a single character (N, S, E, or W) indicating the direction, followed by a number indicating the room that exit takes you to, then by the description of the exit.

Note that it is safe to assume that the rooms are specified in ascending order (in increments of 1) starting with room 1.

Behavior

The main difference between this and the previous iteration is that the f and b commands are now replaced by the n, s, e and w commands.

The following is a sample session from a working implementation running on the above data file:

Enter a command: l

Treasure room:

You are in 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.

To the north, there is a small door

Enter a command: n
You are now in the Cubby
Enter a command: l

Cubby:

You are in a tiny cubby. There's nothing of consequence here.

To the north, there is a looming entrance to a cave
To the south, there is a small door
To the east, there is a wardrobe

Enter a command: n
You are now in the Cavern
Enter a command: n
Can't move north from here
Enter a command: w
Can't move west from here
Enter a command: s
You are now in the Cubby
Enter a command: e
You are now in the Forest
Enter a command: l

Forest:

You are in a lush forest. The wardrobe clearly functions as a magical gateway 
between worlds. A narrow path through the forest stretches before you.

To the east, there is the forest path
To the west, there is a shimmering wall

Enter a command: e
You are now in the Middle of the forest
Enter a command: l

Middle of the forest:

You are in a densely forested passage. It stretches out interminably before you.

To the east, there is a meandering path
To the west, there is the way back

Enter a command: e
You are now in the Middle of the forest
Enter a command: e
You are now in the Middle of the forest
Enter a command: w
You are now in the Forest
Enter a command: w
You are now in the Cubby
Enter a command: s
You are now in the Treasure room
Enter a command: q
Quitting!

Implementation requirements

Room changes

You should modify your Room class so that it contains the following two arrays:

  • int[] exitDestinations
  • String[] exitDescriptions

Each entry in the first array will contain the number of the room which the exit in a given leads to. If there is no exit in a particular direction, the corresponding entry will contain -1. Each entry in the second array will contain the description of the exit in that direction.

In order to match array indexes to directions, you should also add the following constant definitions to your Room class.

public static final int NORTH = 0;
public static final int SOUTH = 1;
public static final int EAST  = 2;
public static final int WEST  = 3;

The following methods should also be added to Room:

  • public int getExitDestination(int direction)
  • public String getExitDescription(int direction)

In this way, a caller could do something like room.getExitDescription(Room.NORTH) to fetch the description of the northern exit of a room object.

REPL changes

Instead of reading in just three rooms, the main method in your REPL class will need to first read in the number of rooms, then proceed to create an array of rooms and initialize each of its entries by calling the (updated) Room constructor.

Your own data file

Again, please craft a separate data file containing your own room narratives/descriptions. Your data file should contain a minimum of 7 rooms, with at least 1 of those rooms containing 4 exits, and another 2 rooms containing 2 or more exits each. Save your file as "rooms-v2.dat" in a subdirectory called "datafiles" in your Eclipse project so that we can easily locate it.

Scoring

  • 5 points for correct behavior (per the sample session shown above) — note that random sessions will be tested
  • 5 points for nicely formatted output
  • 5 points for adhering to the implementation requirements faithfully
  • 5 points for your own (working) data file