# 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. ## 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

2. 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;
}```

3. 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. 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. I just figured i'd go ahead and say this, but there are links to http://www.aihorizon.com all over this site.

6. ## 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. Thx everyone! Though it never hurts to have more examples, I love what you guys have given me. (especially cozman)

8. finite state machines are groovy. I use them in my games.

9. Try here

Popular pages Recent additions