FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Php Calculations





janszmatt
I wanna make a level system for my browser-based game using only variables and calculations(*,/,+,-).

Variables:
$skillpoints
$divide

Ok here the level system

Level 1 needs 0 skill points
Level 2 needs 100 skill points
Level 3 needs 300 skill points
Level 4 needs 600 skill points

the skill points have to be divided by a variable $divide to get the level eg:

eg:

Level2 = 100/50
Level3 = 300/100
Level4 = 600/150
level X = $skillpoints/$variable

so how do i set the $divide variable to +50 every level
and how do i remove (not round up) the decimals after the division?

If there's another way witout using variables and calculations, i'm willing to hear but if it is possible, i would like to use variable coz it's simple...
coreymanshack
yea dude... u need to take a lesson in strings with php... there is some kind of built in extract function in php....it will let you extract the decimal... and making something for every level blah?

just do this...

if($lvl=="whateversomenumber"){
//put what you want it to do here.
}
else{
//do everything else you need if the number is not equal
}


Also if you are making a game with php i would suggest getting in touch with mysql... i love it.... its a good way of storing game data... rather than having it in a cookie or having it in a file somwhere... if this isn't what you wanted to know then get back to me.
SlowWalkere
I'm not sure I quite understand what you mean with the $divide part. But, if you already know the level number, and want to know what $divide should be, given that $divide starts at 0 for level 1 and increases by 50 points with every level, this formula should do...

Code:

$divide = ($level - 1) * 50;


As for the second part, I haven't found a truncate function in php, but I'm sure there is one. I had the same problem before and found that typecasting the result into an integer will effectively truncate any decimals.

Code:

$intResult = (integer)$floatResult;


Hope that helps.

-Walkere
coreymanshack
this will probably help with that one problem...


http://us2.php.net/str_replace
ganbate
use floor function to remove the decimal part.

example :
Code:

$result = floor($number);
Philip
ganbate wrote:
use floor function to remove the decimal part.

example :
Code:

$result = floor($number);


or you can use ceil function for decimal part also.

example :
Code:

$result = ceil($number);
Wink
janszmatt
ok thx for the remove decimal part..

for those of you who are unsure of my post above here's another version of it

to get to level 2 you'll need 100 skill points
level 3 needs 300 skill points
level 4 needs 600 skill points


k?
hexkid
janszmatt wrote:
to get to level 2 you'll need 100 skill points
level 3 needs 300 skill points
level 4 needs 600 skill points


Code:
<?php
function find_level($variable) {
  static $levels = array(0, 100, 300, 600);
  $level = 0;
  while (($variable > $levels[$level]) && ($level < count($levels))) {
    ++$level;
  }
  return $level;
}

for ($v=0; $v<800; $v+=rand(50,80)) {
  echo $v, ' :: ', find_level($v), "<br>\n";
}
?>
SlowWalkere
Although it's a simple pattern that determines how many skill points you need per level, I don't think you can write it in a simple equation. So, you need to write a function to do the work for you.

Hexkid's function will work fine, and you just need to enter in the skill points for every level in the array definition. Here's something slightly different that will work out the level requirements automatically.

Code:

   function getLevel($skillPoints)
   {
      $num = 1;
      
      while ($skillPoints > 0)
      {
         $skillPoints = $skillPoints - ($num * 100);
      
         $num++;
      }
      
      if ($skillPoints == 0)
         return ($num - 1);
      else
         return ($num - 2);
   }


The loop basically goes through levels 0 to the current level, decides how many skill points were needed to earn that level and then subtracts them. When you get to either zero or a negative number, you know you've found the person's level and the appropriate level is returned.
janszmatt
thx guys you help me alot Very Happy
Related topics
http://tuvanonline.com/library/index.php
PHP-Nuke platium?
PHP Write to line
Anyone good with PHP-NUKE?
PHP safety?
req: PHP to list files in directory, and link to them
php admin and mysql admin console
Mysql And PHP HELP PLZ
Great php editor
Script php about gallery
File upload with PHP, Build an upload database for your site
[php scripts ] phpweather&email
Including With PHP
ActionScript / PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.