Leveling Game Program Help!

I’m making my own little program with a game that lets players level up and allow them to go to the next level. The programming language I’m using is Java and I know the basics. The problem is, my game has way to many if statements. What’s worse, every if statement is inside the previous if statement.
The game I’m creating has a similar concept to this game: http://www.dojo.com/game/the-enchanted-cave-2

If every if statement is inside the previous one, which I’m taking to mean that you have a whole bunch of nested if statements like this:

if(){
    if(){
         ...
    }
}

Then it sounds like there’s a pattern going on and you can maybe organize it in a loop? Something like

bool My100Bools[100];
My100Bools[0]=something;
for(int i=1;i<100;++i){
    My100Bools[i]=somethingElse && My100Bools[i-1];
}

Hard to say more from what you told us

As @Agade Agade said, your information is too vague to give you specific help, but you may want to look into the Strategy design pattern.

Rather than

if (level == 1) {
    doLevel1Action();
} else if (level == 2) {
    doLevel2Action();
} else if (level == 3) {
    doLevel3Action();
}

Do something like this:

List<LevelActionStrategy> levelActions = Arrays.asList(
    new Level1ActionStrategy(), new Level2ActionStrategy(), new Level3ActionStrategy());

...

levelActions.get(level-1).doAction();

This has the advantage of localizing all of your level-specific (or otherwise situationally-specific) code into self-contained units.

  • danBhentschel

basically it’s like this:
if (left) {
if (right) {
if (up) {
action
if (right) {

}
}
}
}

There’re hundreds of routes the player can take. But instead of having an if statement for each, I was hoping for something that’ll replace the hundreds of if statements.

Are you saying you’re coding the map into your logic? You really need to put your game map into some kind of data file that is parsed by your program. Read in the file, and perform actions based on the map data and the player’s current location / status.

  • danBhentschel

I was actually hoping to have a text-based game for now so it’s “playable”. Players will enter “up”, “left”, etc to play. After that, I’ll try to create pictures and stuff for it.