Page 1 of 1

Do you get these problems? [SOLVED]

Posted: Wed Jul 20, 2011 1:33 am
by alexrules01
Do you get problems sometimes where a while or if statement won't work? I am currently using a while statement, but one of the conditions are not triggering once the variable is changed

I have:

Code: Select all

while ($winner == 0 || $b < 200)
{
if
{
     if
     {
     $winner++;
     }
}
}
Thats the basic structure of it, hopefully you won't need the rest of the code.
The $b variable is fine, it increments by 1 each loop, so there is 200 loops
The trouble I am having is the while statement doesnt seem to be recognising that $winner has changed. I even print the $winner variable after each loop and it prints 1, then maybe 2 and 3 before it cuts off at 200 (sometimes it goes over 200, like to 205)
Does anyone know what is going on?

Re: Do you get these problems?

Posted: Wed Jul 20, 2011 1:58 am
by Jackolantern
Did you remove the conditions on the two IF statements to post the code here, or is there no conditionals on them? If it is the latter, that is a problem, because IF statements need them.

I know you removed some of the code from these statements, but it may be an issue of your IF statements growing too large to really look at and read well.

Re: Do you get these problems?

Posted: Wed Jul 20, 2011 3:08 am
by alexrules01
Yea the if statements do have conditions which work fine, I just typed that out in the post, but I'll c+p the code if you need it, I just thought it might be a simple fix

Re: Do you get these problems?

Posted: Wed Jul 20, 2011 3:09 am
by 62896dude
yeah, show us the whole code for that section

Re: Do you get these problems?

Posted: Wed Jul 20, 2011 3:16 am
by alexrules01

Code: Select all

$winner = 0;
$b = 0;
while ($winner == 0 || $b < 200)
{
	$i = 0; // Sets array value
	$hpstamina = $stamina + $health;
	$hpstamina2 = $stamina2 + $health2;
	if ($playerturn == $name)
	{
		$pinif = $hpstaminamax2 / 5;
		$randpin = rand(1,100);
		print "RandPin: " . $randpin . "<br>";
		print "HPstaminaMAX: " . $hpstaminamax2 . "<br>";
		print "HPStamina " . $hpstamina2 . "<br>";
		print "PinIf: " . $pinif . "<br>";
		if ($randpin < 50 && $hpstamina2 <= $pinif && $player2position == "Grounded")
		{
			print $name . " goes for a KO<br>";
			$pinattempt = rand(1,100) + $cover - $kickout2;
			if ($pinattempt > 50)
			{
				print "The winner is " . $name . "<br>";
				$winner = 1;
			}
			else
			{
				print $wrestler . "gets back up<br>";
				$player2position = "Opening";
			}
		}
$b++;
This is just a snippet of the code, where the winner is set if there is a winner.

Re: Do you get these problems?

Posted: Wed Jul 20, 2011 3:20 am
by alexrules01
I think I have just realised what is happening.
If a winner is set prior to the 200th loop, the loop continues until its completes 200.

If $winner is still 0 by the 200th loop, it keeps going after 200 until $winner = 1

Re: Do you get these problems?

Posted: Wed Jul 20, 2011 3:51 am
by Jackolantern
Correct. With no comments, I don't know all of the logic that goes into this, but OR WHILE loops (i.e. ($this == 0 || $a < 200)) are rare. Here you are saying the loop will run while either $winner == 0 or $b < 200.

From the last post you made, it sounds like maybe you meant to have an AND (i.e. &&) in the middle of the WHILE statement. Then the loop will run for up to 200 turns or until someone wins.

And just as a side note, this kind of logic error is called a "short circuit". Part of your code was not being evaluated due to the logic of the loop.

Re: Do you get these problems?

Posted: Wed Jul 20, 2011 5:46 am
by alexrules01
Oh yes you are correct Jack! Can't believe I didnt realise that lol. I was thinking of it differently, the and works well!

Re: Do you get these problems?

Posted: Wed Jul 20, 2011 6:29 am
by Jackolantern
Glad you got it fixed :) Can you mark this topic [resolved] or [solved]?