Ai

This is a discussion on Ai within the Game Programming forums, part of the General Programming Boards category; Can someone show an example of how to make any type of an AI. Sorry for being so open. I'll ...

  1. #1
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Ai

    Can someone show an example of how to make any type of an AI. Sorry for being so open. I'll take any answer
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    //Let the computer guess what number you type
    
    int InputNumber;
    int ComputersGuess;
    int Difficulty;
    
    cout << "Enter difficulty: (1 - Easy, 2 - Normal, 3 - Hard) ";
    cin >> Difficulty;
    
    cout << "\nEnter a number: ";
    cin >> InputNumber;
    
    switch(Difficulty)
    {
       case 1:
          ComputerGuess = InputNumber + 1;
          break;
    
       case 2:
          ComputerGuess = (rand() % 1000);
          break;
    
       case 3:
          ComputerGuess = InputNumber;
          break;
    }
    
    cout << "You entered " << InputNumber;
    cout << ", the computer guessed the number " << ComputerGuess << endl;
    
    if(InputNumber == ComputerGuess)
    {
       cout << "Computer wins!" << endl;
    }
    else
    {
       cout << "Player wins!" << endl;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    AI is a pre-dertimened response based on certain factors and what the situation is.

    Here is an example of AI with a tic-tac-toe game...
    Code:
    bool loop = true;   //variable for the loop
    
    while(loop == true)     //while the loop is true
    {
    if(grid[1][1] == ' ')   //if the very middle square is empty
     grid[1][1] = 'O';     //then fill the very middle square with an O
    else     //if the very middle square isn't empty
     for(short h = 0; h < size; h++)    //the size of the 2d array grid
      {
        for(short i = 0; i < size; i++)     //the size of the 2d array grid
         {
           if(grid[i][h] == ' ')   //tries to find the first open square    
            {
             grid[i][h] = 'O';
             loop = false;       //breaks the loop
            }
           else if(i == size - 1 && h == size - 1)    //if no square is empty
           {
             loop = false;        //you still need to break the loop
           }
          }
       }
    You can download my game called dynamic-toe, which is an oo version of tic-tac-toe that lets you dynamically set the size of the grid between 3x3 and 9x9, by following the link in my sig. You can look at some of the AI I used in the game, which is fairly basic. Just click on Projects once you get to the site.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Oh, and look up recursion - I've found recursion to be very useful in AI for games, especially when the computer needs to look so many plys deep for a move.

  5. #5
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    I just figured i'd go ahead and say this, but there are links to http://www.aihorizon.com all over this site.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    state machines

    common technique is a state machine:


    you may have some enums like
    WANDER, HUNT, HIDE

    then some code (this is just psuedocode for it)

    Code:
    switch(state)
    {
        case WANDER:
               ChooseRandomDir();
               for each enemy:
                    if enemy is closer than X:
                        if enemy is big
                             state = HIDE
                             target = enemy
                        if enemy is smal
                              state = HUNT
                               target
         case HUNT:
                if target is in kill range:
                     kill
               if target is out of range:
                     state = WANDER
                if target is in hunt range:
                      MoveCloser(enemy)
            ....
    Code like that could produce an enemy which wanders around looking for something to kill, and with the right settings you could make it have a better range of detecting enemies, or actually predict where the player will move next.

  7. #7
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    Thx everyone! Though it never hurts to have more examples, I love what you guys have given me. (especially cozman)
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  8. #8
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    finite state machines are groovy. I use them in my games.

  9. #9
    Registered User Bieh's Avatar
    Join Date
    Sep 2002
    Posts
    83
    Try here
    "It always takes longer than you think, even when you take Hofstadter's Law into account."
    -Hofstadter's Law

    Bored? Try my game SpaceWars , made in Allegro and VC++
    or see my new game Redoubt , made in OpenGL and VC++

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, 07: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, 11:35 AM
  5. Technique of all board-like games?
    By Nutshell in forum Game Programming
    Replies: 28
    Last Post: 04-24-2002, 09:19 AM

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