\n"; echo " \n"; echo " \n"; echo " $page_title\n"; echo " \n"; echo " \n"; echo "

$page_title

\n"; echo "

$page_msg\n"; echo " \n"; echo "\n"; } // Makes sure the user passes back a $move_str string with only // basic numbers and spaces that are exactly limited to "1", "2", // "3", and "4". function is_moves_str_valid($moves_str) { $rv = 1; $moves = split(" ", $moves_str); $i_max = count($moves); for( $i = 0 ; $i < $i_max ; ++$i) { if ($moves[$i] == "0") { continue; } if ($moves[$i] == "1") { continue; } if ($moves[$i] == "2") { continue; } if ($moves[$i] == "3") { continue; } if ($moves[$i] == "4") { continue; } $rv = 0; break; } return $rv; } function send_images($moves_str) { // Show the chocolate images and count (if it is not commented out below). $moves = split(" ", $moves_str); $i_max = count($moves); $img_count = 21; for( $i = 0 ; $i < $i_max ; ++$i ) { $img_count -= $moves[$i]; for( $j = 0 ; $j < $img_count ; ++$j ) { echo " \n"; } // FOR NOW, do not show the user the count. //echo " [$img_count]\n"; echo "
\n"; } echo "
\n"; } function send_page($new_count, $moves_str) { send_images($moves_str); // Show the form. echo "

\n"; echo " \n"; echo " \n"; echo " Take 1\n"; echo " \n"; echo "
\n"; if( $new_count >= 2 ) { echo " Take 2\n"; echo " \n"; echo "
\n"; } if( $new_count >= 3 ) { echo " Take 3\n"; echo " \n"; echo "
\n"; } if( $new_count >= 4 ) { echo " Take 4\n"; echo " \n"; echo "
\n"; } echo "
\n"; echo " \n"; echo "
\n"; echo "
\n"; echo " Rules\n"; } function calc_my_take($new_count) { switch( $new_count ) { // We win. Just take the remaining pieces. case 1: $rv = 1; break; case 2: $rv = 2; break; case 3: $rv = 3; break; case 4: $rv = 4; break; // We lose if the opponent plays perfectly. case 5: $rv = rand(1,4); break; // Force Opponent to 5. case 6: $rv = 1; break; case 7: $rv = 2; break; case 8: $rv = 3; break; case 9: $rv = 4; break; // We lose if the opponent plays perfectly. case 10: $rv = rand(1,4); break; // Force Opponent to 10. case 11: $rv = 1; break; case 12: $rv = 2; break; case 13: $rv = 3; break; case 14: $rv = 4; break; // We lose if the opponent plays perfectly. case 15: $rv = rand(1,4); break; // Force Opponent to 15. case 16: $rv = 1; break; case 17: $rv = 2; break; case 18: $rv = 3; break; case 19: $rv = 4; break; // We lose if the opponent plays perfectly. case 20: $rv = rand(1,4); break; // Force Opponent to 20. case 21: $rv = 1; break; // Anything else is an internal error. default: echo_error_page("Internal Error", "Unexpected value for \$new_count in calc_my_take()"); exit; } return $rv; } function challenge_to_new_game($msg, $moves_str) { echo " $msg
"; echo "
\n"; send_images($moves_str); echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "
\n"; } function get_my_take($new_count) { $rv = 0; if( $new_count == 21 ) { // New Game. Originally, I flipped for it, but it is more // difficult for users to figure out the game if they do not see // how the computer plays perfectly. //$heads = rand(0, 1); $heads = 1; if( $heads ) { // The user moves first. $rv = 0; echo " New Game: Your move.

\n"; } else { // I move first. $rv = calc_my_take($new_count); echo " New Game: I took $rv.

\n"; } } else { // Continuation of current game. $rv = calc_my_take($new_count); if( $rv > 0 ) { echo " Continue Game: I took $rv.

\n"; } } return $rv; } // // Script starts here. // // Initialize variables. $count = $_POST["count"]; if( $count == "" ) { $count = 21; } $users_take = $_POST["users_take"]; if( $users_take == "") { $users_take = 0; } $moves_str = $_POST["moves_str"]; if( $moves_str == "" ) { $moves_str = "0"; } // Validate input. if( $count > 21 ) { echo_error_page("Input Error", "The value for count is out of range."); exit; } if( ($users_take < 0) || ($users_take > 4) ) { echo_error_page("Input Error", "The value for users_take is out of range."); exit; } if( ($count != 21) && ($users_take == 0)) { // User is trying to make a move without taking a chocolate piece. echo_error_page("Input Error", "You must take a piece on each move."); exit; } if( !is_moves_str_valid($moves_str) ) { echo_error_page("Input Error", "Illegal value for moves_str."); exit; } // Send header. $page_title = "Chocolate Pieces"; echo "\n"; echo " \n"; echo " \n"; echo " $page_title\n"; echo " \n"; // Send body. echo " \n"; echo "

$page_title

\n"; $new_count = $count - $users_take; if( $new_count == 0 ) { challenge_to_new_game("You won.", $moves_str); exit; } if( $users_take > 0 ) { $moves_str = "$moves_str $users_take"; } $my_take = get_my_take($new_count); $new_count = $new_count - $my_take; if( $new_count == 0 ) { challenge_to_new_game("You lost.", $moves_str); exit; } if( $my_take > 0 ) { $moves_str = "$moves_str $my_take"; } send_page($new_count, $moves_str); echo " \n"; echo "\n"; ?>