Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-03-2012, 01:11 PM   PM User | #1
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
Stacking an amount of damage in variable

I query for two things in a database, Health and Attack. It's setup in a while loop to repeat depleting the health. However it's doing this,

PHP Code:
You attacked enemy for 32 Damage. (204 Remaining)
You attacked enemy for 32 Damage. (204 Remaining)
You attacked enemy for 32 Damage. (204 Remaining)
You attacked enemy for 32 Damage. (204 Remaining
I can't remove the health because it's static information, not an actual enemy. I need to take the original health, and add up all the damage on each while loop record, and and after 9 rounds, if it's higher than the enemies health, then it displays enemy is dead, if not it slows the outcome of how much you attacked it for and it's remaining health.

So my question is how do you want a set value of say 100, and on every while loop keep adding the damage total up until it hits that number?
I thought about like $damage++; but don't know if that will work.
__________________
Been a sign maker for 5 years. My business:
American Made Signs

Last edited by myfayt; 01-03-2012 at 09:06 PM..
myfayt is offline   Reply With Quote
Old 01-03-2012, 01:15 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
$damage++

or

$damage=$damage+1;
if($damage > 100){
// do something
}
mlseim is offline   Reply With Quote
Old 01-03-2012, 05:40 PM   PM User | #3
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
Hmm it's now doing random values. Not adding them all together.
(You hit enemy for 555 Damage (-506) Remaining) that remaining value should decrease every time, and the minus value should mean it's dead.

PHP Code:
$time 1;

        
        while (
$time 10) {

        
$mydamage $userrow['attackpower'];
        
$mydamage2 $mydamage 2;

          
$userhp $userrow['currenthp'];
        
$enemy1hp $ginfo['maxhp'];
          
$damage rand($mydamage2,$mydamage);
        
$enemydam $ginfo['maxdam'];
        
$enemydam2 $enemydam 2;
        
$edam rand($enemydam2,$enemydam);
        
        
$hitfor $userhp $edam;
        
        
$remain $enemy1hp $damage;


          
$page .= "You hit enemy for ".number_format($damage)." Damage (".number_format($remain).") Remaining,<br />and receive ".number_format($edam)." Damage in return (".number_format($userrow['currenthp']).") Remaining.<br />
         <br /><br />"
;


          if (
$edam >= $userhp) {
            
$page .= '<h3><font color=white>You Were Killed!</font></h3>';
            
$updatequery doquery("UPDATE {{table}} SET currenthp=0 WHERE id='".$userrow['id']."' LIMIT 1""users");
          }
          else {
            
$updatequery doquery("UPDATE {{table}} SET currenthp=currenthp-$edam WHERE id='".$userrow['id']."' LIMIT 1""users");
          }

          
$time++;
          
$remain++;
        } 
PHP Code:
You hit enemy for 555 Damage (-506Remaining,
and 
receive 20 Damage in return (1,127Remaining.


You hit enemy for 739 Damage (-690Remaining,
and 
receive 19 Damage in return (1,127Remaining.


You hit enemy for 702 Damage (-653Remaining,
and 
receive 35 Damage in return (1,127Remaining.


You hit enemy for 685 Damage (-636Remaining,
and 
receive 24 Damage in return (1,127Remaining.


You hit enemy for 983 Damage (-934Remaining,
and 
receive 24 Damage in return (1,127Remaining.


You hit enemy for 556 Damage (-507Remaining,
and 
receive 34 Damage in return (1,127Remaining.


You hit enemy for 901 Damage (-852Remaining,
and 
receive 37 Damage in return (1,127Remaining.


You hit enemy for 994 Damage (-945Remaining,
and 
receive 33 Damage in return (1,127Remaining.


You hit enemy for 550 Damage (-501Remaining,
and 
receive 21 Damage in return (1,127Remaining
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 01-03-2012, 05:59 PM   PM User | #4
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by myfayt View Post
Hmm it's now doing random values. Not adding them all together.
(You hit enemy for 555 Damage (-506) Remaining) that remaining value should decrease every time, and the minus value should mean it's dead.

PHP Code:
$time 1;

        
        while (
$time 10) {

        
$mydamage $userrow['attackpower'];
        
$mydamage2 $mydamage 2;

          
$userhp $userrow['currenthp'];
        
$enemy1hp $ginfo['maxhp'];
          
$damage rand($mydamage2,$mydamage);
        
$enemydam $ginfo['maxdam'];
        
$enemydam2 $enemydam 2;
        
$edam rand($enemydam2,$enemydam);
        
        
$hitfor $userhp $edam;
        
        
$remain $enemy1hp $damage;


          
$page .= "You hit enemy for ".number_format($damage)." Damage (".number_format($remain).") Remaining,<br />and receive ".number_format($edam)." Damage in return (".number_format($userrow['currenthp']).") Remaining.<br />
         <br /><br />"
;


          if (
$edam >= $userhp) {
            
$page .= '<h3><font color=white>You Were Killed!</font></h3>';
            
$updatequery doquery("UPDATE {{table}} SET currenthp=0 WHERE id='".$userrow['id']."' LIMIT 1""users");
          }
          else {
            
$updatequery doquery("UPDATE {{table}} SET currenthp=currenthp-$edam WHERE id='".$userrow['id']."' LIMIT 1""users");
          }

          
$time++;
          
$remain++;
        } 
PHP Code:
You hit enemy for 555 Damage (-506Remaining,
and 
receive 20 Damage in return (1,127Remaining.


You hit enemy for 739 Damage (-690Remaining,
and 
receive 19 Damage in return (1,127Remaining.


You hit enemy for 702 Damage (-653Remaining,
and 
receive 35 Damage in return (1,127Remaining.


You hit enemy for 685 Damage (-636Remaining,
and 
receive 24 Damage in return (1,127Remaining.


You hit enemy for 983 Damage (-934Remaining,
and 
receive 24 Damage in return (1,127Remaining.


You hit enemy for 556 Damage (-507Remaining,
and 
receive 34 Damage in return (1,127Remaining.


You hit enemy for 901 Damage (-852Remaining,
and 
receive 37 Damage in return (1,127Remaining.


You hit enemy for 994 Damage (-945Remaining,
and 
receive 33 Damage in return (1,127Remaining.


You hit enemy for 550 Damage (-501Remaining,
and 
receive 21 Damage in return (1,127Remaining
You never update the enemy1hp value in that loop. So, every look you find the difference from the starting hp and the current damage done (add the damage to the remaining hp in your example for all the output and you'll see they all equal 49). Dunno why you would want to increment $remain before each loop .

I think the problem is just poor logic - not enough thought gone into the design. You're reinitialising the values every single time you loop. If you're effecting variables inside a loop, you should keep the initialisation outside the loop. Like this:
PHP Code:
// Find initial hp before taking effect on it
$enemyHP x;

// Start looping. Start $i at 0, increment it once per iteration and iterate while $i < 10
for($i=0;$i<10;$i++){
    
// Do whatever actions you want
    // But importantly, at the end of the loop
    
$enemyHP -= $damage;
    
// Then, when the next loop uses $enemyHP, it will be the updated HP with damage taken off

__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 01-03-2012, 06:16 PM   PM User | #5
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
I thought it was working, but it seems it's adding health rather than removing it.

PHP Code:
 $getenemy doquery("SELECT * FROM {{table}} WHERE level='$level' LIMIT 1""monsters");
         
$ginfo mysql_fetch_array($getenemy);
         
$userhp $userrow['currenthp'];
        
$enemy1hp $ginfo['maxhp'];
         
$enemydam $ginfo['maxdam'];

        
$time 1;

        
        while (
$time 10) {


        
$mydamage2 $mydamage 2;


          
$damage rand($mydamage2,$mydamage);

        
$enemydam2 $enemydam 2;
        
$edam rand($enemydam2,$enemydam);
        
        
$hitfor $userhp $edam;
        
        
$remain $enemy1hp $damage;
          
$enemyHP -= $remain;

          
$page .= "You hit enemy for ".number_format($damage)." Damage (".number_format($enemyHP).") Remaining,<br />and receive ".number_format($edam)." Damage in return (".number_format($userrow['currenthp']).") Remaining.<br />
         <br /><br />"
;


          if (
$edam >= $userhp) {
            
$page .= '<h3><font color=white>You Were Killed!</font></h3>';
            
$updatequery doquery("UPDATE {{table}} SET currenthp=0 WHERE id='".$userrow['id']."' LIMIT 1""users");
          }
          else {
            
$updatequery doquery("UPDATE {{table}} SET currenthp=currenthp-$edam WHERE id='".$userrow['id']."' LIMIT 1""users");
          }

          
$time++;

        } 

PHP Code:
You hit enemy for 719 Damage (608Remaining,
and 
receive 36 Damage in return (3,000Remaining.


You hit enemy for 818 Damage (1,315Remaining,
and 
receive 41 Damage in return (3,000Remaining.


You hit enemy for 710 Damage (1,914Remaining,
and 
receive 54 Damage in return (3,000Remaining.


You hit enemy for 827 Damage (2,630Remaining,
and 
receive 64 Damage in return (3,000Remaining.


You hit enemy for 665 Damage (3,184Remaining,
and 
receive 41 Damage in return (3,000Remaining.


You hit enemy for 644 Damage (3,717Remaining,
and 
receive 46 Damage in return (3,000Remaining.


You hit enemy for 824 Damage (4,430Remaining,
and 
receive 36 Damage in return (3,000Remaining.


You hit enemy for 874 Damage (5,193Remaining,
and 
receive 59 Damage in return (3,000Remaining.


You hit enemy for 692 Damage (5,774Remaining,
and 
receive 59 Damage in return (3,000Remaining
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 01-03-2012, 07:19 PM   PM User | #6
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by myfayt View Post
I thought it was working, but it seems it's adding health rather than removing it.

PHP Code:
 $getenemy doquery("SELECT * FROM {{table}} WHERE level='$level' LIMIT 1""monsters");
         
$ginfo mysql_fetch_array($getenemy);
         
$userhp $userrow['currenthp'];
        
$enemy1hp $ginfo['maxhp'];
         
$enemydam $ginfo['maxdam'];

        
$time 1;

        
        while (
$time 10) {


        
$mydamage2 $mydamage 2;


          
$damage rand($mydamage2,$mydamage);

        
$enemydam2 $enemydam 2;
        
$edam rand($enemydam2,$enemydam);
        
        
$hitfor $userhp $edam;
        
        
$remain $enemy1hp $damage;
          
$enemyHP -= $remain;

          
$page .= "You hit enemy for ".number_format($damage)." Damage (".number_format($enemyHP).") Remaining,<br />and receive ".number_format($edam)." Damage in return (".number_format($userrow['currenthp']).") Remaining.<br />
         <br /><br />"
;


          if (
$edam >= $userhp) {
            
$page .= '<h3><font color=white>You Were Killed!</font></h3>';
            
$updatequery doquery("UPDATE {{table}} SET currenthp=0 WHERE id='".$userrow['id']."' LIMIT 1""users");
          }
          else {
            
$updatequery doquery("UPDATE {{table}} SET currenthp=currenthp-$edam WHERE id='".$userrow['id']."' LIMIT 1""users");
          }

          
$time++;

        } 

PHP Code:
You hit enemy for 719 Damage (608Remaining,
and 
receive 36 Damage in return (3,000Remaining.


You hit enemy for 818 Damage (1,315Remaining,
and 
receive 41 Damage in return (3,000Remaining.


You hit enemy for 710 Damage (1,914Remaining,
and 
receive 54 Damage in return (3,000Remaining.


You hit enemy for 827 Damage (2,630Remaining,
and 
receive 64 Damage in return (3,000Remaining.


You hit enemy for 665 Damage (3,184Remaining,
and 
receive 41 Damage in return (3,000Remaining.


You hit enemy for 644 Damage (3,717Remaining,
and 
receive 46 Damage in return (3,000Remaining.


You hit enemy for 824 Damage (4,430Remaining,
and 
receive 36 Damage in return (3,000Remaining.


You hit enemy for 874 Damage (5,193Remaining,
and 
receive 59 Damage in return (3,000Remaining.


You hit enemy for 692 Damage (5,774Remaining,
and 
receive 59 Damage in return (3,000Remaining
It's because, no offence, but you've just copy pasted my answer instead of thinking about my explanation. Reading, following and understanding answers to your problems provides a better understanding of PHP.

The way you've set it up just now, you're working out the difference between damage and HP, then taking it away from $enemyHP? Why? $enemyHP at that point will be 0, so you'll be taking a negative value away from 0 (which is the same as addition) and using that as remaining HP. Wrong. Following my previous reply, change this:
PHP Code:
$remain $enemy1hp $damage;
          
$enemyHP -= $remain
to this:
PHP Code:
$enemy1hp -= $damage
This is a prime example of why code design is hugely important. Next time, think about your logic flow and what you need to do. You need to have a running total type value of enemy hp, taking away the damage each iteration.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Last edited by BluePanther; 01-03-2012 at 07:21 PM..
BluePanther is offline   Reply With Quote
Users who have thanked BluePanther for this post:
myfayt (01-03-2012)
Old 01-03-2012, 09:03 PM   PM User | #7
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
I didn't just copy your code, I am not good with formulas at all, I get confused easily. I've been at this for over 6 hours already on my own. Then I decided on some help.

Please bear with me. I made the changes and it seems to be working fine, thank you.
__________________
Been a sign maker for 5 years. My business:
American Made Signs

Last edited by myfayt; 01-03-2012 at 09:05 PM..
myfayt is offline   Reply With Quote
Old 01-03-2012, 09:07 PM   PM User | #8
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by myfayt View Post
I didn't just copy your code, I am not good with formulas at all, I get confused easily. I've been at this for over 6 hours already on my own. Then I decided on some help.

Please bear with me. I made the changes and it seems to be working fine, thank you.
You're probably still using $enemyHP to echo out the HP remaining. Use $enemy1hp
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:44 PM.


Advertisement
Log in to turn off these ads.