![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS206 Data Structures / 2016-Spring / Notebooks |
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) {
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" 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 "::".
String line = "place::pname::This is a description";
String[] parts = line.split("::");
parts[0]
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:
Download each of the above programs. Open them in the editor by clicking on their names in the Files tab.
%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"