# Thread: Tic Tac Toe AI

1. ## Tic Tac Toe AI

The examples I've been able to find on this board seem to use numerous if statements.. I was wondering if there was another way? I've tried the following for statements, but they cause the program to crash when the function is called.

Code:
```for(Row = 0; Row < Board.numrows(); Row++)
{
if((Board[Row][0] == Board[Row][1]) && (Board[Row][2] == ' ') && (Board[Row][0] != ' ') && (Board[Row][1] != ' '))
{
//Row = Row;
Col = 2;
}
if((Board[Row][0] == Board[Row][2]) && (Board[Row][1] == ' ') && (Board[Row][0] != ' ') && (Board[Row][2] != ' '))
{
//	Row = Row;
Col = 1;
}
if((Board[Row][1] == Board[Row][2]) && (Board[Row][0] == ' ') && (Board[Row][1] != ' ') && (Board[Row][2] != ' '))
{
//	Row = Row;
Col = 0;
}
}

for(Col = 0; Col < Board.numcols(); Col++)
{
if((Board[0][Col] == Board[1][Col]) && (Board[2][Col] == ' ') && (Board[0][Col] != ' ') && (Board[1][Col] != ' '))
{
Row = 2;
// Col = Col;
}
if((Board[0][Col] == Board[2][Col]) && (Board[1][Col] == ' ') && (Board[0][Col] != ' ') && (Board[2][Col] != ' '))
{
Row = 1;
// Col = Col;
}
if((Board[1][Col] == Board[2][Col]) && (Board[0][Col] == ' ') && (Board[1][Col] != ' ') && (Board[2][Col] != ' '))
{
Row = 0;
// Col = Col;
}
}```
Yep.. this is a school assignment, but I'm not expecting it to be written for me, I just need a little nudge in the right direction. Any help would be appreciated, thanks.

2. small snippet from recent game made for kid....
Should be enough to help ya out...
Code:
```int MainWindow::IsWinner()
{
// this holds the patterns we will check for
static int pattern[8][3]=
{
0,1,2,
3,4,5,
6,7,8,
0,3,6,
1,4,7,
2,5,8,
0,4,8,
2,4,6
};
// this loop does the checking
for( int i = 0; i < 8; ++i)
{
if( (GameGrid_[pattern[i][0]] == EX) &&
(GameGrid_[pattern[i][1]] == EX) &&
(GameGrid_[pattern[i][2]] == EX))
return EX;
if( (GameGrid_[pattern[i][0]] == OH) &&
(GameGrid_[pattern[i][1]] == OH) &&
(GameGrid_[pattern[i][2]] == OH))
return OH;
}
// no winner so return 0
return 0;
}```

3. >I was wondering if there was another way?
There are plenty of ways to write an AI, but for Tic-Tac-Toe, a short series of if statements is sufficient. When you get into games with more cases for every move, such as checkers or chess, you will need to look at more sophisticated techniques.

>but they cause the program to crash when the function is called.
You should step through the function's execution in the debugger. Quite likely you are accessing an index outside of your table's boundaries.

>I just need a little nudge in the right direction.
You'll hate me for this, but www.google.com should be your first stop when it comes to research.

4. Ah, alright, thanks for the help.

5. ## Re: Tic Tac Toe AI

1) format your code a tad better. Long lines are very cumbersome to read no matter whether in an editor or on the forum. Statements can be listed on multiple lines:
Code:
```if((Board[Row][0] == Board[Row][1]) &&
(Board[Row][2] == ' ') &&
(Board[Row][0] != ' ') &&
(Board[Row][1] != ' '))```

Originally posted by Isamo
The examples I've been able to find on this board seem to use numerous if statements.. I was wondering if there was another way? I've tried the following for statements, but they cause the program to crash when the function is called.
Which statement causes it to crash?

2) If you aren't into the debugger yet, start adding printf() statements at key positions, displaying key information and a label of some kind. This can pinpoint exactly which statement is causing problems.

6. Thanks, I can see why starting a new line makes it easier to read

Once the program hits the for loops, it will crash.. but I was able to get something about an Illegal matrix - 3 max.. something

7. Since we have
-- no idea what value is returned by Board.numrows()
-- no idea what value is returned by Board.numcols()
-- no idea how Board[][] is defined
we can't tell you much.

It appears that Board is a structure or class.
It also appears that said structure might be a two-dim array

If above is true, what value could be in Board[Row][0] ? And which Board.numrows() is being called?

8. I made a tic-tac-toe program in BASIC a long time ago. I have it available for download here: http://www.jasondoucette.com/ai.html#TicTacToe

It computes moves by looking at patterns in the board - not by a search tree. This obviously required me to solve the game by hand, to be able to respond to any pattern that may arise. I have not looked at the code since I wrote it, and it was only for a simple computer course project, so I do not know how well it is written, or how much it will help you.

9. i had posted my tic tact toe game on thi sboard back some time with some level of AI...

it was based on a search tree.. Believe me this is not an efficient way of doing this and the if statements will be really huge in numbers... A better option would be for the game to analyze the players next 2 or 3 possible moves and make its move.... I used this kind of an AI in my chees games (posted here long back) which was quite good at defeating novice players...

10. Originally posted by vasanth
it was based on a search tree.. Believe me this is not an efficient way of doing this and the if statements will be really huge in numbers...
Do you mean that the search tree is not an efficient way, or using the if statements? I assume you mean the if statements, as you end your sentence mentioning them.

There are not really that many patterns in the board, from what I remember of my program (link in my previous post). I think you just have to be a little smart about the if statements you choose, so that one statement can solve many board positions at once.