简体   繁体   中英

Level Up & XP - making the code more efficient

I am in the process of making a Level UP & XP feature for an Android Studio project.

Context:

Created a Database with a Total XP column (updated upon app opening)

Level column gets updated upon app opening

What I need to do:

getTotalXP from database and check if it is meets the criteria to level up. Update the database with the level based on the criteria.

Example:

Level 0: XP < 100

Level 1: 100 <= XP < 199

Level 2: 200 <= XP < 299

Etc..

You get the idea. Fortunately, the amount of XP needed does not variably change as you go up the levels. However this would be an added bonus feature if someone could explain how I would go about this.

I have initially hardcoded my method so that I get the general idea of what it is I want. I am new to programming and understand that my logic is flawed in the below:

 public int updateLocalLevel() {

    int currentLevel = Integer.parseInt(returnDBLevel());
    int currentTotalXP = Integer.parseInt(returnDBTotalXP());

    if(currentTotalXP < 100) {
        //still level 0
        //do nothing
    }
    else if(currentTotalXP >= 100 & currentTotalXP < 199) {
        //level up to level 1
        //get db level
        //add 1 to level
        currentLevel++;            
    }
    else if(currentTotalXP >= 200 & currentTotalXP < 299) {
        //level up to level 2
        //get db level
        //add 1 to level
        currentLevel++;
        //update local level            
    }
    else if(currentTotalXP >= 300 & currentTotalXP < 399) {
        //level up to level 3
        //get db level
        //add 1 to level
        currentLevel++;
        //update local level            
    }
    else if(currentTotalXP >= 400 & currentTotalXP < 499) {
        //level up to level 4
        //get db level
        //add 1 to level
        currentLevel++;
        //update local level            
    }
    else if(currentTotalXP >= 500 & currentTotalXP < 599) {
        //level up to level 5
        //get db level
        //add 1 to level
        currentLevel++;
        //update local level
    }
    else{
        android.util.Log.d(TAG, "ReachedMaxLevel: " + returnDBLevel());

    }
    return currentLevel;
}

I aim to use two methods: updateLocalLevel() and updateDBLevel() The method above updates the level locally before passed through to the updateDBLevel() method {not created as of yet}.

The problem is that there are too many if and else if statements. I want the levels to be unlimited and therefore I need to change this method completely. I am unsure of how to do this. Many Thanks.

Seeing your code and the cases you are exposing, I guess you can make your code more efficient with 2 conditions:

If your current xp is lesser 100 or higher 599 you don't increment the variable

Or if your current xp end with X99 you can increment the variable like this:

public static int updateLocalLevel(String cLevel, String cXp) {

    int currentLevel = Integer.parseInt(cLevel);
    int currentTotalXP = Integer.parseInt(cXp);

    if(currentTotalXP < 100 || currentTotalXP > 599) {
        System.out.println("Level less than 100 or max level reached");
    }
    else if(String.valueOf(currentTotalXP).endsWith("99")) {
        currentLevel++;
        System.out.println("Level ends with x99 -> Level should be incremented ");
    }

    return currentLevel;
}

In the case you want add more levels, you only will need to change the high value 599 in the first if.

If you want to make every level take the same amount of XP then I recommend just dividing the XP by the XP requirement. If you have the exp as int then it will always round down.

public int CalculateLevel(int xp){
  int xpPerLevel = 100;
  return xp/xpPerLevel;
}

If you want different EX requirement per level try

public int CalculateLevel(int xp){
  int[] xpPerLevel = {100,200,500,1000,2000,5000};
  int level = 0;
  for(int i: xpPerLevel ){
    if( xp / i > 0){
      level++;
    }
    else break;
  }
  return level;
}

EDIT:

This would probably be better:

public int CalculateLevel(int xp){
    int[] xpPerLevel = {100,200,500,1000,2000,5000};
    int level = 0;
    for(int i: xpPerLevel ){
        if( xp >= i){
            level++;
        }
        else break;
    }
    return level;
}

EDIT 2: Maybe you want to do something for each level and not just calculate the level. Then I recommend first calculating level with function up there and then putting the result through a method like this:

public void EvaluateLevel(int level){
    switch(level){
        case 4:
            //do stuff for level 4
        case 3:
            //do stuff for level 3
        case 2:
            //do stuff for level 2
        case 1:
            //do stuff for level 1
        case 0:
            //do stuff for level 0
    }
}

Just a reminder, if in switch you don't use break; at and of each case block then all the block of cases bellow are also run. If you don't want to effects of levels stack on top of each other, use break; after each block.

You are overthinking this whole process. Your new level is not dependent on your current level but only on the current amount of XP. Therefore, you need a method that takes the total XP and calculates the level from it. Seeing your code, you intend to have the levels linearly distributed. For each 100 XP, the level should increase by 1. This logic can be achieved by this short snippet:

public int getLevel(int totalXp) {
    return totalXp / 100;
}

To integrate this method in the whole context, you can use something along the lines of...

int totalXP = Integer.parseInt(returnDBTotalXP());
int newLevel = getLevel(totalXP);
updateDBLevel(newLevel);

With the following code, you can easily test at what XP a new level is reached (useful for testing):

int maxInt = 800;
int currentLevel = -1;
for (int xp = 0; xp <= maxInt; xp++) {
    int newLevel = getLevel(xp);
    if (currentLevel != newLevel) {
        System.out.println(xp + " => " + newLevel);
        currentLevel = newLevel;
    }
}

Output:

0 => 0
100 => 1
200 => 2
300 => 3
400 => 4
500 => 5
600 => 6
700 => 7
800 => 8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM