= 2) { // Check if we're done rolling if ($_POST['die1'] != 'hold') { $_SESSION['d1'] = rand(1,6); } if ($_POST['die2'] != 'hold') { $_SESSION['d2'] = rand(1,6); } if ($_POST['die3'] != 'hold') { $_SESSION['d3'] = rand(1,6); } if ($_POST['die4'] != 'hold') { $_SESSION['d4'] = rand(1,6); } if ($_POST['die5'] != 'hold') { $_SESSION['d5'] = rand(1,6); } do_score(); } else { // Continue to roll if ($_POST['die1'] != 'hold') { $_SESSION['d1'] = rand(1,6); } else {$checked1 = "yes";} if ($_POST['die2'] != 'hold') { $_SESSION['d2'] = rand(1,6); } else {$checked2 = "yes";} if ($_POST['die3'] != 'hold') { $_SESSION['d3'] = rand(1,6); } else {$checked3 = "yes";} if ($_POST['die4'] != 'hold') { $_SESSION['d4'] = rand(1,6); } else {$checked4 = "yes";} if ($_POST['die5'] != 'hold') { $_SESSION['d5'] = rand(1,6); } else {$checked5 = "yes";} $_SESSION['rolls'] += 1; do_game($checked1, $checked2, $checked3, $checked4, $checked5); } } else if (eval_done()) { // Check if game is done do_done(); } else { // Interrupted game or accidental reload, just display last roll @do_game(); } } else { // Initialize state for new game session_destroy(); session_start(); init_state(); @do_game(); } ## Initialize state (dice and roll counter) function init_state() { $_SESSION['d1'] = rand(1,6); $_SESSION['d2'] = rand(1,6); $_SESSION['d3'] = rand(1,6); $_SESSION['d4'] = rand(1,6); $_SESSION['d5'] = rand(1,6); $_SESSION['errors'] = ""; $_SESSION['rolls'] = 1; } ## Print the game board function do_game($checked1, $checked2, $checked3, $checked4, $checked5) { $page_title = "Yahtzee!"; $page_css = "body { width: 680px; } table#game { float: left; width: 300px; margin: 15px; padding: 15px; text-align: center; border: 2px solid black; } table#score { width: 300px; margin: 15px; padding: 15px; text-align: center; border: 2px solid black; } #score td { padding-left: 1.5em; text-align: left; border: 1px solid black !important; }"; include('header.html'); ?>
Yahtzee!
DiceHold?
"; } ?>
"; } ?>
"; } ?>
"; } ?>
"; } ?>
UPPER SECTIONScore
Aces
Twos
Threes
Fours
Fives
Sixes
TOTAL SCORE
BONUS= 63) { $bonus = 35; echo $bonus; } ?>
TOTAL UPPER
LOWER SECTIONScore
3 of a kind
4 of a kind
Full House
Sm. Straight
Lg. Straight
YAHTZEE
Chance
TOTAL UPPER
TOTAL LOWER
GRAND TOTAL
Copyright © of Yahtzee! is held by the Milton Bradley Company, a wholly-owned subsidiary of Hasbro. | Dice images from Wikimedia Commons.

"; include('footer.html'); } ## Print the score assignment board function do_score() { $page_title = "Yahtzee!"; $page_css = "body { width: 680px; } table#game { float: left; width: 300px; margin: 15px; padding: 15px; text-align: center; border: 2px solid black; } table#score { width: 300px; margin: 15px; padding: 15px; text-align: center; border: 2px solid black; } #score td { padding-left: 1.5em; text-align: left; border: 1px solid black !important; }"; include('header.html'); ?>
Yahtzee!
Dice
UPPER SECTIONScore
Aces
Twos
Threes
Fours
Fives
Sixes
TOTAL SCORE
BONUS= 63) { $bonus = 35; echo $bonus; } else { echo " "; } ?>
TOTAL UPPER
LOWER SECTIONScore
3 of a kind
4 of a kind
Full House
Sm. Straight
Lg. Straight
YAHTZEE
Chance
TOTAL UPPER
TOTAL LOWER
GRAND TOTAL
Copyright © of Yahtzee! is held by the Milton Bradley Company, a wholly-owned subsidiary of Hasbro. | Dice images courtesy Wikimedia Commons.

