How to simplify this code or a better design?

Posted by Tattat on Stack Overflow See other posts from Stack Overflow or by Tattat
Published on 2010-05-22T11:49:39Z Indexed on 2010/05/22 12:00 UTC
Read the original article Hit count: 273

I am developing a game, the game have different mode. Easy, Normal, and Difficult. So, I'm thinking about how to store the game mode. My first idea is using number to represent the difficulty.

Easy = 0 Normal = 1 Difficult = 2

So, my code will have something like this:

switch(gameMode){

case 0:
//easy
break;

case 1:
//normal
break;

case 3:
//difficult
break;

}

But I think it have some problems, if I add a new mode, for example, "Extreme", I need to add case 4... ... it seems not a gd design.

So, I am thinking making a gameMode object, and different gameMode is sub class of the super class gameMode. The gameMode object is something like this:

    class GameMode{
    int maxEnemyNumber;
    int maxWeaponNumber;
      public static GameMode init(){
         GameMode gm = GameMode();
         gm.maxEnemyNumber = 0;
         gm.maxWeaponNumber = 0;
         return gm;
      }    

    }

class EasyMode extends GameMode{

      public static GameMode init(){
         GameMode gm = super.init();
         gm.maxEnemyNumber = 10;
         gm.maxWeaponNumber = 100;
         return gm;
      }    
}


class NormalMode extends GameMode{

      public static GameMode init(){
         GameMode gm = super.init();
         gm.maxEnemyNumber = 20;
         gm.maxWeaponNumber = 80;
         return gm;
      }    
}

But I think it seems too "bulky" to create an object to store gameMode, my "gameMode" only store different variables for game settings.... Is that any simple way to store data only instead of making an Object? thz u.

© Stack Overflow or respective owner

Related posts about optimization

Related posts about programming-languages