Jump to content

Help with color based task


david212

Recommended Posts

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
Share on other sites

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 8)

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.