'guessing3']); ?> Guessing Game

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 { $_SESSION['started'] = 1; if(isset($_POST['guess'])) { $_SESSION['guess'] = $_POST['guess']; ++$_SESSION['guesscount']; } // See if we pressed the new game button. if(isset($_POST['newgame']) || !isset($_SESSION['guess'])) { // First load. Choose the number. $_SESSION['randno'] = rand(1,MAX_NUM); $_SESSION['guesscount'] = 0; unset($_SESSION['guess']); $comment = ''; echo "

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

"; } else { // Not first load. Need to evaluate. $randno = $_SESSION['randno']; $guess = $_SESSION['guess']; $guesscount = $_SESSION['guesscount']; if($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 { echo << END; } //echo "
"; //echo "Debug: Session: "; print_r($_SESSION); //echo "
Post: "; print_r($_POST); //echo "
"; ?>