HiLo - A Number Guessing Game

The computer has invented a random number somewhere between 0 and 100

Your task is to guess the number using the least number of turns.


Let's PLAY!
The Mystery number lies between 100 and 0

<back


Code:

<body onload="document.takeaguess.guess.focus();">
<?php
if (isset($_POST['submit'])) { // has the "SUBMIT" button been pressed?
       // playing existing game - retrieve previous values
       $mystery    = $_POST['mystery'];
       $guess      = $_POST['guess'];
       $up         = $_POST['up'];
       $down       = $_POST['down'];
       $numguesses = $_POST['numguesses'];
   
       $numguesses++;  // consume a guess for this turn
   
       // check how well they did this turn
       if ($guess < $mystery) {
           //too low
           $feedback = "You guessed TOO LOW";
           $down = $guess;
       } else if ($guess > $mystery) {
           //too high
           $feedback = "You guessed TOO HIGH";
           $up = $guess;
       } else {
           //just right
           $feedback = "You guessed JUST RIGHT. It took " . $numguesses . " attempts.";
       }
   
   } else {
     // new game - set up values ready to play
       $mystery = rand(0,100);   // number to be guessed
       $up = 100;                // upper boundary of guess range
       $down = 0;                // lower boundary of guess range
       $guess = "";              // initial value of guess
       $numguesses = 0;          // tally for guessses taken
       $feedback = "Let's PLAY!";
   }
?>

<form name="takeaguess" method="post" action="part4.php">
<input name="guess" type="text" value="<?php echo $guess; ?>" />
<input name="mystery" type="hidden" value="<?php echo $mystery; ?>"  />
<input name="up" type="hidden" value="<?php echo $up; ?>"  />
<input name="down" type="hidden" value="<?php echo $down; ?>"  />
<input name="numguesses" type="hidden" value="<?php echo $numguesses; ?>"  />
<input name="submit" type="submit" value="Take A Guess" />
</form>
<?php
   echo "<br>" . $feedback;
   if ($guess!=$mystery) 
   echo "<br>The Mystery number lies between ". $up . " and ". $down . "<br>";
?>
<form name="newgame" method="post" action="part4.php">
<input type="submit" name="newgame" value="Start Over" />
</form>