Mike James Johnson
Mike James Johnson

Reputation: 734

Alternative to 1000s of if-then-else statements

I am programming a Text based RPG game for Android in order to familiarize myself with Java and XML. I want to display a string array of choices across a ListView of buttons, and depending on which button the user presses, I want to update the activity accordingly with a new set of choices.

For example, lets say the game starts off with the following choices:

{option1, option2, option3, option4}

If the user clicks on option2 i want to display:

{2-option1, 2-option2, 2-option3, 2-option4}

or lets say the user clicked on 4 instead of 2. Then:

{4-option1, 4-option2, 4-option3, 4-option4}

So, i am trying to determine a speed efficient / memory efficient way of determining the next String[] to display.

Originally i was thinking about constructing a GIANT if-then-else tree (or switch statement - doesn't matter), where there would be an if statement for each possible option. For example:

//choice is the user selected option

if(choice==option1){
   return {"1-option1", "1-option2", "1-option3"}
}else if(choice== 1-option1 ){
   return {"1-1-option1","1-1-option2","1-1-option3"}

And the List would go on and on until every possible choice returns a String[] that corresponds to the next group of options that are to be displayed.

Then, i started thinking maybe it would be cleaner and easier to do this with objects:

   MyObject newObject = MyObject(choice)
   newObject.getOptions()

where choice is a constructor parameter that initializes a String[] to the proper next set of options; and getOptions() returns this String[]

However, this is going to be a lot of objects, and i am not sure it will simplify things over the if/switch statement method.

So, any ideas of an efficient way to handle something like this?

Upvotes: 1

Views: 1027

Answers (3)

Nyamiou The Galeanthrope
Nyamiou The Galeanthrope

Reputation: 1214

You are going to need a singleton object like OptionProvider with methods like :

 - List<Option> getFirstOptions() 
 - List<Option> getNextOptions(Option option)

This object can hold the information as to which options come after which option in a Map like Map<Option,List<Option>>. You can fill the Map manually or it could be filled with data from an XML file or a database.

Then you can have an utility class like DisplayOptionUtils with a method like :

 - String getOptionPrompt(List<Option> chosenOptions, List<Option> nextAvailableOptions)

That will return the way you want your options to appear to the user (it can for example use a StringBuilder to create what you need).

Then in your main class you can have something like that :

List<Option> chosenOptions = new ArrayList<>();
List<Option> nextOptions = OptionProvider.getInstance().getFirstOptions();
while(!nextOptions.isEmpty()) {
  System.out.println(DisplayOptionUtils.getOptionPrompt(chosenOptions, nextOptions);
  Option selectedOption = Option.getByValue(getUserInput());
  chosenOptions.add(selectedOption);
  nextOptions = OptionProvider.getInstance().getNextOptions(selectedOption);
}

This design is not perfect (I come up with it in 10 minutes) but hopefully it will give you in idea on how to achieve what you want.

Upvotes: 1

Durandal
Durandal

Reputation: 20069

Transform the concept into a more general one: There is always a current state, and there may be a set of options for that state. Choices can then be understood simply as triggering state transitions.

So you could simply model that in XML as:

<state name="Main Menu">
   <option name="Buy" newState="Shop"/>
</state>
<state name="Shop>
   <option name="Exit" newState="Main Menu"/>
</state>

State and Option can be easily modeled as classes. There is no need for complicated code, just put all existing states into a Map. For changing state, just ask the map for the state that belong to that state name and set that state as the current state...

Upvotes: 0

Nick
Nick

Reputation: 3591

Use a Map (probably a HashMap) internal to your object to associate "option2" with {"2-option1", "2-option2", "2-option3", "2-option4"}.

Data-structure choice is very important in programming.

Upvotes: 5

Related Questions