A "guess my number" game, with simple AI

This is a discussion on A "guess my number" game, with simple AI within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a simple program where the user enters a number and the computer tries to guess ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    A "guess my number" game, with simple AI

    I am trying to write a simple program where the user enters a number and the computer tries to guess it. I wanted to make it a bit smarter, so that the program knows if its guess it higher or lower than what the users number are, and make the next guess with using that information

    But it dont work.

    Could someone please help me out a bit?

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main() 
    {
        int myRandNo;
        int compNo = 50;
        int randNo = 0;
        
        cout << "Welcome to the guessing game" << endl;
        cout << "Please enter a number between 1 and 100: ";
        cin >> myRandNo;
        
        while (compNo != myRandNo)
        {
              //seed the rand
              srand(time(0));
              
              //guess was to low
              if (compNo < myRandNo)
              {
                  randNo = rand();
                  compNo = (randNo % 100) + currentGuess;
                  //cout << compNo << "CompNo is smallest" << endl;
              }
              
              //guess was to high
               if (compNo > myRandNo)
              {
                  randNo = rand();
                  compNo = (randNo % currentGuess) + 1;
                  //cout <<compNo  << "CompNo is biggest" << endl;
              }
              
        //cout <<"wrong" << endl;
                 
        }
        
        cout <<"right";
         
        system("pause");
        return 0;
    }
    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But it dont work.
    Okay...how? As much as I enjoy troubleshooting random programs posted on the internet, you'll need to be more specific.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Well, in most cases it just never finds the number. And for some reason it uses very high numbers for the guessing sometimes, like 22000 and so on.

    Also it seams like it almost never evaluates to "//guess was to high"

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    That's an interesting solution, but it won't compile as posted. Can you post the code that you're actually using?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I had one variable which I did not think I was using, so I removed it from the program I pasted here. The error was in that variable, as I should not really have been using it.

    Sorry for taking out your time.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >o I removed it from the program I pasted here
    Well, that explains your large numbers. If you remove currentGuess from
    Code:
    compNo = (randNo % currentGuess) + 1;
    You get the straight result of rand plus one. Ignoring that this could potentially overflow, it's extremely likely that compNo will be much greater than 100.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Well, I see that I did not really solve my problem. It now longer trys to use the bigg numbers, and it always finds the number it is supposted to in the end. Some times it is realy fast, but other times it has to guess a rediculus amount of times.


    I think this is what is cousing the problem, but I dont know why
    Code:
    {
                      randNo = rand();
                      compNo = (randNo % compNo) + 1;
                      //cout <<compNo  << "CompNo is biggest" << endl;
                  }
    The whole "program"
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main() 
    {
        int myRandNo;
        int compNo = 50;
        int randNo = 0;
        int tryes = 0;
        char gameRep = 'y';
        
        cout << "Welcome to the guessing game" << endl;
        cout << "Please enter a number between 1 and 100: ";
        cin >> myRandNo;
        
        while (gameRep =='y')
        {
            while (compNo != myRandNo)
            {
                  //seed the rand
                  srand(time(0));
                  
                  //guess was to low
                  if (compNo < myRandNo)
                  {
                      randNo = rand();
                      compNo = (randNo % 100) + compNo;
                      //cout << compNo << "CompNo is smallest" << endl;
                  }
                  
                  //guess was to high
                   if (compNo > myRandNo)
                  {
                      randNo = rand();
                      compNo = (randNo % compNo) + 1;
                      //cout <<compNo  << "CompNo is biggest" << endl;
                  }
                  
                  tryes++;
            }
                  
             //cout << "\nThe computer guessed you number after " ;
             cout << tryes << " attempts" << endl;
        
             cout << "Would you like to play again? (y/n)";
             cin >> gameRep;
                   
            }
        
      
         
        system("pause");
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> //seed the rand
    You should only seed the random number generator once in your program, not each time through the loop. The way you have it, rand() will return the same number over and over again as long as the current time in seconds doesn't change. That's a bunch of runs since a lot of computation is done by a computer in that time.

    >> compNo = (randNo % 100) + compNo;
    I think it is actually this one that is not right. The first time through, compNo is 50. Then if guess is too low, you set compNo to that equation. But randNo%100 returns a value from 0 to 99, and when you add 50 to it you get something from 50 to 149. I would assume you want a range of 50 (or 51) to 100.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Action Game - AI Contest
    By Queatrix in forum Contests Board
    Replies: 4
    Last Post: 01-18-2007, 05:55 PM
  2. Looking for coder: Simple C++ Client / Server game
    By kocho in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 02-04-2006, 01:34 PM
  3. Whats a very simple but good game i can try to make?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2001, 08:24 PM
  4. Replies: 1
    Last Post: 11-06-2001, 01:15 PM
  5. not 3x3 tic tac toe game AI
    By Unregistered in forum Game Programming
    Replies: 9
    Last Post: 08-29-2001, 04:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21