patterncsharpModerate
Increase difficulty based on score
Viewed 0 times
scoreincreasebaseddifficulty
Problem
What I'm trying to do is very straightforward and easy, but I am wondering whether there is a more elegant solution.
Basically I have a game and I want the difficulty to increase the moment the score gets between 100 - 500, then increase once again when the score is between 500 - 1500, then again when the score is between 1500 - 3000. In total, the difficulty should increase 3 times.
I need to check:
Here's what I've got.
LE: I should have mentioned that the method above is called multiple times per frame.
Basically I have a game and I want the difficulty to increase the moment the score gets between 100 - 500, then increase once again when the score is between 500 - 1500, then again when the score is between 1500 - 3000. In total, the difficulty should increase 3 times.
I need to check:
- Whether I am in one of those ranges.
- Whether I already increased the score when I got in that range.
Here's what I've got.
bool Increased100500 = false;
bool Increased5001500 = false;
bool Increased15003000 = false;
void IncreaseScore()
{
if (Score >= 100 && Score = 500 && Score = 1500 && !Increased15003000)
{
Increase();
Increased15003000 = true;
}
}LE: I should have mentioned that the method above is called multiple times per frame.
Solution
Be careful with your inequalities. You've listed 500 twice: once as
I think that you would be better off with one state variable representing the current difficulty level. The code would also be simpler if you determine
If the score can never decrease in this game, then you can simplify it further:
if (Score = 500). That's confusing, as the first condition takes precedence.I think that you would be better off with one state variable representing the current difficulty level. The code would also be simpler if you determine
NewDifficulty using a level-triggered rather than edge-triggered mechanism.int Difficulty;
void IncreaseScore()
{
int NewDifficulty = (Score < 100) ? 0
: (Score <= 500) ? 1
: (Score <= 1500) ? 2
: 3;
// One-way ratchet: difficulty can only increase, even if the
// score subsequently drops.
if (NewDifficulty < Difficulty)
{
Difficulty = NewDifficulty;
}
}If the score can never decrease in this game, then you can simplify it further:
void IncreaseScore()
{
Difficulty = (Score < 100) ? 0
: (Score <= 500) ? 1
: (Score <= 1500) ? 2
: 3;
}Code Snippets
int Difficulty;
void IncreaseScore()
{
int NewDifficulty = (Score < 100) ? 0
: (Score <= 500) ? 1
: (Score <= 1500) ? 2
: 3;
// One-way ratchet: difficulty can only increase, even if the
// score subsequently drops.
if (NewDifficulty < Difficulty)
{
Difficulty = NewDifficulty;
}
}void IncreaseScore()
{
Difficulty = (Score < 100) ? 0
: (Score <= 500) ? 1
: (Score <= 1500) ? 2
: 3;
}Context
StackExchange Code Review Q#61889, answer score: 19
Revisions (0)
No revisions yet.