0

for class I've been tasked with making a text based adventure game. Recently, I've been trying to program in battling and exp in the game. Here is what I am using to calculate it in the Player class:

int level = 1;
int exp = 0;
int close;

public void levelup() {
level += 1;
}

public int nextlevel() {

int x = level;
close = (4 * (x ^ 3) / 5);
return close;

}


public boolean levelready() {
return exp >= nextlevel();
}

Here is the calculations from the BattleManager class:

public void battleend() {
player.exp += expgain;

if (player.levelready()) {
print("You leveled up!");
player.levelup();
}


}

Now, the difficulty I'm having is that the nextlevel() method doesn't seem to always return the amount of exp required for the next level, it just returns 0. In the Room class I have a method that prints your stats to the screen.

The code is something like:

public void printstats() {


print("Level " + player.level);
print("EXP " + player.exp);
print("Next Level " + player.nextlevel());
}

Here is what's printed out before the battle:

Level 1
EXP 0
Next Level 1

Here is what's printed out after the battle:

Level 2
EXP 1
Next Level 0

Can somebody please help me with this? I would really appreciate it. Thank you!

Bread Bouquet
  • 23
  • 1
  • 1
  • 10

1 Answers1

1

I would like to thank Mahmoud Hossam for helping me figure out the problem. It turns out that the ^ operator in Java does not raise a number to a power but is a xor operator. Here's how I changed the method to fix the code!

public int nextlevel() {

double x = (double) level;

close = Math.round(4 * (Math.pow(x, 3)) / 5);

int next = (int) close;

return next;
}

tgdavies asked why I didn't just copy and paste the whole thing into here. This is because Room is currently over 500 lines, and I took out a bunch of unnecessary info from the method to make it easier to read. Here is the method in question:

String spc = "n\------";

public String STATS() {
String a = ("---Stats---\nHP" + dummy.curhp + "/" + dummy.maxhp + "n\Attack " + dummy.atktotal + "n\Defense" + dummy.deftotal);
a = (a + "n\Magick " + dummy.mgk + "n\Speed " + dummy.speed);
a = (a + spc + "n\Level " + dummy.level + "n\EXP " + dummy.exp + "Next Level " + dummy.nextlevel() + " EXP");

return a
}
Bread Bouquet
  • 23
  • 1
  • 1
  • 10