Problems with While Loop

Reply

Join Date: Apr 2006
Posts: 20
Reputation: Ries is an unknown quantity at this point 
Solved Threads: 0
Ries Ries is offline Offline
Newbie Poster

Problems with While Loop

 
0
  #1
Oct 16th, 2006
Im having a problem that my while loop is not finishing even when the conditions have been meet.

[php]$turn = "0";
while ($player['health_n'] > "0" OR $opponent['health_n'] > "0" AND $turn < "25"){
$turn = $turn+1;
$hitfor = $youatk / $oppdef;
if ($hitfor <= "0"){
echo "$turn. $opponent[name] blocked.<br />";
}else{
echo "$turn. You hit $opponent[name] for $hitfor damage.<br />";
mysql_query("UPDATE members SET health_n=health_n-$hitfor WHERE id='$opponent[id]' ")or die(mysql_error());
}
$turn = $turn+1;
$atkfor = $oppatk / $youdef;
if ($atkfor <= "0"){
echo "$turn. You blocked.<br />";
}else{
echo "$turn. $opponent[name] hit you for $atkfor damage.<br />";
mysql_query("UPDATE members SET health_n=health_n-$atkfor WHERE id='$player[id]' ")or die(mysql_error());
}
}[/php]

I am getting the data from the database outside of the while loop; if this is any help.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

Re: Problems with While Loop

 
0
  #2
Oct 16th, 2006
try this:
[php]
while ($player['health_n'] || $opponent['health_n'] && $turn < 25)

[/php]
If a variable is zero it can be thought of as FALSE, and also you were looking for $turn that was greater than the string 25. see if that helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 20
Reputation: Ries is an unknown quantity at this point 
Solved Threads: 0
Ries Ries is offline Offline
Newbie Poster

Re: Problems with While Loop

 
0
  #3
Oct 17th, 2006
That sorta worked except it was checking the right thing, I wanted it to perform the loop if the player health was above 0 OR opponent health was above 0 OR turns where below 25. So something like this

[php]while ($player['health_n'] > "0" || $opponent['health_n'] > "0" || $turn < 25)[/php]

Except it does not work. This happens whenever I do == or > or => or althing like that with the player/opponent health and so your previous example not using any conditional for the arrays worked but it did not create the desired effect.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

Re: Problems with While Loop

 
0
  #4
Oct 17th, 2006
you are checking the value of strings and NOT integers. stop putting the numbers in quotes, you dont need to do that.

Also, this snippet you posted is different, you have three || statements not 2 || and an && statemnt. is that what you wanted to do all along?
Have you tried echoing out the variables in your script, before after and during your loop??
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 20
Reputation: Ries is an unknown quantity at this point 
Solved Threads: 0
Ries Ries is offline Offline
Newbie Poster

Re: Problems with While Loop

 
0
  #5
Oct 17th, 2006
Yes having 3 || is what I wanted.

EDIT: Ok I've just tried removing the "" and it still carry's on after the turns have reached above 25 and the health is below 0. i'll show you my updated code

[php]while ($player['health_n'] > 0 || $opponent['health_n'] > 0 || $turn < 25){
$turn = $turn+1;
$hitfor = $youatk / $oppdef;
if ($hitfor <= "0"){
echo "$turn. $opponent[name] blocked.<br />";
}else{
echo "$turn. You hit $opponent[name] for $hitfor damage.<br />";

mysql_query("UPDATE members SET health_n=health_n-$hitfor WHERE id='$opponent[id]' ")or die(mysql_error());
}
$turn = $turn+1;
$atkfor = $oppatk / $youdef;
if ($atkfor <= "0"){
echo "$turn. You blocked.<br />";
}else{
echo "$turn. $opponent[name] hit you for $atkfor damage.<br />";
mysql_query("UPDATE members SET health_n=health_n-$atkfor WHERE id='$player[id]' ")or die(mysql_error());

}
}[/php]

Basically this loop is supposed to run until the health of someone reaches 0 or turns get higher than 25 (Whichever comes first.) And then after this I want to give the player bonuses for winning so I have:
[php]if ($player['health_n'] <= "0"){[/php]

Too check if he lost and

[php]}elseif ($opponent['health_n'] <= "0"){[/php]

If he won (Also a if condition to check if the 25 turn limit was reached and so to set it as a draw.)

But whats happening is that while loop above isn't stopping, it carrys on and on.
Last edited by Ries; Oct 17th, 2006 at 3:22 pm. Reason: Dont want to double-post
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

Re: Problems with While Loop

 
0
  #6
Oct 17th, 2006
[php]
while ($player['health_n'] > "0" OR $opponent['health_n'] > "0" AND $turn < "25"){
[/php]
is from your first post. I guess I was confused at what your intentions were...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 20
Reputation: Ries is an unknown quantity at this point 
Solved Threads: 0
Ries Ries is offline Offline
Newbie Poster

Re: Problems with While Loop

 
0
  #7
Oct 17th, 2006
Must have not been thinking when I was coding it, sorry for misleading ya and wasting your time.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC