Guessing Game

This page is the home of the wonderful guessing game. How exciting!

This page is the home of the exciting number guessing game, where I pick a number and you try to figure out what it is. Would you like to play?

END; } else { $resend = 1; if($_POST['initial'] === "1") { // First load. Choose the number. $randno = rand(1,MAX_NUM); $guesscount = 0; $comment = ''; echo "

I'm thinking of a number between 1 and ", MAX_NUM, ". Can you guess it?

"; } else { // Not first load. // Pull the information from the post and unmask $randno = ($_POST['randno'] ^ RAND_MASK)/RAND_MULT; $guesscount = $_POST['guesscount']; $guess = $_POST['guess']; // Make sure the data hasn't been messed with. if($_POST['check'] != sha1($randno . $guesscount . SHA1EXTRA)) { $start_form = 1; echo "

Invalid Post. Start over?

"; } elseif($randno == $guess) { $start_form = 1; echo "

Congratulations! Your guess of $guess ", "is correct! Would you like to play again?

"; } else { if($guess < $randno - MAX_NUM / 2) { $comm = "way too small"; $comcl = "wts"; } elseif($guess < $randno) { $comm = "too small"; $comcl = "ts"; } elseif($guess > $randno + MAX_NUM / 2) { $comm = "way too large"; $comcl = "wtl"; } else { // Already checked if it matches exactly. $comm = "too large"; $comcl = "tl"; } echo "

No, your guess of $guess is $comm. "; // See if the game will continue. if($guesscount < GUESS_LIMIT) { echo "Care to try again? You have ", GUESS_LIMIT-$guesscount, " guesses left.

"; } else { echo "

I'm sorry, you have used all ", "of your ", GUESS_LIMIT, " guesses. The number ", "I was thinking of was $randno. Would you like ", "to try again?

"; $start_form = 1; } } } } // Game continues. Generate the correct form. $me = $_SERVER['PHP_SELF']; if($start_form) { echo << END; } else { ++$guesscount; // Hide the target random number, and make faking the page // difficult. $check = sha1($randno . $guesscount . SHA1EXTRA); $randno = ($randno * RAND_MULT) ^ RAND_MASK; echo << END; } ?>