Let's say node '0' is called the root node at the top of the 'tree', 'You are in the entrance hall'
If you had a tree implementation, then, you could go
root.AddChild node1 'Go to the saloon'
root.AddChild node2 'Go to the general store'
root.AddChild node3 'Go to the sheriff's office'
So, at node0, the root / starting point, you display all the children and get those options ... so, lets say the player choses node2/'General Store' and you had this :-
node2.AddChild node20 'Go to the freezer'
node2.AddChild node21 'Go to the Meat section'
node2.AddChild node22 'Go to the bakery'
you then have 3 options for what to do in the 'general store', you display the children of node2 to get the next available paths
you could also have just one path, eg,
node3.AddChild node30 'You're in the Cell'
or a dead end, no children/paths, unless some other condition is met - eg, for all the paths in the general store to show up you might need to have picked up cash along the way
Obviously, you need to derive a tree (can't remember if java has a tree data structure), and be able to read the tree from a file for instance, to build the 'paths'
Something like this
public class TreeNode<T> implements Iterable<TreeNode<T>> {
T data;
TreeNode<T> parent;
List<TreeNode<T>> children;
public TreeNode(T data) {
this.data = data;
this.children = new LinkedList<TreeNode<T>>();
}
public TreeNode<T> addChild(T child) {
TreeNode<T> childNode = new TreeNode<T>(child);
childNode.parent = this;
this.children.add(childNode);
return childNode;
}
}
Could be set up manually like
TreeNode<String> root = new TreeNode<String>("You are in the entrance hall");
{
TreeNode<String> node1 = root.addChild("Go to the saloon");
TreeNode<String> node2 = root.addChild("Go to the general store");
{
TreeNode<String> node20 = node2.addChild("Go to the freezer");
TreeNode<String> node21 = node2.addChild("Go to the Meat section");
TreeNode<String> node22 = node2.addChild("Go to the bakery");
}
TreeNode<String> node3 = root.addChild("Go to the sheriff's office");
{
TreeNode<String> node30 = node3.addChild("You're in the Cell");
}
}
See
GitHub - gt4dev/yet-another-tree-structure: Sample tree structure for C# / Java with iterator and search[
^] for instance
I would build the 'game tree' in a seperate program, and serialize it to a file then in your actual game, all you need do is read the tree back, but get it going with the hard-coded layout first ...
Hopefully thats enough of a start