...

View Full Version : Guessing Game Help



Phoenix216
11-03-2006, 11:14 PM
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;}

marek_mar
11-03-2006, 11:22 PM
Forum Rules (http://www.codingforums.com/rules.htm), section 1.5.

Phoenix216
11-03-2006, 11:27 PM
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.

marek_mar
11-04-2006, 12:47 AM
You mean actually use $low and $high in the random() function?
BTW that code will generate a _lot_ of notices.

Phoenix216
11-04-2006, 01:03 AM
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.

vinyl-junkie
11-04-2006, 11:26 PM
You mean a search like this (http://www.google.com/search?hl=en&lr=&q=%22number+guessing+game%22+script+php&btnG=Search)? 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.

Phoenix216
11-04-2006, 11:51 PM
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.

marek_mar
11-05-2006, 12:10 AM
<?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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum