I have an algorithm question related to XP levels in games:
I have a table that lists level numbers, and number of XPs in that level:
GameLevel ExperiencePointsInLevel
1 30
2 70
3 160
4 250
For each player, I only maintain their total XP, not any level info.
Given a player’s total XP, how do I determine the following via an algorithm (I’m using C# & LINQ)?
- Current level of person
- Number points the person has in this level
For example, if a player has 110 XPs, the answer would be:
- Level 3
- 10 XPs; (110-30-70 = 10)
Any ideas?
Cheers,
Dean
If experience points in level is changed to represent the total XP a player needs to reach that level.
A more functional approach with LINQ would be something like the following:
I think this simplifies the solution and avoids having to generate arrays for a look up.
Edit: fixed to check less than or equal to level.
To find out how many xp the player has to get to the next level you can use a similiar LINQ query.