Click the red chip to play."; } /* this function creates the random numbers to be used as values for the dice. it also assigns a point value to the $point variable only the first time it is run. there is also a switch statement helps to create the message to be displayed for the user in the browser. */ function rollDice() { global $firstDie; global $secondDie; global $sumDice; global $point; global $status; global $message; $firstDie = rand(1,6); $secondDie = rand(1,6); $sumDice = $firstDie + $secondDie; $status += 1; if ($status == 1) $point = $sumDice; /* i used a switch statement during the first roll of the dice, to assign game status to the $status variable */ if($status == 1) { switch($sumDice) { case 2: $messageControl = "immediateLoss"; break; case 3: $messageControl = "immediateLoss"; break; case 4: $messageControl = "point"; break; case 5: $messageControl = "point"; break; case 6: $messageControl = "point"; break; case 7: $point = ""; #ensures that if you roll an 11, there is no point value $messageControl = "immediateWin"; break; case 8: $messageControl = "point"; break; case 9: $messageControl = "point"; break; case 10: $messageControl = "point"; break; case 11: $point = ""; #ensures that if you roll an 11, there is no point value $messageControl = "immediateWin"; break; case 12: $messageControl = "immediateLoss"; break; default: $messageControl = "systemError"; break; } /* a conditional test to find out when the user has won. */ } else if ($status > 1) { if ($sumDice == 7) { $messageControl = "delayedLoss"; } else if ($sumDice == $point){ $messageControl = "delayedWin"; } else { $messageControl = "point"; } } /* stores a message to display after the game is over */ $playAgain = " Click the red chip to play another round."; /* a switch statement seemed like the best way to handle the creation of game status feedback for the user */ switch ($messageControl) { case "immediateLoss": $message = "You rolled a $firstDie and a $secondDie. Unfortunately, because they add up to $sumDice, you lost." . $playAgain; $status = 0; break; case "immediateWin": $message = "You rolled a $firstDie and a $secondDie. Congratulations! The sum was $sumDice, so you won." . $playAgain; $status = 0; break; case "point": $message = "You rolled a $firstDie and a $secondDie, which add up to $sumDice. Try to roll your dice so they create a sum of $point, to match your point."; break; case "delayedLoss": $message = "Rolling a 7 while trying to match your point means you lose." . $playAgain; $status = 0; break; case "delayedWin": $message = "You rolled a $firstDie and a $secondDie. Congratulations! You won by matching your point ($point)." . $playAgain; $status = 0; break; case "systemError": $message = "The server is experiencing difficulty. Please refresh the page or try again in a few minutes."; break; default: $message = "Welcome to PHP Craps! Click the red chip to play."; break; } } ?>

Welcome to PHP Craps

Die 1 Die 2 Sum Point

The Rules:

If you roll a 7 or 11 on the first roll, you win. If you roll a 2, 3, or 12 on the first roll, you lose. If you roll anything else (4, 5, 6, 8, 9, 10), this is called your "point". You must keep rolling until your point comes up again. If you roll a 7 while trying to roll your point, you lose. Click the red chip to begin.