Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. Guessing Game Help

I am supposed to make a guessing game for my programming class. I got the first part which was to have the computer select a random number and then have the computer guess the number outputting each number guessed and if it was too high or too low. The second part is to reset the lower range to drone's guess + 1 and the upper range to drone's guess - 1 and then output the updated range.
I'm afraid I'm completely lost and since this is an online course it's hard to ask the professor for help. Could any of you look at my code and steer me in the right direction? I'd really appreciate any hints.

print "***** Version - 2 Game *****<br>";
random(10,25,&\$myNumber);
print "I will pick a number between 10 and 25 - how long will the drone take to guess?<br>";
print "My number is \$myNumber.<br>";
\$keepGuessing = start;
\$attempt = 0;

while (\$keepGuessing==start)
{

random(10,25,&\$droneGuess);
print "Drone selects \$droneGuess<br>";
if(\$droneGuess == \$myNumber )
{
\$keepGuessing =stop;

}
elseif(\$droneGuess<\$myNumber)
{
\$keepGuessing = start;
\$low=\$num-1;
\$high=\$high;
print "Your guess is too low<br>";
print "....new range is \$low.... \$high<br>";
}
else
{
\$keepGuessing = start;
\$high=\$num+1;
\$low=\$low;
print "Your guess is too high<br>";
print "....new range is \$low.... \$high<br>";
}
\$attempt= \$attempt + 1;
}
print "******You needed \$attempt tries to guess my number******<br>";
return;

function random(\$low,\$high,\$num){\$range = \$high - \$low + 1;\$num = rand() % \$range + \$low;}

• Forum Rules, section 1.5.

• I read the rules. I'm not asking for anyone to do my homework for me. I just need hints on how to reset the range.

• You mean actually use \$low and \$high in the random() function?
BTW that code will generate a _lot_ of notices.

• I'm supposed to change the range to narrow the numbers the computer can pick and keep it from picking the same number more than once. I've been searching for more than a week to find a tutorial or something but I can't even figure out the right search terms to get the answers I need.

• You mean a search like this? The search keywords weren't particularly hard to come up with. The first search result on that page seems to have what you're looking for.

• I have done a search like that. Those are all games with user input. I'm making a game where the computer guesses the number. I'm trying to learn how to reset the random range after every loop. The objective is to narrow the range, reducing the number of times the computer guesses and eliminating any repeat guesses.

• PHP Code:
``` <?php \$min = 1; \$max = 100; function guess(\$min, \$max) {     return mt_rand(\$min, \$max); } print 'Computer picks a random number from ' . \$min . ' to ' . \$max . ".\n<br/>"; \$number = guess(\$min, \$max); print 'Drone will try to guess the number picked by the computer. ' . "\n<br/>"; \$attempts = 0; do  {     \$guess = guess(\$min, \$max);     print 'Drone guesses ' . \$guess . ' from the range ' . \$min . ' to ' . \$max . ".\n<br/>";     if(\$number < \$guess)     {         print 'Guess was too high.' . "\n<br/>";         \$max = \$guess - 1;     }     else if(\$number > \$guess)     {         print 'Guess was too low.' . "\n<br/>";         \$min = \$guess + 1;     }     ++\$attempts; } while(\$guess != \$number); print 'Drone guessed the number ' . \$number . ' correctly! Drone needed ' . \$attempts . ' attempts.' . "\n<br/>"; ?> ```
First two lines of my sig.

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•