# Tic-Tac-Toe game AI

I'm looking into creating a simple tic tac toe/noughts and crosses game in Actionscript3 and am trying to understand the ideas behind the AI used in a game like this.

I've seen some simplistic examples online but from what I've read a game tree or something like minimax is the best way to go about this. Can anyone help explain or reference any good examples of this?

I've seen that there is a library called as3ds - data structures for game developers which has a number of classes that might help tie this together?

Any info/examples or help is much appreciated.

-

If you implement the rules from Tic-tac-toe Wiki you can create the perfect AI that always wins, then you can decrease the difficulty by making the AI do random moves, and the frequency of random moves is the difficulty level. Here are the rules:

1. Win: If the player has two in a row, play the third to get three in a row.
2. Block: If the [opponent] has two in a row, play the third to block them.
3. Fork: Create an opportunity where you can win in two ways.
4. Block opponent's Fork:
• Option 1: Create two in a row to force the opponent into defending, as long as it doesn't result in them creating a fork or winning. For example, if "X" has a corner, "O" has the center, and "X" has the opposite corner as well, "O" must not play a corner in order to win. (Playing a corner in this scenario creates a fork for "X" to win.)
• Option 2: If there is a configuration where the opponent can fork, block that fork.
5. Center: Play the center. (If it is the first move of the game, playing on a corner gives "O" more opportunities to make a mistake and may therefore be the better choice; however, it makes no difference between perfect players.)
6. Opposite corner: If the opponent is in the corner, play the opposite corner.
7. Empty corner: Play in a corner square.
8. Empty side: Play in a middle square on any of the 4 sides.
-