Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with my program.

Started by zero_hour Jul 12, 2006 at 6:01 PM 3 replies 700+ views
Original Post
zero_hour
zero_hour
My book wanted me to write a piece of code that would have the player pick a number, and then have the computer try to guess it. I did so successfully, but i cant get the computer to recognize its previous guesses. Could someone help me to solve the problem?
// Guess MY NUmber

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
    cout << "\tWelcome to Guess MY NUmber 2.0\n";
    cout << "\tIn this version, the computer has to guess YOUR Number\n\n";
    char yesNo;
    int theNumber;
    
    do
    {
      cout << "Please Input a number between (1 - 100): ";
      cin >> theNumber;
      
      if((theNumber < 1) || (theNumber > 100))
          {
                     cout << "Between 1 and 100 please\n\n";
                     continue;
          }
      
      cout << "\nIs " << theNumber << " the number you really want to choose? (y/n) : ";
      cin >> yesNo;
      }while (yesNo != 'y');
      
      srand(time(0));
      int highGuess = 100;
      int lowGuess = 1;
      int tries = 0;
      int guess;
      
      do
      {
      guess = ((rand() % highGuess) + lowGuess);
      ++tries;
      cout << guess << " ";
      
      if (guess > theNumber)
      {
                cout << "Too High\n";
                
                if (guess < highGuess)
                   { highGuess = guess;
                     continue;
                   }
                
                else
                    continue;
      }
      
      if (guess < theNumber)
      {
                cout << "Too Low\n";
                
                if (guess > lowGuess)
                {
                          lowGuess = guess;
                          continue;
                }
                
                else
                    continue;
      }
      }while (guess != theNumber);
      
cout << "\nThe Computer Got the number in " << tries << " tries";
std::cin.ignore(std::cin.rdbuf()->in_avail() +3);
return 0;

Driv3MeFar
Driv3MeFar
Quote:
Original post by zero_hour
I did so successfully, but i cant get the computer to recognize its previous guesses.


What, exactly, do you mean by that?

Also, for a simpler (non-random) solution to this problem, you can always have the computer pick a number half way between the high and low guess, and thus always cut the possible solutions in half with each guess.
zero_hour
zero_hour
Quote:
Original post by Driv3MeFar
Quote:
Original post by zero_hour
I did so successfully, but i cant get the computer to recognize its previous guesses.


What, exactly, do you mean by that?

Also, for a simpler (non-random) solution to this problem, you can always have the computer pick a number half way between the high and low guess, and thus always cut the possible solutions in half with each guess.



Well one of the easy characteristics of a human, is that he is aware of his mistakes.
I have the computer start by guessing a number between 1 and 100. Then what i want him to do, is if he is aware that say 69 is above the real number, to guess between 1 and 69, so i have 69 replace the high guess, which was originally 100, but for some reason, the computer continues to guess higher then 69 despite the limitation I (tried) to set.
Driv3MeFar
Driv3MeFar
the problem lies here:

guess = ((rand() % highGuess) + lowGuess);

Take, for example, a high guess of 60 and a low guess of 20.
rand() % 60 will yeild results from 0-59. To this you add 20, yielding results from 20-79.

What you want is:

guess = ( ( rand() % (highGuess-lowGuess) ) + lowGuess );
Edit: To clarify, take the same example with the new code.
highGuess - lowGuess = 40, so your modulo will return results ranging from 0 to 39. Add 20 to that, and you get 20-59, which is what you want.
zero_hour
zero_hour
Thanks for all your help, The program works better, it does occasionally guess the same number, but that's probably because of the math, again thanks for your help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.