Question

My question is about scalable logic branching.

Is there an elegant way to do branching logic trees in java (although I've always thought that they look more like root systems, but that's beside the point). I'm trying to develop a very simple text based adventure game as a side project to my studies, but I'm not sure what the best way to go about navigating these large logic systems is.

What I'm trying currently is an array that holds four values: stage, location, step, choice.

[EDIT - added choice variable to store user choice, changed name to reflect actual name in my code so that I don't get confused later]

int[] decisionPoint = {stage, location, step, choice};

A stage is supposed to represent a single major part of the tree. A location is supposed to represent my location within the tree. A step is supposed to represent my progress through a given location. Choice is the user input

At the moment, since I'm only dealing with a single tree, stage isn't being used much. Location and step are working well, but any time I get into a decision within a step the system breaks down.

I could keep creating more and more variables to represent deeper and deeper layers into the tree, but I feel like Java probably provides a better solution somewhere.

Currently, I'm using switch statements to figure out where in the program I am based on the values stored in nextQuestion. Is there something better? Or, is there a way to extend the array beyond what I'm using here to make it a bit more polymorphic (in the methods for the individual questions/text/whatever could I just have it create a larger array from a smaller one? Could I pass a smaller array as an argument but define the parameter as a larger array?)

//Switch example
switch(LocationTracker.getLocation()) { //start location finding switch
    case 1 : //Location 1
        switch (LocationTracker.getStep()) {//start location1 switch
            case 1 :
                location1s1(graphicsStuff);
                break;
            case 2 :
                location1s2(graphicsStuff);
                break;
         } break; //end location1 switch
    case 2 : //Location 2
        switch (LocationTracker.getStep()) {
            //same stuff that happened above
        } break;

Everything I find online just brings me to irrelevant pages about different online survey creators that I can use. If I could view their source-code that'd be kind of nice, but since I can't, I'm hoping you guys can help. :)

[EDIT]

Wow, what a nice response in such a short time at such an early hour!

I'll try to go into very explicit detail about how I'm solving the problem right now. It's worth mentioning that this does technically work, it's just that every time I need a branch inside a branch I have to create another variable inside a string array to keep track of my position, and really I'm just fishing for a solution that doesn't need an infinitely expanding string as the program becomes more and more complex.

Right now I have a program with 5 classes: The Main Class which starts the GUI The GUI class which provides three services: userInput, userOptions, and outputArea.

The DecisionTreeStage1 class which handles the logic of my problem at the moment (using switch statements). The LocationTracker class which is designed to track my location within the DecisionTreeStage1 class The DialogueToOutput class which changes the options that the users have, and also updates the output fields with the results of their actions.

Special point of interest: I want to have multiple decision branches at some point, and one main tree (maybe call it Yggdrasil? :D). For now, the DecisionTreeStage1 represents a very isolated system that isn't planning to go anywhere. I hope to use the stage variable stored in my array to move from one major branch to the next (climbing the tree if you will). My current implementation for this just uses nested switch statements to decide where I'm going. This imposes an annoying limitation: every time my path gets deeper and deeper I need another variable in my array to store that data. For example:

//Switch example deeper
switch(LocationTracker.getLocation()) { //start location finding switch
    case 1 : //Location 1
        switch (LocationTracker.getStep()) {//start location1 switch
            case 1 :
                switch(LocationTracker.getChoice()) {//Make a decision at this step based on the user choice

Given this example, what if the user choice doesn't just lead to some logic? (In this case, just an update to the outputArea) What if it leads to ANOTHER branching path? And that leads to another branching path? Eventually, I would want all paths to converge on the same spot so that I could move to the next "stage."

My real hope is to make this problem infinitely scalable. I want to be able to go as deep into one branch as I need to, without having to create a static and arbitrary number of variable declarations in my decisionPoint array every time.

I haven't been able to find much information about this, like I said.

Let me try presenting this question: Are there any other branching logic statements other than:

if(something)
    stuff;
else
    otherStuff;

and

switch(something) {
    case 1:
        stuff;
        break;
    case 2:
        otherStuff;
        break;

And if so, what are they?

PS - I know about the ternary if statement in Java, but it doesn't seem useful to what I'm doing. :)

Was it helpful?

Solution

You can build normal tree structures in Java, similar to the trees that can be built in C. Regardless if object references are theoretically pointers or not, they substitute pointers nicely in the tree constructions:

class Node {
  Node left;
  Node right;
  Node parent;
}

You can also build graphs (cyclic graphs including) and linked lists no problem. There is no any obvious reason why large structures should have problems (apart from that object reference uses some memory).

OTHER TIPS

Instead of returning a value, you could return an Callable which just needs to be executed. This can then be chained (theoretically infinitely)

You can have a LocationEvaluation for example which could return a SpecificLocationEvaluator which in turns returns one of StepEvaluation or ChoiceEvaluator or somesuch. All of these would implement the Callable interface.

Depending on how you do it, you could have strict type checking so that a LocationEvaluation always returns a SpecificLocationEvaluator or it can generic and then you can chain any of then in any order.

Once you build the structure, out, you would essentially have a tree which would be traversed to solve it.

I don't understand the problem adequately to be able to provide more concrete implementation details - and apologies if I misunderstood some of the branching (i.e. the names of the classes / steps above)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top