HiLo - A Number Guessing GameThe 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 |
<?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="part3.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>"; ?>