# Trouble with I'm Thinking of a Number game

I am supposed to write “I’m thinking of a number” game. The computer will generate a random number and let the user guess it’s value. Tell the user if guess is too high, too low, or correct. When guess is correct, tell how many turns it took. Here is my code so far. I can’t figure out how to make rand run once til the number is guessed correctly.

``````
<?php session_start() ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<title>
Thinking of a Number
</title>

<body>

<h1>I'm Thinking of a Number 1-100</h1>

<?php

if (isset(\$_SESSION["counter"])){
\$counter = \$_SESSION["counter"];
} else {
\$counter = 0;
} // end if
\$counter++;

//store new data in counter
\$_SESSION["counter"] = \$counter;

\$guess = filter_input(INPUT_POST, "guess");
\$rand = rand(1,100);
//I have a problem with rand, there only has to be one random number until the user guesses it.

if (filter_has_var(INPUT_POST, "guess"))
{
print "The number you input is \$guess <br />";

if (\$guess == \$rand)
{
print "You are correct <br />";
print "You guessed it in \$counter attempt(s).";
}
else if (\$guess != \$rand)
{

if(\$guess > \$rand)
{
print "You are too high. <br />";
print "Try again";

}
else if (\$guess < \$rand)
{
print "You are too low. <br />";
print "Try again";
}
}
}
else
{
print <<<HERE

<form action = "" method = "post">
<fieldset>
<label>Enter a number: </label>
<input type = "text" name = "guess" />
<br />
<button type = "submit">Submit</button>
</fieldset>
</form>

HERE;
}

?>
</body>
</html>

``````

Thank you.

Here you go:

``````<?php

session_start();

if(!isset(\$_SESSION['the_number']))
{
\$_SESSION['the_number'] = rand(1, 100);
}

if(!isset(\$_SESSION['counter']))
{
\$_SESSION['counter'] = 0;
}
else
{
\$_SESSION['counter']++;
}

\$rand = \$_SESSION['the_number'];
\$counter = \$_SESSION['counter'];
\$guess = isset(\$_POST['guess']) ? (int) \$_POST['guess'] : false;

if(\$guess == \$rand)
{
// If the user guessed the number, unset counter & rand number
// This way, on the next refresh, the game will restart
unset(\$_SESSION['the_number']);
unset(\$_SESSION['counter']);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<title>Thinking of a Number</title>

<body>

<h1>I'm Thinking of a Number 1-100</h1>

<?php

if (\$guess != false)
{
print "<hr />";
print "The number you input is \$guess <br />";

if (\$guess == \$rand)
{
print "You are correct <br />";
print "You guessed it in ".\$counter." attempt(s).";
}
else if (\$guess != \$rand)
{
if(\$guess > \$rand)
{
print "You are too high. <br />";
print "Try again";
}
else if (\$guess < \$rand)
{
print "You are too low. <br />";
print "Try again";
}
}
}

?>

<hr />

<?php if(\$guess != \$rand): ?>
<form action = "" method = "post">
<fieldset>
<label>Enter a number: </label>
<input type = "text" name = "guess" /><br />
<button type = "submit">Submit</button>
</fieldset>
</form>
<?php else: ?>
<a href="random-number-game.php">Press Here to Restart</a>
<?php endif; ?>

<!--

The Random Number: <?php echo \$rand; ?>

The Counter: <?php echo \$counter; ?>

The Guess: <?php echo htmlspecialchars(\$guess); ?>

-->

</body>
</html>
``````

The issue was that you were setting \$rand on every page load, so the number would change every time. The random number has to be set in to the session like the counter.

Thank you for taking the time to help. I’m reading your codes and realized my codes were out of order and I have a lot to learn. Thank you very much. Perfect!

If this time it’s the computer’s turn to guess a number, what do I need to change in the codes?

Generate a new random number and set the session variable storing the computer’s random number to the new random number.

This time the user generates a random integer between 1 and 100, the computer guesses the number. Let the user pick from “too high”, “too low”, or “correct” each turn. Your algorithm should be able to guess in seven turns or fewer.

Then you need to store in session variables the range of possible guesses for the computer.

Initially the range will be between 1 and 100. Then say the the computer guesses 50 (by generating a random numer between current range of possibilities). If the guess is too high then the computer’s next guess will be a random number between 1 and 49. If the guess is too low then the next guess will be a random number between 51 and 100.

After each computer “guess” you adjust the range of possible guesses for the next guess based on whether the current guess is too high or too low as above until the computer gets the correct number.

Thank you.

Just a minor correction to webdev1958’s solution:

In order for the computer to “guess” the number in 7 tries or less every time, it MUST guess the median (middle) number every time based on an adjusted minimum and maximum range. The computer should never generate a random number, or it will on occasion take more than 7 guesses. Initialize a session minimum of 0 (not 1, I’ll explain) and maximum of 100, and make the program “guess” 50 first every time a new session starts.

If 50 is higher than the number submitted, then set the session max to 50 and calculate the median between 0 & 50 (25). The reason you use 0 for the MIN is that if the MIN is 1 and you submit 1 as your number to be guessed, the program will never get to 1 using the median formula. The math to find the median is ((MAX - MIN)/2) + MIN. You also need to use the round() function on the formula to avoid fractions.

If the number generated is ever lower than the number submitted, then just set the MIN value to the last calculated number. You’re resetting the MAX (if too high) or the MIN (too low) every time a new number is calculated and getting the median of the new MAX & MIN until the program narrows down to your submitted number.

This way it will never take more than 7 tries.

nup, it works for me without having to guess the median number.

Uhm… no?
Worst case Scenario Time!
Number: 1.
Guess 1: 99 (Lower)
Guess 2: 98 (Lower)
Guess 3: 97 (Lower)

Guess 99: 1 (Correct!)

Can you see where this is going? The number MUST be the median in order to reduce at a predictable rate (1/2 the previous range) and guarantee that the answer is reached within 7 guesses.
(Side note: 7 in this case is related to 100 in the form: “Given that n[i] = floor(n[i-1] / 2), and n[0] = 100, the value of i such that n[i] = 1 is 7.” In this mathematical way, you can describe any number as being guessable in X tries. 500? guessable in 9. 5000? Guessable in 13.)

Thank you StarLion. I should’ve explained why you had to use the median value the way you did. You made my point much more eloquently and simply.

I’m very new to PHP and had the exact same assignment for a class. I made it work properly only after forcing the program to use the median number. Using random number generation will inevitably result in more than 7 guesses.