I have two issues/questions with figuring out a proper way to write this code for a player leveling system.
My First question/issue is that when this code is run it works as intended with a slight problem. public void levelUp()
is being used to, "set", the level if you will (I am sure this is a sloppy way of doing this but we will get to that in the next question) while levelUpXp()
is being used as the only way I currently know how to store the elements of int[] requiredXP
to let the game know when the player has reached X amount of xp to level him up to X level. ding()
is simply a notification for the player to be aware that they have increased in level.
As stated before this all works the way I am wanting it to work however upon implementing ding()
after a countless amount of time trying to write multiple if statements to do what ding is doing now, I have run into an issue where curXP
is NOT equal to either reqXP
or requiredXP[]
due to the amount of XP the player is gaining not always being an exact number within requiredXP[]
. While I already assumed this would happen, it is now resulting in either a level up notification not being sent because curXP == requiredXP[x]
is not always the case if the amount of xp needed to level is 17 but after killing an enemy the player goes from 15xp to 18xp or curXP <= requiredXP[x]
causing the level up notification to be constantly sent until that player reaches their next level in which the notification is still sent however with a new level attached.
Second question will be below code.
Here is my player class and methods:
public class Player extends Creature {
int health = 100;
int maxHealth = 100;
int attackDamage = 25;
int numHealthPotions = 3;
int healthPotHeal = 30;
int curXP = 0;
int level = 0;
int reqXP = 0;
int[] currentLevel = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] requiredXP = { 0, 6, 17, 36, 65, 105, 158, 224, 305, 402 };
public void levelUp() {
if (curXP == requiredXP[0]) {
level = currentLevel[0];
} else if (curXP == requiredXP[1]) {
level = currentLevel[1];
} else if (curXP == requiredXP[2]) {
level = currentLevel[2];
} else if (curXP == requiredXP[3]) {
level = currentLevel[3];
} else if (curXP == requiredXP[4]) {
level = currentLevel[4];
} else if (curXP == requiredXP[5]) {
level = currentLevel[5];
} else if (curXP == requiredXP[6]) {
level = currentLevel[6];
} else if (curXP == requiredXP[7]) {
level = currentLevel[7];
} else if (curXP == requiredXP[8]) {
level = currentLevel[8];
} else if (curXP == requiredXP[9]) {
level = currentLevel[9];
}
}
public void levelUpXp() {
if (level == currentLevel[0]) {
reqXP = requiredXP[0];
} else if (level == currentLevel[1]) {
reqXP = requiredXP[1];
} else if (level == currentLevel[2]) {
reqXP = requiredXP[2];
}else if (level == currentLevel[3]) {
reqXP = requiredXP[3];
} else if (level == currentLevel[4]) {
reqXP = requiredXP[4];
}else if (level == currentLevel[5]) {
reqXP = requiredXP[5];
} else if (level == currentLevel[6]) {
reqXP = requiredXP[6];
}else if (level == currentLevel[7]) {
reqXP = requiredXP[7];
} else if (level == currentLevel[8]) {
reqXP = requiredXP[8];
}else if(level == currentLevel[9]) {
reqXP = requiredXP[9];
}
}
public void ding() {
if(level == 2 && curXP == requiredXP[1]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
} else if(level == 3 && curXP == requiredXP[2]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
} else if(level == 4 && curXP == requiredXP[3]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
} else if(level == 5 && curXP == requiredXP[4]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
} else if(level == 6 && curXP == requiredXP[5]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
} else if(level == 7 && curXP == requiredXP[6]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
} else if(level == 8 && curXP == requiredXP[7]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
} else if(level == 9 && curXP == requiredXP[8]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
} else if(level == 10 && curXP == requiredXP[9]) {
System.out.println(" #############################");
System.out.println(" # You have reached level " + level + "! # ");
System.out.println(" #############################");
}
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public void Level() {
int maxLevel = 200;
int maxLevelXP = 1000000;
for (int currentLevel = 1; currentLevel < maxLevel; currentLevel += 1) {
float x = currentLevel / (float) maxLevel;
double y = Math.pow(x, 2.61);
int requiredXP = (int) (y * maxLevelXP);
System.out.println("Level " + currentLevel + " XP: " + requiredXP);
}
}
Here is game class where ding and other previously mentioned Methods are used:
GAME: while (running) {
p.levelUp();
p.levelUpXp();
System.out.println("-----------------------------------------------");
String enemy = e.enemies[rand.nextInt(e.enemies.length)];
int enemyHealth = e.enemyHealth;
int preLevel = 1;
int curLevel = preLevel + 1;
System.out.println("\t# " + enemy + " has appeared! #\n");
while (enemyHealth > 0) {
System.out.println(
"\t" + userName + "\n\t HP: " + p.health + " Level: " + p.level + " Exp: " + p.curXP + "\n\t" + "\n\t" + enemy + "\n\t HP: " + enemyHealth);
System.out.println("\n\t What would you like to do?");
System.out.println("\t 1. Attack");
System.out.println("\t 2. Drink health potion");
System.out.println("\t 3. Run!");
String input = in.nextLine();
if (input.equals("1")) {
int damageDealt = rand.nextInt(p.attackDamage);
int damageTaken = rand.nextInt(e.maxEnemyAD);
enemyHealth -= damageDealt;
p.health -= damageTaken;
if (damageDealt > 0) {
System.out.println("\t> You strike the " + enemy + " for " + damageDealt + " damage!");
} else if (damageDealt < 1) {
System.out.println("\t> You attempt to hit the " + enemy + " but miss!");
}
if (damageTaken > 0) {
System.out.println("\t> The " + enemy + " retaliates! You take " + damageTaken + " damage!");
} else if (damageTaken < 1) {
System.out.println("\t> The " + enemy + " retaliates but misses!");
}
if (p.health < 1) {
System.out.println(
"\t ##################" + "\n\t # You Have Died! #" + "\n\t ##################");
break;
}
} else if (input.equals("2")) {
if (p.numHealthPotions > 0 && p.health != p.maxHealth) {
p.health += p.healthPotHeal;
p.numHealthPotions--;
if (p.health > p.maxHealth) {
p.health = p.maxHealth;
}
System.out.println("\t> You drink the Health Potion!" + "\n\t> You now have " + p.health
+ " Health!" + "\n\t> You now have " + p.numHealthPotions + " Health Potion(s) left!");
} else if (p.health == p.maxHealth) {
System.out.println("\t> Your health is already full!");
} else if (p.numHealthPotions < 1) {
System.out.println("\t You have no Health Potions left!");
}
} else if (input.equals("3")) {
System.out.println("\t> You run away from the " + enemy + "!");
continue GAME;
} else {
System.out.println("\t Invalid Command!");
}
}
if (p.health < 1) {
System.out.println("You fought bravely but in the end, you're just another corpse...");
break;
}
System.out.println("-----------------------------------------------");
if (enemyHealth < 1) {
p.curXP += 3;
p.levelUp();
p.levelUpXp();
}
System.out.println(" # The " + enemy + " was defeated! # ");
System.out.println(" # You have " + p.health + " HP left! # ");
System.out.println(" # You have gained " + e.xpGive + " xp! # ");
p.ding();
if (rand.nextInt(100) < e.dropChance) {
p.numHealthPotions++;
System.out.println(" # The " + enemy + " droppped a Health Potion! # ");
System.out.println(" # You now have " + p.numHealthPotions + " Health Potion(s)! # ");
}
System.out.println("-----------------------------------------------");
System.out.println("\tWhat would you like to do?");
System.out.println("\t1. Press on!");
System.out.println("\t2. Get me out of here!");
String input = in.nextLine();
while (!input.equals("1") && !input.equals("2")) {
System.out.println("\tInvalid Command!");
input = in.nextLine();
}
if (input.equals("1")) {
System.out.println("\t> You march on, deeper into the Dungeon!");
} else if (input.equals("2")) {
System.out.println(
"\tYou exit the dungeon. Battered and bruised as you may be, you live to fight another day!");
break;
}
}
}
Now my second and last question is with my Player class and more specifically the way my levelUp()
, levelUpXp()
and ding()
Methods are written. There has to be a better way right? I have read Javadocs, watched tutorials, read physical books and I have even done courses on sites like Codecademy and just can't seem to wrap my head around this.
My initial goal was to have the Array automatically switch from currentLevel[0]
(Level 1) to currentLevel[1]
(Level 2) once the amount of reqXP
of requiredXP[1]
was met. I know a .next()
exists for ArrayLists similar to rand.nextInt()
but I don't know if that is exclusive to ArrayLists or if they can be used with Arrays as well. The current code you see now is a result of this lack of knowledge so any help or advice is much appreciated.
I am still new to programming and this is also only my second post in 2 years.
Creature
if that will enable reviewers to compile and run the code ourselves. Don't worry about including "too much" code - that's unlikely to be a problem here (we're very different to Stack Overflow in that respect!). \$\endgroup\$