Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    while loop problem

    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.

    Code:
    $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;
    		}
    	
    	}

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    "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.

  • #3
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    ... 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.

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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.

    Code:
    $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;
    		}
    	}

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by Fumigator View Post
    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.
    Last edited by ptmuldoon; 07-26-2007 at 03:26 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •