oni-kun Posted January 4, 2010 Share Posted January 4, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187078-how-to-calculate-level-based-on-exp/ Share on other sites More sharing options...
mikesta707 Posted January 4, 2010 Share Posted January 4, 2010 that loops looks like it would be endless. perhaps it should be just greater than rather than greater than or equal Quote Link to comment https://forums.phpfreaks.com/topic/187078-how-to-calculate-level-based-on-exp/#findComment-987908 Share on other sites More sharing options...
oni-kun Posted January 4, 2010 Author Share Posted January 4, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187078-how-to-calculate-level-based-on-exp/#findComment-987909 Share on other sites More sharing options...
teamatomic Posted January 4, 2010 Share Posted January 4, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187078-how-to-calculate-level-based-on-exp/#findComment-987921 Share on other sites More sharing options...
oni-kun Posted January 4, 2010 Author Share Posted January 4, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/187078-how-to-calculate-level-based-on-exp/#findComment-987943 Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2010 Share Posted January 4, 2010 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/187078-how-to-calculate-level-based-on-exp/#findComment-988023 Share on other sites More sharing options...
oni-kun Posted January 4, 2010 Author Share Posted January 4, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/187078-how-to-calculate-level-based-on-exp/#findComment-988026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.