Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For a final project, my group and I are working on a text-based adventure game. The way the game works is users are presented with 3 options. For example:
1. Go to the saloon
2. Go to the general store
3. Go to the sheriff's office

The player selects 1 option by inputting the corresponding number and based on their choice they are led down different paths. My problem is I know that if we used a bunch of if/else statements it will not be good for the grade. My professor mentioned something about using a tree of linked nodes to implement the options chart. How should I go about doing that

What I have tried:

We have used if/else statements to progress the game
Posted
Updated 25-May-20 18:34pm

1 solution

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;
    }

    // other features ...

}


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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900