# Computer Guesses My Number 1-100

Taking php, I’ve been working and working on this code and I can’t get it to work.

I’m supposed to write a script that gets a number from a user, then it tries to guess the number, and should allow the user to tell the computer if the number it guessed was higher or lower than their number, or if it was the correct answer. The number should be from 1-100.

I’ve got an html page that submits the user input as well as a first computer guess of 50

[code]

Daniel Foley - CIS2515 Portal - Chapter 4

## Number Guessing Game!

You enter a random number right here, and I'll try to guess what it is!
(I won't look, I promise)

[/code]

The HTML page works fine. This is the PHP code I’ve written now. There are some extra echos in there so I can see where it’s going in the If structures and whatnot.

I want it to work like this:

1. Gets the number from user
2. Guesses 50, and asks the user if the users number is higher/lower or equal to the computer’s guess.
3a. If the guess was too low, it should take the guess / 2 and add that back to the guess ( 50 would become 50 + (50/2) = 75
3b. If the guess was too high, it should take the guess / 2 ( 50 would be (50/2) = 25.
3. Asks the user if the users number is higher/lower or equal to the computer’s guess, etc until success
4. Correct guess displays a message of success with the number of guesses it took to do that (i don’t even have a loop in there right now cause I’m just trying to get the logic to work properly)

[php]

Daniel Foley - CIS2515 Portal - Chapter 4

## Number Guessing Game!

<? \$userNum = \$_REQUEST["number"]; \$lastGuess = \$_REQUEST["lastGuess"]; \$LLastGuess; \$counter; function tooHigh(\$var1, \$var2) { if (\$var2 < \$var1) { echo "
Last guess: \$var2"; echo "
Current guess: \$var1"; \$var1 = ceil(((\$var1 - \$var2) + \$var1) - \$var1 / 2); echo "
too High 1"; } else { echo "
Last guess: \$var2"; echo "
Current guess: \$var1"; \$var1 = floor((\$var2 / \$var1)/2); echo "
too High 2"; } \$counter = \$counter++; return(\$var1); } function tooLow(\$var1, \$var2) { if (\$var1 > \$var2) { echo "
Last guess: \$var2"; echo "
Current guess: \$var1"; \$var1 = floor((\$var1 - (\$var2 - \$var1) / 2)); echo "
too Low 1"; } else { echo "
Last guess: \$var2"; echo "
Current guess: \$var1"; \$var1 = floor((\$var1 - \$var2)/2); echo "
too Low 2"; } \$counter = \$counter++; return(\$var1); } echo "
Just so you don't forget, your Number Was \$userNum"; echo "
Alright, I'm going to guess your number now."; echo "
Tell me if your number was lower or higher than my guess,"; echo "
or if I was correct.
"; echo ""; echo ""; echo ""; echo ""; \$answer = \$_REQUEST["answer"]; if (\$answer != "Correct") { if (\$answer == "Higher") { \$compNum = tooHigh(\$lastGuess, \$LLastGuess); echo "
Was your number \$compNum?"; \$lastGuess = \$LLastGuess; \$compNum = \$lastGuess; echo ""; } elseif (\$answer == "Lower") { \$compNum = tooLow(\$lastGuess, \$LLastGuess); echo "
Was your number \$compNum?"; \$lastGuess = \$LLastGuess; \$compNum = \$lastGuess; echo ""; } else { echo "
This is my first guess.
Was your number "; echo \$compNum = 50; \$compNum = \$lastGuess; \$lastGuess = \$LLastGuess; echo ""; } } else { echo "

# Haha! I win!

"; echo "
It only took me \$counter guesses."; } ?> [/php]

Since there is interaction with user, the form in your second file (randomNum.php) must store all the info what you need either in hidden form fields, or in \$_SESSION. On first iteration randomNum.php receives data from your first file (where user enter their number). To receive this number on second iteration, randomNum.php will need to store it is somewhere. Same with previous answer, if you need to display it to user. This will add few lines to randomNum.php in between tags:

[php]echo “”;
echo “<input type=‘hidden’ name=‘number’ value=’”.\$_REQUEST[‘number’]."’>";
echo “<input type=‘hidden’ name=‘lastGuess’ value=’”.\$_REQUEST[‘lastGuess’]."’>";
echo “<input type=‘hidden’ name=‘counter’ value=’”.\$_REQUEST[‘counter’]."’>";

echo “”;
echo “”;
echo “”;[/php]

And at the top of php code, where you declare variables you need to set \$counter based on previous iteration:
[php]\$counter=\$_REQUEST[‘counter’];[/php]