Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How can I optimize my code: multiple boolean checks  (Read 4035 times)
0 Members and 1 Guest are viewing this topic.
Offline Norakomi
« Posted 2014-10-24 14:57:29 »

I've got a level select screen where the player can choose between 1 of the 7 levels of my game.
If he finishes a level a boolean (LEVELxFINISHED = true;) gets set and a check mark is drawn over the level in the level select screen and the player is only able to choose from the remaining levels.

If he finishes ALL level, for now I want all boolean for if the leve is finished to be reset.

I'm doing this with this code.

1  
2  
3  
4  
if (LEVEL1FINISHED && LEVEL2FINISHED &&
            LEVEL3FINISHED && LEVEL4FINISHED &&
            LEVEL5FINISHED && LEVEL6FINISHED &&
            LEVEL7FINISHED ) {resetLevels();}


I was wondering is there is a way to automate/optimize this code.
It works fine, but imagine i would have had like 40 levels....
Would be nice to know how to do this check with some kind of for loop.


Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #1 - Posted 2014-10-24 14:58:54 »

Keep a counter?

1  
2  
3  
if (levelsFinished == 7) {
    reset();
}
Offline Norakomi
« Reply #2 - Posted 2014-10-24 15:03:24 »

O yes, forgot about that, that's a good way for sure.

But then I get to my resetLevels method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
   public void resetLevels() {
      LEVEL1FINISHED = false;
      LEVEL2FINISHED = false;
      LEVEL3FINISHED = false;
      LEVEL4FINISHED = false;
      LEVEL5FINISHED = false;
      LEVEL6FINISHED = false;
      LEVEL7FINISHED = false;
   }


And I'm still curious if it is possible to do this more efficiƫnt for say if it where 40 levels.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #3 - Posted 2014-10-24 15:04:52 »

1  
levelsFinished = 0;


Tip: If you ever have lots of bits of code that do more or less the same thing, that's not good.
Offline Norakomi
« Reply #4 - Posted 2014-10-24 15:13:54 »

Well.... levelsFinished =0; wouldnt suffice i think

Let me explain.

So i have a menuselect Screen where I draw all 7 levels as a pictogram on screen and the player can select one.
Once a level is finished the screen switches back to the menuSelect screen and depending on which LEVELFINISHED boolean is set to true, it draws a checkmark over the level Pictogram, so the player:
1. sees which level(s) he has finished
2. can't select the level anymore.

The levels don't need to be selected in chronological order.
So if the player wants to play lvl3 first and then 1 and after 6, etc. He/She can.

You say that
Quote
If you ever have lots of bits of code that do more or less the same thing, that's not good.

which sound very plausible, but in this case i don't see what else i can do to set a bit to true or false in order to check for checkmark-draw and whether or not the level can be played

Offline StrideColossus
« Reply #5 - Posted 2014-10-24 15:15:13 »

Maybe create a separate object to track the number of completed levels, something like the following?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public class LevelTracker {
    private final boolean[] completed = new boolean[ NUMBER_OF_LEVELS ];

    public void completeLevel( int level ) {
        completed[ level ] = true;
    }

    public boolean isComplete( int level ) {
        return completed[ level ];
    }

    public int countCompletedLevels() {
        int count = 0;
        for( boolean b : completed ) {
            if( b ) ++count;
        }
        return count;
    }

    public void reset() {
        Arrays.fill( completed, false );
    }
}


No need for separate constants for each level.

Offline Norakomi
« Reply #6 - Posted 2014-10-24 15:16:12 »

Interesting!

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #7 - Posted 2014-10-24 15:20:25 »

That would work. I wondered if levels could be played non-sequentially.
Offline Norakomi
« Reply #8 - Posted 2014-10-24 15:31:23 »

Yip, thought from a standpoint of trying to optimize the time a player stays interested in your game, would be to give them more options in which level they want to play in stead of starting with the same levels over and over.

Offline actual

JGO Coder


Medals: 25



« Reply #9 - Posted 2014-10-24 19:31:48 »

If you had more statuses than completed y/n, you could use an enum.
1  
2  
3  
public enum LevelState {
   NOT_TRIED, ATTEMPTED, COMPLETED, COMPLETED_PERFECT_SCORE;
}


Then you could use an array of these enums to capture more nuanced state.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 285
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #10 - Posted 2014-10-25 21:18:52 »

You could also just have an array of booleans.

boolean[] levelsFinished = new boolean[7];

And then to check if all levels are completed, just do something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
boolean allFinished = true;
for(boolean b : levelsFinished){
   if(!b){
      allFinished = false;
      break; //no need to check the rest
   }
}
if(allFinished){
   resetLevels();
}


Whoops, I just realized this is what StrideColossus is saying. But the point is that whenever you find yourself using a bunch of variables that mean almost the same thing, it's time for an array or a new class.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline Catharsis

JGO Ninja


Medals: 75
Projects: 1
Exp: 21 years


TyphonRT rocks!


« Reply #11 - Posted 2014-10-26 23:27:26 »

I recommend using an enum for the levels (lets make that Levels) and EnumSet for the tracking.

EnumSet<Levels> completedLevels = EnumSet.noneOf(Levels.class);

Just like all the other collections there are many familiar operations:

completedLevels.size() gives the count.

completedLevels.add(Levels.LEVEL_1);

You can get an iterator for enums in the set.

You can get a complement set via:

EnumSet<Levels> unfinishedLevels = EnumSet.complementOf(completedLevels);

And lots more without having to write any custom tracking code like other solutions in this thread.

You can even create a EnumSet for special conditions and easily check against completedLevels:

EnumSet<Levels> mediumDifficultyLevels = EnumSet.range(Levels.LEVEL_10, Levels.LEVEL_20);

if (completedLevels.containsAll(mediumDifficultlyLevels))
{
// etc etc etc.
}

Check out the TyphonRT Video Suite:
http://www.typhonvideo.com/

Founder & Principal Architect; TyphonRT, Inc.
http://www.typhonrt.org/
http://www.egrsoftware.com/
https://plus.google.com/u/0/+MichaelLeahy/
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1572 views)
2018-06-10 19:43:48

EgonOlsen (1632 views)
2018-06-10 19:43:44

EgonOlsen (1143 views)
2018-06-10 19:43:20

DesertCoockie (1569 views)
2018-05-13 18:23:11

nelsongames (1173 views)
2018-04-24 18:15:36

nelsongames (1638 views)
2018-04-24 18:14:32

ivj94 (2395 views)
2018-03-24 14:47:39

ivj94 (1605 views)
2018-03-24 14:46:31

ivj94 (2691 views)
2018-03-24 14:43:53

Solater (882 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!