"; include('footer.html'); } ## Print the done screen function do_done() { $page_title = "Yahtzee!"; $page_css = "body { width: 680px; } table#game { float: left; width: 300px; margin: 15px; padding: 15px; text-align: center; border: 2px solid black; } table#score { width: 300px; margin: 15px; padding: 15px; text-align: center; border: 2px solid black; } #score td { padding-left: 1.5em; text-align: left; border: 1px solid black !important; }"; include('header.html'); ?>
Yahtzee!
Dice
UPPER SECTIONScore
Aces
Twos
Threes
Fours
Fives
Sixes
TOTAL SCORE
BONUS= 63) { $bonus = 35; echo $bonus; } else { echo " "; } ?>
TOTAL UPPER
LOWER SECTIONScore
3 of a kind
4 of a kind
Full House
Sm. Straight
Lg. Straight
YAHTZEE
Chance
TOTAL UPPER
TOTAL LOWER
GRAND TOTAL
Copyright © of Yahtzee! is held by the Milton Bradley Company, a wholly-owned subsidiary of Hasbro. | Dice images courtesy Wikimedia Commons.

"; include('footer.html'); } ## Check if game is done function eval_done () { if (isset($_SESSION['ones'], $_SESSION['twos'], $_SESSION['threes'], $_SESSION['fours'], $_SESSION['fives'], $_SESSION['sixes'], $_SESSION['three_kind'], $_SESSION['four_kind'], $_SESSION['full_house'], $_SESSION['sm_straight'], $_SESSION['lg_straight'], $_SESSION['yahtzee'], $_SESSION['chance'])) { return true; } else { return false; } } ## Evaluate score and update session with a message or a score function eval_score() { $_SESSION['errors'] = ""; $dice = array (0=>$_SESSION['d1'], 1=>$_SESSION['d2'], 2=>$_SESSION['d3'], 3=>$_SESSION['d4'], 4=>$_SESSION['d5']); sort($dice); $tmp = array_unique($dice); sort($tmp); if (sizeof($tmp) == 1) { // Check if roll was a Yahtzee for Joker rules (bonus points) if ($_SESSION['yahtzee']) { $is_bonus = true; $is_yahtzee = true; } else { $is_yahtzee = true; } switch ($tmp[0]) { case '1': $upper_field = 'ones'; break; case '2': $upper_field = 'twos'; break; case '3': $upper_field = 'threes'; break; case '4': $upper_field = 'fours'; break; case '5': $upper_field = 'fives'; break; case '6': $upper_field = 'sixes'; break; } } else { $is_yahtzee = false; } switch ($_POST['score']) { // Evaluate dice for the 'score' case 'ones': if ($is_bonus && ($upper_field != $_POST['score'])) { $_SESSION['errors'] = "Must score " . $upper_field; } else { foreach ($dice as $d) { if ($d == 1) { $score += $d; } else { $score += 0; } } } $_SESSION['ones'] = $score; break; case 'twos': if ($is_bonus && ($upper_field != $_POST['score'])) { $_SESSION['errors'] = "Must score " . $upper_field; } else { foreach ($dice as $d) { if ($d == 2) { $score += $d; } else { $score += 0; } } } $_SESSION['twos'] = $score; break; case 'threes': if ($is_bonus && ($upper_field != $_POST['score'])) { $_SESSION['errors'] = "Must score " . $upper_field; } else { foreach ($dice as $d) { if ($d == 3) { $score += $d; } else { $score += 0; } } } $_SESSION['threes'] = $score; break; case 'fours': if ($is_bonus && ($upper_field != $_POST['score'])) { $_SESSION['errors'] = "Must score " . $upper_field; } else { foreach ($dice as $d) { if ($d == 4) { $score += $d; } else { $score += 0; } } } $_SESSION['fours'] = $score; break; case 'fives': if ($is_bonus && ($upper_field != $_POST['score'])) { $_SESSION['errors'] = "Must score " . $upper_field; } else { foreach ($dice as $d) { if ($d == 5) { $score += $d; } else { $score += 0; } } } $_SESSION['fives'] = $score; break; case 'sixes': if ($is_bonus && ($upper_field != $_POST['score'])) { $_SESSION['errors'] = "Must score " . $upper_field; } else { foreach ($dice as $d) { if ($d == 6) { $score += $d; } else { $score += 0; } } } $_SESSION['sixes'] = $score; break; case 'three_kind': if ($is_bonus && !$_SESSION[$upper_field]) { $_SESSION['errors'] = "Must score " . $upper_field; } else if (($dice[0] == $dice[1]) && ($dice[1] == $dice[2])) { foreach ($dice as $d) { $score += $d; } } else if (($dice[1] == $dice[2]) && ($dice[2] == $dice[3])) { foreach ($dice as $d) { $score += $d; } } else if (($dice[2] == $dice[3]) && ($dice[3] == $dice[4])) { foreach ($dice as $d) { $score += $d; } } else { $score = 0; } $_SESSION['three_kind'] = $score; break; case 'four_kind': if ($is_bonus && !$_SESSION[$upper_field]) { $_SESSION['errors'] = "Must score " . $upper_field; } else if (($dice[0] == $dice[1]) && ($dice[1] == $dice[2]) && ($dice[2] == $dice[3])) { foreach ($dice as $d) { $score += $d; } } else if (($dice[1] == $dice[2]) && ($dice[2] == $dice[3]) && ($dice[3] == $dice[4])) { foreach ($dice as $d) { $score += $d; } } else { $score = 0; } $_SESSION['four_kind'] = $score; break; case 'full_house': if ($is_bonus && !$_SESSION[$upper_field]) { $_SESSION['errors'] = "Must score " . $upper_field; } else if ($_SESSION[$upper_field]) { $score = 25; } else if ($dice[1] == $dice[3]) { // Prevent 5 of a kind from scoring $score = 0; } else if (($dice[0] == $dice[1]) && (($dice[2] == $dice[3]) && ($dice[3] == $dice[4]))) { $score = 25; } else if ((($dice[0] == $dice[1]) && ($dice[1] == $dice[2])) && ($dice[3] == $dice[4])) { $score = 25; } else { $score = 0; } $_SESSION['full_house'] = $score; break; case 'sm_straight': $tmp = array_unique($dice); sort($tmp); // Re-sort to get a uniform index if ($is_bonus && !$_SESSION[$upper_field]) { $_SESSION['errors'] = "Must score " . $upper_field; } else if ($_SESSION[$upper_field]) { $score = 30; } else if ($tmp[0] == 1 && $tmp[1] == 2 && $tmp[2] == 3 && $tmp[3] == 4) { $score = 30; } else if ($tmp[1] == 3 && $tmp[2] == 4 && $tmp[3] == 5 && $tmp[4] == 6) { $score = 30; } else if ($tmp[0] == 2 && $tmp[1] == 3 && $tmp[2] == 4 && $tmp[3] == 5) { $score = 30; } else if ($tmp[0] == 3 && $tmp[1] == 4 && $tmp[2] == 5 && $tmp[3] == 6) { $score = 30; } else { $score = 0; } $_SESSION['sm_straight'] = $score; break; case 'lg_straight': $tmp = array_unique($dice); if ($is_bonus && !$_SESSION[$upper_field]) { $_SESSION['errors'] = "Must score " . $upper_field; } else if ($_SESSION[$upper_field]) { $score = 40; } else if (($tmp[0] == $tmp[1] - 1) && ($tmp[1] == $tmp[2] - 1) && ($tmp[2] == $tmp[3] - 1)) { $score = 40; } else if (($tmp[1] == $tmp[2] - 1) && ($tmp[2] == $tmp[3] - 1) && ($tmp[3] == $tmp[4] - 1)) { $score = 40; } else { $score = 0; } $_SESSION['lg_straight'] = $score; break; case 'yahtzee': if ($is_yahtzee == true) { $score = 50; } else { $score = 0; } $_SESSION['yahtzee'] = $score; break; case 'chance': if ($is_bonus && !$_SESSION[$upper_field]) { $_SESSION['errors'] = "Must score " . $upper_field; } else if ($_SESSION['chance']) { $_SESSION['errors'] = "Scored already!"; } else { foreach ($dice as $d) { $score += $d; } } $_SESSION['chance'] = $score; break; } if ($_SESSION['errors'] == "") { // Add the Jokers rule bonus if applies if ($is_yahtzee = true) { if ($is_bonus == true) { $_SESSION['yahtzee'] += 100; } } } }