CS 116 Lab 02
Overview
In this lab we'll start work on the implementation of an interactive fiction (aka "text adventure") engine that we'll continue to build on and improve throughout the semester.
On completing this lab, we should have a simple working read-eval-print loop (REPL) for our engine, and the ability to process a data file containing specifications for three separate rooms in our game world.
Data file
You can download a sample data file for use with your engine — the contents of the file itself are shown below, for reference.
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. , B no exit F a small door . 2 Cubby a tiny cubby. There's nothing of consequence here. , B a small door F a looming entrance to a cave . 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. , B the cave entrance F pitch darkness .
Note that the specification for each room begins with a room number and is terminated by a period on a single line. Between the room number and period are the following items:
- the title of the room (occupying a single line)
- a long description of the room (potentially occupying multiple lines), terminated by a comma on a line by itself
- a description of the backward exit, prefaced by the character 'B'
- a description of the forward exit, prefaced by the character 'F'
Behavior
When run, your program will read and parse the contents of the data file,
generating three separate Room
objects (i.e., based on a class of your design
called Room
), and will then enter a loop where the user is asked to enter one
of the following commands:
l
(for "Look")f
(for "go Forward")b
(for "go Backward")q
(for "Quit")
At the outset, the current room will be set to the first room read from the
data file. The l
command will print out the title, long description, and exit
information for the current room. The f
and b
commands will move forwards
and backwards from the current room (if possible), then print out the title of
the new room. Note that moving backwards from room 1 and forwards from room 3
are not possible.
The following is a sample session from a working implementation:
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. In front, there is a small door Behind, there is no exit Enter a command: f You are now in the Cubby Enter a command: l Cubby: You are in a tiny cubby. There's nothing of consequence here. In front, there is a looming entrance to a cave Behind, there is a small door Enter a command: f You are now in the Cavern Enter a command: l Cavern: You are in 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. In front, there is pitch darkness Behind, there is the cave entrance Enter a command: f Can't move forward from here. Enter a command: b You are now in the Cubby Enter a command: b You are now in the Treasure room Enter a command: b Can't move back from here. 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. In front, there is a small door Behind, there is no exit Enter a command: q Quitting!
Implementation requirements
You will need to create a Room
class that has the following properties and
minimal API:
- Properties:
int id
String title
String description
String frontExitDescr
String backExitDescr
- Constructor:
Room(java.util.Scanner)
- the constructor will read data from the provided scanner to initialize all its properties
- Getters for
title
,description
,frontExitDescr
, andbackExitDescr
- Your
main
method will reside in a class calledREPL
.main
will do the following (at minimum):- initialize two separate
Scanner
objects — one forSystem.in
, and one for the data file - three
Room
variables that will be initialized with the data fileScanner
- enter a loop predicated on some
gameOver
boolean flag, which is used to process commands and produce the necessary output (typically obtained by querying the relevantRoom
object)
- initialize two separate
Your own data file
As a final touch, please craft your own data file containing your own room
narratives/descriptions — each room description should span at least two
lines. Be sure to test it with your implementation. Save and commit it using a
different file name so that we can easily test it by simply switching the name
specified in your main
method.
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