Game Design

Today, we will play your Text Adventure game.

Generics

Consider having LinkedLists of different types. We would need:

public class Node 
    String data;
    Node(String data) {
        this.data = data;
    }
}
public class Node 
    Double data;
    Node(Double data) {
        this.data = data;
    }
}

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

public class Node<T>
    T data;
    Node(T data) {
        this.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

"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]:
parts[0]
|  Expression value is: "place"
|    assigned to temporary variable $4 of type String

Out[4]:
place

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.

Game

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.

https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks

%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Game.java
%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Place.java
%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Thing.java
%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Action.java
%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/LinkedList.java
%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Stack.java
%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Node.java

In a terminal, you can compile them all with:

javac *.java

or one-at-a-time with:

javac Game.java

To run the Game:

java Game "filename.game"
  • There are four missing bits of code in Game.java:
    • 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