Skip to main content
added 104 characters in body
Source Link

I am not fond of the hardcoding since that will make it difficult to change your score thresholds later.This rules out Enum's as well. I liked Janos's second answer best, but that means storing a copy of the thresholds in a hash. In addition it will continue to loop through every threshold after the max difficulty is reached just to do nothing.

The following uses one list, only checks for the next threshold not reached (no loop)handles level jumping, and once max difficulty is reached it short circuits as quickly as possible.

Initial variable declarations are only to illustrate type/expectation. Score, Difficulty, and the threshold list should be wrapped up nicely somewhere else in a static Singleton managing game state. And the thresholds need to come out of a configuration store somewhere so you don't need to recompile your game to change your mind later.

The key to this approach is that the difficulty is the index into the threshold list to determine the next increase. It also uses a while loop to handle jumping multiple levels. I did assume that you wouldn't be going backwards like the other solutions. Once difficulty level is out of bounds you know you have nothing left to check and are at max level.

int currentScore = 0; int currentDifficulty = 0; List<int> difficultyThresholds = new List<int>() { 100, 500, 1500 }; void DifficultySlider() { if (currentDifficulty >= difficultyThresholds.Count) { return; }   while (currentScore >= difficultyThresholds[currentDifficulty]) { currentDifficulty += 1; if (currentDifficulty >= difficultyThresholds.Count) { return; } } } 

I am not fond of the hardcoding since that will make it difficult to change your score thresholds later.This rules out Enum's as well. I liked Janos's second answer best, but that means storing a copy of the thresholds in a hash. In addition it will continue to loop through every threshold after the max difficulty is reached just to do nothing.

The following uses one list, only checks for the next threshold not reached (no loop), and once max difficulty is reached it short circuits as quickly as possible.

Initial variable declarations are only to illustrate type/expectation. Score, Difficulty, and the threshold list should be wrapped up nicely somewhere else in a static Singleton managing game state. And the thresholds need to come out of a configuration store somewhere so you don't need to recompile your game to change your mind later.

The key to this approach is that the difficulty is the index into the threshold list to determine the next increase. It also uses a while loop to handle jumping multiple levels. I did assume that you wouldn't be going backwards like the other solutions. Once difficulty level is out of bounds you know you have nothing left to check and are at max level.

int currentScore = 0; int currentDifficulty = 0; List<int> difficultyThresholds = new List<int>() { 100, 500, 1500 }; void DifficultySlider() { if (currentDifficulty >= difficultyThresholds.Count) { return; }   while (currentScore >= difficultyThresholds[currentDifficulty]) { currentDifficulty += 1; } } 

I am not fond of the hardcoding since that will make it difficult to change your score thresholds later.This rules out Enum's as well. I liked Janos's second answer best, but that means storing a copy of the thresholds in a hash. In addition it will continue to loop through every threshold after the max difficulty is reached just to do nothing.

The following uses one list, handles level jumping, and once max difficulty is reached it short circuits as quickly as possible.

Initial variable declarations are only to illustrate type/expectation. Score, Difficulty, and the threshold list should be wrapped up nicely somewhere else in a static Singleton managing game state. And the thresholds need to come out of a configuration store somewhere so you don't need to recompile your game to change your mind later.

The key to this approach is that the difficulty is the index into the threshold list to determine the next increase. I did assume that you wouldn't be going backwards like the other solutions. Once difficulty level is out of bounds you know you have nothing left to check and are at max level.

int currentScore = 0; int currentDifficulty = 0; List<int> difficultyThresholds = new List<int>() { 100, 500, 1500 }; void DifficultySlider() { if (currentDifficulty >= difficultyThresholds.Count) { return; } while (currentScore >= difficultyThresholds[currentDifficulty]) { currentDifficulty += 1; if (currentDifficulty >= difficultyThresholds.Count) { return; } } } 
added 5 characters in body
Source Link

I am not fond of the hardcoding since that will make it difficult to change your score thresholds later.This rules out Enum's as well. I liked Janos's second answer best, but that means storing a copy of the thresholds in a hash. In addition it will continue to loop through every threshold after the max difficulty is reached just to do nothing.

The following uses one list, only checks for the next threshold not reached (no loop), and once max difficulty is reached it short circuits as quickly as possible.

