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.

You will be asked your name when you successfully guess the number and that will be saved in our HiScores table


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

<back


Code:

<?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 if (isset($_POST['savename'])) {
           //score needs to be written to table
           $mystery = "";
           $guess = "";
           $numguesses = $_POST['numguesses'];
           $name=$_POST['name'];
           $date= date('Y-m-d');
 
           mysql_connect('localhost','SNumber','password');
           mysql_select_db('Snumber');
           mysql_query('insert into hiscores values ("'.$name.'","'.$numguesses.'","'.$date.'")');
           $feedback = "data saved"; 
   
   } 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="part5.php">
<?php
if ($guess != $mystery) {
   //still playing, so display in-game controls
   ?>
<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" />
<?php
} else {
   // must have won game, so do hiscores table here instead.
   //connect
   mysql_connect('localhost','Snumber','password');
   mysql_select_db('Snumber');
   
   //get existing data
   $result = mysql_query('select * from hiscores order by score asc'); 
   //display with in-table data insertion point

   print("<table>
            <tr><th>Rank</th><th>Name</th><th>Score</th><th>Date</th></tr>");
   $i = 0;
   $printed = 0;
   while ($row = mysql_fetch_array($result))  {
       //grabs rows with names column headings
       $i++;
       if (($row['score'] >= $numguesses)&&($printed==0)) {
           // locate where YOUR name will be added on the list
           printf('<tr><td>%s</td><td><input type="text" name="name" value="type your name here"></td>
                       <td>%s</td><td>%s</td></tr>',$i,$numguesses,date('Y-m-d'));
           $printed = 1;
       }
       //output the other rows
       printf('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
               $i,$row['name'],$row['score'],$row['date']);
   }
   
   print('</table>');
   printf('<input name="numguesses" type="hidden" value="%s" >',$numguesses);
   print('<input type="submit" name="savename" value="Save Your Score">');
   
}
?>
</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="part5.php">
<input type="submit" name="newgame" value="Start Over" />
</form>