A simplified / more efficient method of a nested if ... else thread?

I am currently working on an emulation server for a flash client game that has a "pet system", and I was wondering if there is an easier way to check the level of these pets.

Current Code:

public int Level { get { if (Expirience > 100) // Level 2 { if (Expirience > 200) // Level 3 { if (Expirience > 400) // Level 4 - Unsure of Goal { if (Expirience > 600) // Level 5 - Unsure of Goal { if (Expirience > 1000) // Level 6 { if (Expirience > 1300) // Level 7 { if (Expirience > 1800) // Level 8 { if (Expirience > 2400) // Level 9 { if (Expirience > 3200) // Level 10 { if (Expirience > 4300) // Level 11 { if (Expirience > 7200) // Level 12 - Unsure of Goal { if (Expirience > 8500) // Level 13 - Unsure of Goal { if (Expirience > 10100) // Level 14 { if (Expirience > 13300) // Level 15 { if (Expirience > 17500) // Level 16 { if (Expirience > 23000) // Level 17 { return 17; // Bored } return 16; } return 15; } return 14; } return 13; } return 12; } return 11; } return 10; } return 9; } return 8; } return 7; } return 6; } return 5; } return 4; } return 3; } return 2; } return 1; } } 

Yes, I know that I have experience with an error, I made a mistake in the previous function and did not manage to update everything.

+6
c #
source share
7 answers
 int[] levelCutoffs = new int[] {0, 100, 200, 400, 600 /*...*/}; for (int level = 0; level < levelCuttoffs.size; ++level) { if (Experience < levelCuttofs[level]) return level; } return levelCuttoffs.size; 

Edit: Modified to use the Bradley Mountford offer.

+13
source share

Use a SortedList<int, int> and SortedList<int, int> over it until you find a value that is greater than the value you are looking for. You can do this using a simple iteration, as in the answer you already accepted. Or it can be done elegantly using LINQ (at a low execution cost):

 SortedList<int, int> levels = new SortedList<int, int> { {0, 1}, {100, 2}, {200, 3}, {400, 4}, {600, 5}, }; public int Experience; public int Level { get { return levels.Last(kvp => Experience >= kvp.Key).Value; } } 

Note that maintaining the β€œlevel” is not really strictly necessary, since you can get it from the index of an item in a list. It may be beneficial to use a simple List<int> , which is sorted instead, to prevent errors when you accidentally miss a level, as in a decision you have already made.

If you need better performance, you can use List.BinarySearch , but I think that the additional complexity is not worth it if you do not have a profile profile and that this is a bottleneck.

 List<int> levels = new List<int> { 0, 100, 200, 400, 600 /* etc... */ }; int index = levels.BinarySearch(Experience); int level; if (index < 0) { level = ~index; } else { level = index + 1; } return level; 
+22
source share
Sentence

@Mark is reasonable. You can also cancel the experience rating order to unlock ifs:

 if (Expirience > 23000) return 17; if (Expirience > 17500) return 16; //... and so on. 

But I would probably just use a regular C # array and the BinarySearch method, which can return the index of the corresponding element or 2 addition to the smallest element that is larger than the value you were looking for:

 int[] levelThresholds = new[] { 100, 200, 400, 600, 1000, ..., 23000 }; int experience = 11403; int index = Array.BinarySearch( levelThresholds, experience ); // returns either the index, or the 2 complement of the // first index greater than the value being sought int level = index < 0 ? ~index : index+1; 
+4
source share

What about a simple formula based on a logarithmic function?

Something like

 return Math.Floor(LinearScale * Math.Log(Expirience, LogBase)); 
+3
source share

You go from the most inclusive to the most exclusive. If you go the other way, you do not need all nesting.

 if (Expirience > 23000) // Level 17 { return 17; // Bored } else if (Expirience > 17500) // Level 16 { return 16; } else if (Expirience > 13300) // Level 15 { return 15; } ... 
+2
source share

I would ask Mark Bayer to answer one more step. Since this is a bit confusing (I would forget which one is there). Make a sorted list instead

 SortedList<UserLevel> 

Thus, you can define much more than just the required number of experience points for each level. you can also assign a name, that is, "Super Uber Elite" and, possibly, even an individual greeting message at each level.

+2
source share

If the experiment algorithm can be reduced to a function, it should use functional calculation, i.e.

 return (Expirience/200); // if each level was 200 xp etc 

However, your nested ones, if the above does not seem to apply to any function curve, is there? Operator:

 return (Expirience > 23000) ? 17 : (Expirience > 17500) ? 16 : (Expirience > 13300) ? 15 : .. etc .. (Expirience > 100) ? 2 : 1; 
+2
source share

All Articles