# What is the best (or a good) approach to "code" difficult level

I've to decide how to code enemy spawn: quantity and time. What's a good approach to do that ?

Maybe a class like this ?

class Level {
int   levelNr ;
int   soldierQty; // number of enemy soldier
int   aircraftQty; // number of aircraft
float maxTimeToComplete;
...
...
}


Then, in main code

   Level[100] levels;

levels[0].levelNr = 1;
levels[0].soldierQty = 5;
levels[0].aircraftQty = 3;
levels[0].maxTimeToComplete = 10; // 10 seconds
levels[1].levelNr = 2;
etc etc etc


Is it a good approach ? Is there a different way I haven't thought ?

Thanks

• Test it out / playtest and find what feels like a challenging but not frustrating level.. toy with the numbers yourself, there is no right answer Mar 8 '17 at 19:44
• My question is not about "how to set difficult", but how to set levels .. hard coding isn't a good approach maybe... Mar 8 '17 at 20:38
• Well it's wording doesn't suggest that right now Mar 9 '17 at 0:20

There isn't a perfect answer here. But one thing to note is that it isn't great to put 'magic numbers' in the code.

This is also referred to as hard coding. It would be better to get the values for the number of enemies etc from a data file by deserialising it. That way you don't need to change the code each time you need to make a change, you can just update the data file.

This makes your code more maintainable and more easily extended.

You might want to look if it is possible to calculate the numbers for each level at runtime. That's likely far less typing, less prone to errors and has the advantage that you can have infinitely many levels.

I would then have a constructor for Level which just takes the level number and calculates all the variables from it. Something like this:

class Level {
int   levelNr ;
int   soldierQty; // number of enemy soldier
int   aircraftQty; // number of aircraft
float maxTimeToComplete;

public Level (int number) {
this.levelNr = number;
this.soldierQty = number * 2 + 3;
this.aircraftQty = number / 2 - 5;
this.maxTimeToComplete = 600.0f / (number + 9);
}
}