Jump to content

How to calculate level based on exp?


oni-kun

Recommended Posts

Once a friend who was making a game came up with a little trouble calculating exp/level, He figured out a method but I've wanted for experience to learn how to do this exactly. I've come up with my own method: $exp % 60 for example, so it displays the remainder after it meeting the 'requirement'.

 

$exp = 100;
$level = 1;

while ($exp >= $exp % 60) {
   $exp = $exp % 60;
   $level++;
}

echo $level;

 

I thought that'd work, but when I put it in a while loop to iterate through itself, it just freezes, what is the problem with that loop?

Link to comment
https://forums.phpfreaks.com/topic/187078-how-to-calculate-level-based-on-exp/
Share on other sites

that loops looks like it would be endless. perhaps it should be just greater than rather than greater than or equal

 

I was just thinking (that if they got 100/100 exp) that it should be >= 100. I try this code:

$exp = 9900;
$level = 1;

while ($exp > $exp % 60) {
   $exp = $exp % 60;
   $level++;
}

echo $level;

 

It outputs '2' No matter what value I use. Is there a simple error in my math/logic preventing it from keep going up?

I fail to see how this will accomplish anything

while ($exp > $exp % 60)

it either going to run through once and drop out or be an infinite loop cause you do nothing to change any of the values. Maybe use

while ($exp > $exp % $level)

 

 

HTH

Teamatomic

I fail to see how this will accomplish anything

while ($exp > $exp % 60)

it either going to run through once and drop out or be an infinite loop cause you do nothing to change any of the values. Maybe use

while ($exp > $exp % $level)

 

 

HTH

Teamatomic

 

Mhmm yes! I was confusing myself over the modulus, I used it right in the while loop but it shouldn't have in the code:

$exp = 200;
$gaintolvlup = 60
$level = 1;

while ($exp > ($exp % $gaintolvlup)) { 
   $exp = $exp - 60;
   $level++;
}
echo "Exp: " . $exp . "<br/>\n"; //Exp: 20 (from remainder/60)
echo "Level: " . $level; //Level: 4

 

That works fine, and will only 'level up' if it's /60.

 

Thanks!

I don't see the reason for a loop can't you just do this

 

<?php
$exp = 200;
$gaintolvlup = 60; 	

echo "Exp: " . ($exp % $gaintolvlup) . "<br/>\n"; //Exp: 20 (from remainder/60)
echo "Level: " . ceil($exp/$gaintolvlup); //Level: 4
?>

I don't see the reason for a loop can't you just do this

 

<?php
$exp = 200;
$gaintolvlup = 60; 	

echo "Exp: " . ($exp % $gaintolvlup) . "<br/>\n"; //Exp: 20 (from remainder/60)
echo "Level: " . ceil($exp/$gaintolvlup); //Level: 4
?>

 

That code works fine, But I made it more dynamic ( in case you're wondering something like):

while ($exp >= ($exp % (60*(1.2*$level)))) {
   $exp = $exp - (60*(1.2*$level));
   $level++;
   
   if ($exp < (60*(1.2*$level)))  {
      break;
   }
   echo "<br/> Reached level: " . $level . "<br/> " . $exp .  "\n\n";
}

 

Not sure if it's the best of ways, but it dynamically increases the amount of EXP needed to level up, and displays the current exp/max as so. It's perfect for my game..er.. concept of a part of a game.

 

A loop is required for the obvious dynamic part of it!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.