Initial variable declarations are only to illustrate type/expectation. Score, Difficulty, and the threshold list should be wrapped up nicely somewhere else in a static Singleton managing game state. And the thresholds need to come out of a configuration store somewhere so you don't need to recompile your game to change your mind later.

The key to this approach is that the difficulty is the index into the threshold list to determine the next increase. It also uses a while loop to handle jumping multiple levels. I did assume that you wouldn't be going backwards like the other solutions. Once difficulty level is out of bounds you know you have nothing left to check and are at max level.

int currentScore = 0; int currentDifficulty = 0; List<int> difficultyThresholds = new List<int>() { 100, 500, 1500 }; void DifficultySlider() { if (currentDifficulty >= difficultyThresholds.Count) { return; } ifwhile (currentScore >= difficultyThresholds[currentLevel]difficultyThresholds[currentDifficulty]) { currentDifficulty += 1; } } 

I am not fond of the hardcoding since that will make it difficult to change your score thresholds later.This rules out Enum's as well. I liked Janos's second answer best, but that means storing a copy of the thresholds in a hash. In addition it will continue to loop through every threshold after the max difficulty is reached just to do nothing.

The following uses one list, only checks for the next threshold not reached (no loop), and once max difficulty is reached it short circuits as quickly as possible.

Initial variable declarations are only to illustrate type/expectation. Score, Difficulty, and the threshold list should be wrapped up nicely somewhere else in a static Singleton managing game state. And the thresholds need to come out of a configuration store somewhere so you don't need to recompile your game to change your mind later.

The key to this approach is that the difficulty is the index into the threshold list to determine the next increase. Once difficulty level is out of bounds you know you have nothing left to check and are at max level.

int currentScore = 0; int currentDifficulty = 0; List<int> difficultyThresholds = new List<int>() { 100, 500, 1500 }; void DifficultySlider() { if (currentDifficulty >= difficultyThresholds.Count) { return; } if (currentScore >= difficultyThresholds[currentLevel]) { currentDifficulty += 1; } } 

I am not fond of the hardcoding since that will make it difficult to change your score thresholds later.This rules out Enum's as well. I liked Janos's second answer best, but that means storing a copy of the thresholds in a hash. In addition it will continue to loop through every threshold after the max difficulty is reached just to do nothing.

The following uses one list, only checks for the next threshold not reached (no loop), and once max difficulty is reached it short circuits as quickly as possible.

Initial variable declarations are only to illustrate type/expectation. Score, Difficulty, and the threshold list should be wrapped up nicely somewhere else in a static Singleton managing game state. And the thresholds need to come out of a configuration store somewhere so you don't need to recompile your game to change your mind later.

The key to this approach is that the difficulty is the index into the threshold list to determine the next increase. It also uses a while loop to handle jumping multiple levels. I did assume that you wouldn't be going backwards like the other solutions. Once difficulty level is out of bounds you know you have nothing left to check and are at max level.

int currentScore = 0; int currentDifficulty = 0; List<int> difficultyThresholds = new List<int>() { 100, 500, 1500 }; void DifficultySlider() { if (currentDifficulty >= difficultyThresholds.Count) { return; } while (currentScore >= difficultyThresholds[currentDifficulty]) { currentDifficulty += 1; } } 
Source Link

I am not fond of the hardcoding since that will make it difficult to change your score thresholds later.This rules out Enum's as well. I liked Janos's second answer best, but that means storing a copy of the thresholds in a hash. In addition it will continue to loop through every threshold after the max difficulty is reached just to do nothing.

The following uses one list, only checks for the next threshold not reached (no loop), and once max difficulty is reached it short circuits as quickly as possible.

Initial variable declarations are only to illustrate type/expectation. Score, Difficulty, and the threshold list should be wrapped up nicely somewhere else in a static Singleton managing game state. And the thresholds need to come out of a configuration store somewhere so you don't need to recompile your game to change your mind later.

The key to this approach is that the difficulty is the index into the threshold list to determine the next increase. Once difficulty level is out of bounds you know you have nothing left to check and are at max level.

int currentScore = 0; int currentDifficulty = 0; List<int> difficultyThresholds = new List<int>() { 100, 500, 1500 }; void DifficultySlider() { if (currentDifficulty >= difficultyThresholds.Count) { return; } if (currentScore >= difficultyThresholds[currentLevel]) { currentDifficulty += 1; } }