Thread: My AI is AS (Artificial Stupidity)

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69

    My AI is AS (Artificial Stupidity)

    Hello, my AI is not going so great. I can make the computer make a good first guess but then the guesses are way off. Is there anyway I could make a record of all the guesses, find the middle two, and find the difference?
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int game()
    {
        using namespace std;
       
        cout << "Welcome to High or Low - Beat Adam" << endl;
        cout << "What is your first name?: ";
        string name;
        cin >> name;
        cout << "Welcome " << name << ".";
        Sleep(2000);
        system("cls");
        srand((unsigned)time(NULL));
        bool win;
        win = false;
        char turn;
        turn = 'o';
        int answer, guess, guessyou, guessadam, whowon;
        guessyou = 0;
        guessadam = 0;
        answer = rand()%1001;
       
        while (win == false)
        {
       
            while (turn == 'o')
            {
                cout << name << "'s turn." << endl;
                cout << "Guess a number between 1 and 1,000: ";
                cin >> guess;
                guessyou++;
           
                if (guess == answer)
                {
                    cout << endl << "Congratulations! You have beaten computer Adam in " << guessyou << " guesses!";
                    whowon = 1;
                    win = true;
                    turn = 'x';
                }
           
                else if (guess > answer)
                {
                cout << "Too high!" << endl;
                turn = 'a';
                }
               
                else if (guess < answer)
                {
                cout << "Too low!" << endl;
                turn = 'a';
                }
               
                else
                {
                    cout << "An unknown error occured." << endl;
                    cout << endl;
                    return 0;
                }
           
            while (turn == 'a')
            {    
                cout << endl << "Adam's turn.";
               
                if (guess > answer)
                {
                    guess = guess / 2;
                    guess = abs(guess);
                    cout << endl << "Adam guesses " << guess << "." << endl;
                    guessadam++;
                   
                    if (guess == answer)
                    {
                        cout << "Ha ha! Adam won in " << guessadam << " guesses!" << endl;
                        whowon = 2;
                        win = true;
                        turn = 'x';
                    }
                   
                    else if (guess > answer)
                    {
                        cout << "Too high!" << endl << endl;
                        turn = 'o';
                    }
                   
                    else if (guess < answer)
                    {
                        cout << "Too low!" << endl << endl;
                        turn = 'o';
                    }
                   
                    else
                    {
                        cout << "An unknown error occured." << endl;
                        cout << endl;
                        return 0;
                    }
                }
               
                else if (guess < answer)
                {
                    guess = ((guess / 2) + guess);
                    guess = abs(guess);
                    cout << endl << "Adam guesses " << guess << ".";
                    guessadam++;
                   
                    if (guess == answer)
                    {
                        cout << "Ha ha! Adam won in " << guessadam << " guesses!" << endl;
                        whowon = 2;
                        win = true;
                        turn = 'x';
                    }
                   
                    else if (guess > answer)
                    {
                        cout << endl << "Too high!" << endl << endl;
                        turn = 'o';
                    }
                   
                    else if (guess < answer)
                    {
                        cout << endl << "Too low!" << endl << endl;
                        turn = 'o';
                    }
                   
                    else
                    {
                        cout << endl << "An unknown error occured." << endl;
                        cout << endl;
                        return 0;
                    }
                }
            }
        }
    }
    }
    int main()
    {
        using namespace std;
       
        game();
        cout << endl;
        system("pause");
        return 0;
    }
    Thanks for help, Adam
    Adam

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Adam, I can tell you something you should be doing differently. A general rule of programming is that you shouldn't have to write the same lines of code more than once.

    I see here that you have the same lines of code in different if and else statements. All this does is extend the length of your code and make it very hard to read. It's possible to reduce the multiple occurances of code blocks to just one block.

    And where do you actually assign "guess" when it's Adam's turn before you try to test it?

    Code:
            while (turn == 'a')
            {    
                cout << endl << "Adam's turn.";
    
                 //**Where is the assignment?**
               
                if (guess > answer)
                {
    Should be some kind of random assignment here. You might want to create 'max' and 'min' variables here.

    With those two variables, you can generate a random number within a certain range, based on what Adam knows.

    For instance, if he "knows" that 50 is too low and 100 is too high from guessing in the past, you can generate a random number between 50-100 by keeping track of what's too high and what's too low.

    The way to implement something like that is:

    Code:
    int range = (max-min)+1
    guess = (rand()%range)+min
    In this, our range is 51. You can only have a remainder as high as 50 so this gives you a random number, 0-50...then adds min (50) to create a random number between 50-100

    Hope that helps.
    Last edited by Krak; 10-13-2005 at 08:00 PM.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Guess is assigned by the other player's last guess. That's why it's so screwed up.
    Adam

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. Artificial Life: Where to Start?
    By Mr.Sellars in forum General AI Programming
    Replies: 11
    Last Post: 09-22-2007, 02:03 AM
  3. chess ai contest
    By Raven Arkadon in forum Contests Board
    Replies: 7
    Last Post: 07-09-2005, 06:38 AM
  4. AI Contest Proposal
    By MadCow257 in forum Contests Board
    Replies: 4
    Last Post: 03-13-2005, 03:27 PM
  5. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM