Jump to content

[SOLVED] While statement help (noob)


shergold

Recommended Posts

Hey, i was wondering if someone would be kind enough to help me fix this while statement. Sorry if its a stupid mistake but im just learning php and this has been bugging me for the past hour.

 

<?php
//player
$lvl = 10;
$maxhit = $lvl *15;
$minhit = $lvl *15/4;
$yourhp = $lvl *100;
//opponent
$oplvl = 8;
$opmaxhit = $oplvl *15;
$opminhit = $oplvl *15/4;
$hp = $oplvl *100;
while ($hp>0 && $yourhp>0)
{
$yourhit = rand($minhit,$maxhit);
$hp - $yourhit;
echo "You hit {$yourhit}, leaving your opponent with {$hp}hp!</br>";
}
?>

 

When i execute the php file i get an infinite loop, the echo statement displays a random number correctly but the hit isnt subtracted from the hp.

 

for example: You hit 57, leaving your opponent with 800hp!.

only the random number changes but the hp stays on 800.

 

Thanks,

Shergold.

Link to comment
Share on other sites

ah -.- thankyou i didnt realise i had to do that.

also when the hp has gone down to a certain point say "17", and the $hit value is for example "100" it will stop.

could you give me an example as to how i can get around that and make it take off the remaining hp amount rather than stopping when the hit amount is greater than the total hp?

 

Thanks again,

Shergold.

 

Edit: i just added this to the while statement: if ($yourhit>$hp) $hp= $hp - $hp;

but the hp goes into minus :S

Edit again: actually the while statement goes into minus without the if statement.

Link to comment
Share on other sites

Here is an example:

You hit 106, leaving your opponent with 694hp!

You hit 118, leaving your opponent with 576hp!

You hit 39, leaving your opponent with 537hp!

You hit 150, leaving your opponent with 387hp!

You hit 147, leaving your opponent with 240hp!

You hit 136, leaving your opponent with 104hp!

You hit 133, leaving your opponent with -29hp!

 

that is without the if statement i tried, i want it so that if the hit is greater than the hp, just the remaining hp amount is taken off so the end value is 0.

 

Thanks,

Shergold.

Link to comment
Share on other sites

ah sorry as i said before im a noob , i dont understand any of this:

$hp = ($hp < 0)? 0 : $hp;

 

sorry but please could you explain it to me and how i can implement that into my code :S

 

thanks,

shergold.

 

it means if $hp is less than 0, make it 0, if not, make it $hp (more than 0)

Link to comment
Share on other sites

To add to that:

 

Replace the code I gave you before with that.

 

What it means is:

 

If the thing in brackets is true, then assign the value of the thing after the question mark to the variable, and if it's false, then assign the thing after the colon to the variable.

 

For example:

 

$a = (1==1) ? $b : $c;

 

You are assigning either $b or $c to $a. Since 1 does equal 1, after this code, $a will be equal to $b.

 

$a = (1 == 2) ? $b : $c;

 

In this case, 1 doesn't equal 2, so after this code, $a will be equal to $c.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.