Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Location
    Lawrence, KS
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;}

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Forum Rules, section 1.5.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    New to the CF scene
    Join Date
    Nov 2006
    Location
    Lawrence, KS
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You mean actually use $low and $high in the random() function?
    BTW that code will generate a _lot_ of notices.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    New to the CF scene
    Join Date
    Nov 2006
    Location
    Lawrence, KS
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,073
    Thanks
    2
    Thanked 22 Times in 22 Posts
    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.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #7
    New to the CF scene
    Join Date
    Nov 2006
    Location
    Lawrence, KS
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.
    Last edited by marek_mar; 11-05-2006 at 12:17 AM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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