Page 1 of 1

a nested loop ... confused! please help

Posted: Sun Jun 28, 2009 12:31 pm
by lebowskijeff
Hello,
I'm new to PHP, going through a textbook on my own and I can't get my head around a nested loop. Would anyone be willing to explain this to me? TIA sorry if this is wordy, I'm trying my best to explain what the book tells me and what I think I understand. also note that there most likely are better ways to achieve the goals outlined below, but this is one of the early chapters in the book and it's meant to be done this way--I'm just trying to wrap my head around this.

It's for a dice poker game (5 dice roll, roll once, then you choose what you want to keep, roll again, script evaluates for pairs, straights, etc.)

Below is just a part of the evaluate() function--its purpose is to examine the $die array--which holds the values of rolled dice (5 of them)--and see if the user has achieved patterns worthy of reward.

from the book:
the $numVals array tracks how many times each possible value appears. Analyzing the $numVals array is an easier way to track the various scoring combination than looking directly at the $die array (...) $numVals has six elements. $numVals[1] contains the number of ones the user rolled; $numVals[2] shows how many twos, and so on

I'm trying to understand the nested loop after the //count the dice comment. Does the outer loop repeat until it reaches 6, or does it break at some point--which wouldn't' make sense to me if it did--it has to cycle through all the values. Lets say we're checking for the dice value of 1, how does the script check the whole index of the $die array?

what is the 'if' statement doing? for example: on the first run through of the outer loop, we assign $theVal a 1, so $dieNum has a 0 (meaning we're looking at the first dice value from the $die array), if($die[0] == 1) then increment the first index of $theVal by one ... does the script then check the 2nd index of $die for dice value of one? then the third index, and so on? how does it continue to loop?

function evaluate(){
global $die, $cash;
//set up payoff
$payoff = 0;

//subract some money for this roll
$cash -= 2;

//count the dice
$numVals = array(6);
for ($theVal = 1; $theVal <= 6; $theVal++){
for ($dieNum = 0; $dieNum < 5; $dieNum++){
if($die[$dieNum] == $theVal){
$numVals[$theVal]++;
}//end if
}//end dieNum for loop
}//end theVal for loop


again, thank you. any help would be appreciated!

Re: a nested loop ... confused! please help

Posted: Sun Jun 28, 2009 2:29 pm
by Mark Baker

Code: Select all

 
//  Create an empty array, with keys for the six possible sides of a dice
    $numVals = array(6);
 
//  Loop through each of the six possible outcomes in turn
    for ($theVal = 1; $theVal <= 6; $theVal++){ 
//      Loop through each of the five dice in turn
        for ($dieNum = 0; $dieNum < 5; $dieNum++){ 
//          If the face value of that dice is the outcome (value) we're intereseted in for the 
//              theVal loop, the increment the appropriate theVal entry in the numVals array    
            if($die[$dieNum] == $theVal){ 
                $numVals[$theVal]++; 
            }//end if
        }//end dieNum for loop
    }//end theVal for loop
 
Far easier to do just with a single statement of

Code: Select all

 
$numVals = array_count_values($die);
 
except that the latter will only contain counts for values actually rolled, while the nested loop routine counts the $numVals array entries for all possible outcomes, even if the resultant count is 0 for a value