Home > Software engineering >  How do I set a level based on the required XP using an int and an xp table array
How do I set a level based on the required XP using an int and an xp table array

Time:09-19

Say I have an array with the require xp for each level so for instance

0 == Level 1
83 == Level 2
174 == Level 3
276 == Level 4
388 == Level 5
512 == Level 6
650 == Level 7
801 == Level 8
969 == Level 9
...

What's the most efficient way to increment an int to the corresponding level?

So I'd have an int like this

ìnt currentLevel = 1;

and then then say I added 25 xp, that would still put me at level 1, but then I added 800 xp which would set me at 825xp which then would put me at level 8

This was my solution but it keeps running at 98 and won't stop since it never levels up to 99. Not sure how to fix it.

internal class Program
{
    private const int XPGain = 500;
    static int currentLevel = 1;
    static int currentXP = 0;

    static void Main(string[] args)
    {
        string[] levels = File.ReadAllLines("XPTable.txt");

        while (currentLevel < 99)
        {
            currentXP  = XPGain;
            for (int i = currentLevel; i < levels.Length; i  )
            {
                if (currentXP >= Convert.ToInt32(levels[i]))
                {
                    //Set appropriate level
                    for (int l = 1; l < levels.Length   1; l  )
                    {
                        if (currentXP <= Convert.ToInt32(levels[l - 1]))
                        {
                            currentLevel = l - 1;
                            Console.WriteLine("Level up!");
                            break;
                        }
                    }
                    break;
                }
            }
            Console.WriteLine($"[Action] - Current Level: {currentLevel}. Added XP: {XPGain} - Current XP: {currentXP}");
        }
    }
}

Here's the XP table I'm using. https://www.klgrth.io/paste/g79ht

CodePudding user response:

There are a few improvements you can make

internal class Program
{
    private const int XPGain = 500;
    static int currentLevel = 1;
    static int currentXP = 0;

    static void Main(string[] args)
    {
        while (currentLevel < 99)
        {
            int oldLevel = currentLevel;
            currentXP  = XPGain;
            // 1 dont need to increment this one at a time, just set it when you know the value
            currentLevel = CalculateLevel(currentXP);
            for (int i = oldLevel; i < currentLevel; i  )
            {
                Console.WriteLine("Level up!");
            }
            Console.WriteLine($"[Action] - Current Level: {currentLevel}. Added XP: {XPGain} - Current XP: {currentXP}");
        }
    }

    static int CalculateLevel(int currentXP)
    {
        string[] levels = File.ReadAllLines("D:\\Workspace\\FormTest\\ConsoleApp1\\XPTable.txt");

        // 2 just go through each line and figure out the level.
        for (int i = 1; i < levels.Length; i  ) // level 1 to 98
        {
            // maxXP for level 1 is on line 2, level 2 is on line 3 and so on...
            int maxXPForLevel = int.Parse(levels[i]);
            if (currentXP < maxXPForLevel)
                return i;
        }
        return levels.Length; // 3. im greater than the highest values
    }
}
  1. You can programmatically get currentLevel from currentXP you dont need to use this accumulator logic and instead compute this on demand.

  2. You can use a single for loop to figure out level ups and new level gains.

  3. Your solution doesnt get to lvl 99 because in your code you need to check between the lower bound and the upper bound for that level, and level 99 doesn't have an upper bound. I think you tweaked the loop indexes to get it to work until level 98 but you need to somewhere write the behavior at lvl 99.

  • Related