...

View Full Version : while loop problem



ptmuldoon
07-25-2007, 01:00 AM
I'm trying to get the below loop to count backwards to find the last player turn in a game, and can't seem to get it work, as it keeps timing out on me.

Essentially, it should count back to 0, and when it hits 0 to loop back to the total number (ie, last player) in the game.


$numplayers = $numplayers + 1;

$last_player_id = $current_id - 1;

while(1){ // loop until a valid last player is found
if($last_player_id == 0) {// Reset player id to last player when reach 0
$last_player_id = $numplayers;
}

}

Fumigator
07-25-2007, 01:02 AM
"while(1)" will always evaluate to true, thus you are stuck in an endless loop. You can use "break" to break out of it, or use another condition on the loop.

PappaJohn
07-25-2007, 01:05 AM
... and $last_player_id will always equal $current_id - 1 since you set it to that before the loop and then never decrement the value.

ptmuldoon
07-25-2007, 01:33 AM
Thanks for the help, but I'm not sure if I follow why it evaluating true is an issue. Below is a bigger piece of the code.

Essentially, it starts first by getting the id of the last player (example 7). Then the loop starts. The loops first checks to see if the id is less than two, and if it less than 2, it will set the id to the total number of players in the game (ie, 15). The the loop will check the player state of that id. If the player state is inactive, the loop should stop. If the player state condition is not met, than it should loop back and subtract 1 from the id and start again.


$last_player_id = $current_id - 1;

while(1){ // loop until a valid last player is found
if($last_player_id < 2) {// Reset player id to max players when reach end
$last_player_id = $numplayers;
}

// make sure the next player is not dead
$sql = "SELECT state FROM game_{$game['id']} WHERE id = {$last_player_id}";
$last_players_state = get_one($sql);

if($last_players_state == 'inactive'){
break;
} else {
$last_player_id - 1;
}
}

Fumigator
07-25-2007, 04:49 AM
Ah, so you now show us the actual code. Is that it, or is there more? Shall we wait for the entire block of code before trying to see what's happening with it? (grr)

The code as you have posted will never break out of that loop if there are no inactive players in the range of 2 and $current_id - 1. It's bad coding to build loops that have no escape valve, which you have found out with an endless loop that causes the page to timeout.

ptmuldoon
07-25-2007, 12:26 PM
Ah, so you now show us the actual code. Is that it, or is there more? Shall we wait for the entire block of code before trying to see what's happening with it? (grr)

The code as you have posted will never break out of that loop if there are no inactive players in the range of 2 and $current_id - 1. It's bad coding to build loops that have no escape valve, which you have found out with an endless loop that causes the page to timeout.

Thats the bulk of the code, but you just just turned the lightbulb on in my head. I forgot that in a complete game, there is nobody listed as inactive, as there all classified as 'dead' instead.

I'll hopefully be able to work on it later tonight or tommorrow. Unfortunately, I have to 'real' work today.

EDIT: That was my issue. I added a condition for the loop to only run on current active games, and it worked fine.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum