0

I'm trying to make a small piece of code that checks amount of items needed to reach a certain level, as you increase in level the amount of exp you get per item increases, in this case it's your level*10.

But it seems as though from what I've tried that the if statement is completely ignored, I don't really know how to proceed from here.

This is the piece of PHP code:

$currentexp = 0;
$currentlevel = 1;
$nextlevel = 83; //value is experience
$goallevel = 13034431; //value is experience
$itemamount = 0;
$itemexp = $currentlevel * 10;

while ($currentexp < $goallevel) {
    $currentexp = $currentexp + $itemexp;
    $itemamount = $itemamount + 1;
    if ($currentexp > $nextlevel) {
        $nextlevel = $nextlevel * 1.10408986946;
        $currentlevel = $currentlevel + 1;
    }
}

echo "You will need " . $itemamount . " items for your goal level";

The output of $itemamount should be some odd 15000 given the start and end point..

2
  • while loop and if condition are executed.That is what i can tell so far. $itemamount is increased by one by every iteration. So its a much as $goallevel Commented Nov 16, 2018 at 16:30
  • @PhilippPalmtag the output it gives me is the goal level divided by 10 or something, I'm assuming it doesn't add a level to $currentlevel Commented Nov 16, 2018 at 16:35

1 Answer 1

1

while loop and if condition are executed.That is what i can tell so far. $itemamount is increased by one by every iteration. So its a much as $goallevel. Your variables in your loop condition do not change, so its executed for every $goallevel

    $currentexp = 0;
    $currentlevel = 1;
    $nextlevel = 83; //value is experience
    $goallevel = 13034431; //value is experience
    $itemamount = 0;
    $itemexp = 10;

    while ($currentexp < $goallevel) {
        $currentexp = $currentexp + $itemexp;
        $itemamount = $itemamount + 1;
        if ($currentexp > $nextlevel) {
            $nextlevel = $nextlevel * 1.10408986946;
            $currentlevel = $currentlevel + 1;
            $itemexp = $currentlevel * 10;
        }
    }

    echo "You will need " . $itemamount . " items for your goal level".PHP_EOL;

Output is: You will need 11766 items for your goal level

Update

Somehow your experience calculation is wrong. When using a fixed table like in the Excel sheet, I get the correct values, see this example (I shortened the experience table!):

$currentexp = 0;
$currentlevel = 1;
$goallevel = 13034431; //value is experience
$itemamount = 0;
$itemexp = 10;


$exp_borders = [
    0,
    83,
    174,

    // [...] shortened!

    8771558,
    9684577,
    10692629,
    11805606,
    13034431,
];

while ($currentexp < $goallevel) {
    $currentexp = $currentexp + $itemexp;
    $itemamount = $itemamount + 1;
    if ($currentexp > $exp_borders[$currentlevel]) {
        $currentlevel = $currentlevel + 1;
        $itemexp = $currentlevel * 10;
    }
}

echo "You will need " . $itemamount . " items for your goal level".PHP_EOL;

Output is like expected: You will need 15057 items for your goal level

Sign up to request clarification or add additional context in comments.

6 Comments

Could you be so kind as to explain the changes you made to the code? The output of 11766 is still not as expected, you can see a table here: docs.google.com/spreadsheets/d/…
with your code, if I set $currentexp = 11805606, $currentlevel = 98, $nextlevel = 13034431, and $goallevel = 13034431, it will output 1254, which is the correct value, i don't really get it
It seems that the problem is it adds extra levels? for example if $currentexp is 0, $currentlevel is 1, $nextlevel is 83, and $goallevel is 174, it goes up to level 5, whereas 174 exp is level 3.
I see the issue now... every level is calculated as the experience needed to go from one level to another, so instead of stopping at level 99 with 1228825 experience between 98 and 99, it will stop at 122, with 13.2something million exp between 121 and 122. guess i'll have to figure out how to change that to work how i want
@T.H. Please see my updated answer. Your exp calculation value (1.10408986946) seems to be wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.