Game Design

Today, we will play your Text Adventure game.


Consider having LinkedLists of different types. We would need:

public class Node 
    String data;
    Node(String data) { = data;
public class Node 
    Double data;
    Node(Double data) { = data;

But Java has a better way, called Generics, and it works like this:

public class Node<T>
    T data;
    Node(T data) { = data;

It treats a type as if it were a variable. You fill in the variable when you create an instance:

Node<String> snode = new Node<String>("Hello");
Node<Double> dnode = new Node<Double>(34.56);

NOTE: if you use this, you can't assume anything about T. You could though make T implement an interface, though.


"Parsing" is just breaking a string up into its parts. We have designed our game files to be easy to parse. We just need to "split" a string based on "::".

In [1]:
String line = "place::pname::This is a description";
|  Added variable line of type String with initial value "place::pname::This is a description"

In [3]:
String[] parts = line.split("::");
|  Added variable parts of type String[] with initial value [Ljava.lang.String;@5d3411d

In [4]:
|  Expression value is: "place"
|    assigned to temporary variable $4 of type String


Warning: if any of your names have spaces on either side, you may wish to edit the file to remove them, or to use String.strip() to remove them.


We will need seven classes for our game:

In-class Activity

Download each of the above programs. Open them in the editor by clicking on their names in the Files tab.


In a terminal, you can compile them all with:

javac *.java

or one-at-a-time with:


To run the Game:

java Game ""
  • There are four missing bits of code in
    • when you read in a new object (ie, Thing), you need to put it in the room
    • when you read in a new command (ie, Action), you need to put it in the room
    • you need to be able to "pick up OBJECT"/"drop" an object into/from backpack
    • when you attempt to go from one room to another, you need to find that command in the list of actions for that room, make sure you have the required objects/Things, and then move to that room