david212 Posted April 29, 2010 Share Posted April 29, 2010 Hello friends. I have one hometask to solve. Here it is: <? $vargame = 0; $colorgame = "red"; $gametrick = 0; while ($vargame <= 10) { if ($gametrick > 100) { echo "Abort!"; break; } else if ($vargame < 4 && $gametrick < 2) { $vargame += 3; $colorgame = "green"; } else if ($vargame == 8 AND $colorgame == "red") { $vargame--; $colorgame = "yellow"; } else if ($colorgame == "yellow" XOR $vargame >= 7) { $vargame++; $colorgame = "blue"; } else { $vargame += 2; $colorgame = "red"; } $gametrick++; } ?> and the question is Which color "wins" the game? So in this line: else if ($vargame < 4 && $gametrick < 2) { $vargame += 3; $colorgame = "green"; } the value of $vargame is 0 and it is less than 4. the $gametrick also is less than 2 and its value is 0; The line $vargame += 3; means that the value of $vargame will be 3. In this line else if ($vargame == 8 AND $colorgame == "red") { $vargame--; $colorgame = "yellow"; } the value of $vargame is 3 and it wont be 8 and $colorgame value is now green and it won't be red else if ($colorgame == "yellow" XOR $vargame >= 7) { $vargame++; $colorgame = "blue"; } as i know XOR is used when one of the value is true not both of them and here $colorgame is not yellow it is green and $vargame >= 7 is not more than 7 it is 3. So imy question is which color won the game? I think green color won the game. How can i calculate how many loops are in the code? Could any one help me to understand better the code? Thank you Link to comment https://forums.phpfreaks.com/topic/200152-help-with-color-based-task/ Share on other sites More sharing options...
cags Posted April 29, 2010 Share Posted April 29, 2010 Just follow it through in your head if it helps the write down what happens. This may not be perfect as I rushed through, but it should give you an idea. start loop ($vargame = 0, $gametrick = 0, $colorgame = "red") $vargame += 3 (becomes 3) $colorgame = "green" $gametrick++ (becomes 1) start loop ($vargame = 3, $gametrick = 1, $colorgame = "green") $vargame += 3 (becomes 6) $colorgame = "green" $gametrick++ (becomes 2) start loop ($vargame = 6, $gametrick = 2, $colorgame = "green") $vargame += 2 (becomes $colorgame = "red" $gametrick ++ (becomes 3) start loop ($vargame = 8, $gametrick = 3, $colorgame = "red") $vargame--(becomes 7) $colorgame = "yellow" $gametrick++ (becomes 4) start loop ($vargame = 7, $gametrick = 4, $colorgame = "yellow") $vargame += 2 (becomes 9); $colorgame = "red"; $gametrick++ (becomes 5) start loop ($vargame = 9, $gametrick = 6, $colorgame = "red") $vargame += 2 (becomes 11) $colorgame = "red"; $gametrick++(becomes 6) end loop Link to comment https://forums.phpfreaks.com/topic/200152-help-with-color-based-task/#findComment-1050462 Share on other sites More sharing options...
ignace Posted April 29, 2010 Share Posted April 29, 2010 I got this: 0: $vargame = 0, $colorgame = "red", $gametrick = 0; 1: $vargame = 3, $colorgame = "green", $gametrick = 1;//elseif ($vargame < 4 && $vargame < 2) 2: $vargame = 6, $colorgame = "green", $gametrick = 2;//elseif ($vargame < 4 .. 3: $vargame = 8, $colorgame = "red", $gametrick = 3;//else 4: $vargame = 7, $colorgame = "yellow", $gametrick = 4;// elseif ($vargame == 8 .. 5: $vargame = 9, $colorgame = "red", $gametrick = 5;// else 6: $vargame = 10, $colorgame = "blue", $gametrick = 6;// elseif ($colorgame == "yellow" ... 7: end The text in comments is what applied. Link to comment https://forums.phpfreaks.com/topic/200152-help-with-color-based-task/#findComment-1050474 Share on other sites More sharing options...
cags Posted April 29, 2010 Share Posted April 29, 2010 Quite right, should have gone into the elseif($colorgame ... not sure why I went into the else, that's what you get for rushing, there's also some copy-paste/typos in mine. Link to comment https://forums.phpfreaks.com/topic/200152-help-with-color-based-task/#findComment-1050492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.