Thread: Ai?

  1. #1
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67

    Ai?

    I was wondering how i would go about and make an AI that actually guesses a number that i pick by whether i say "Too high" or "Too low".
    Here is an example of code from a guessing game.

    Code:
    // Guess My Number
    // The classic number guessing game
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
    	srand(time(0)); // seed random number generator 
    
    	int theNumber = rand() % 100 + 1; // random number between 1 and 100
    	int guess= rand() % 100 + 1;//Computer guesses number between 1 and 100
    	int tries = 0;
        
    	cout << "\tWelcome to Guess My Number\n\n";
    
    	do
    	{
    		cout << "Enter a guess: "<<guess;
    		++tries;
    
    		if (guess > theNumber)
    			cout << "Too high!\n\n";
    
    		if (guess < theNumber)
    			cout << "Too low!\n\n";
    
    	} while (guess != theNumber);
    
    	cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    	
    	system("pause");
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Have the AI guese by generating random numbers. After each guese ajust the min or max random number values.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    So you want to make a game that's impossible to win?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    int guess= rand() % 100 + 1;//Computer guesses number between 1 and 100
    Like this:

    Code:
    int maxVal = 100;
    int minVal = 1;
    
    do{
      guess= rand() % maxVal + minVal;  // This has to be in the loop.
    
      cout << "Enter a guess: "<<guess;
      ++tries;
    
      if (guess > theNumber) {
         cout << "Too high!\n\n";
         maxVal = guess - minVal;
         }
      else if (guess < theNumber) { // Else if cause there is no point in checking both.
         cout << "Too low!\n\n";
         minVal = guess;
         maxVal = maxVal - minVal;
         }
    } while (guess != theNumber);
    My math appears to be a little messed up with the too low, but you get the idea.
    Last edited by SlyMaelstrom; 11-18-2005 at 08:04 AM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I think the OP is talking about a program where the user thinks of a number and the AI tries to guese it, something like this:
    Welcome to the guessing game! Think of a number and I will guess it.
    I guese 10
    is it (h)igher or (l)ower?
    h
    I guese 20
    ...

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    pseudocode
    Code:
    while(wrong)
    {
    guess = (min+max)/2
    if (too_high) max = guess-1
    else if (too_low) min = guess+1
    else you guessed right
    }
    Last edited by Darryl; 11-18-2005 at 10:10 AM.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I was going to suggest a binary search, but Daryl beat me to it.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple space combat AI
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-06-2009, 12:54 AM
  2. chess ai contest
    By Raven Arkadon in forum Contests Board
    Replies: 7
    Last Post: 07-09-2005, 06:38 AM
  3. AI Contest Proposal
    By MadCow257 in forum Contests Board
    Replies: 4
    Last Post: 03-13-2005, 03:27 PM
  4. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM
  5. Technique of all board-like games?
    By Nutshell in forum Game Programming
    Replies: 28
    Last Post: 04-24-2002, 08:19 AM