A little background: as a way to learn multinode trees in C++, I decided to generate all possible TicTacToe boards and store them in a tree such that the branch beginning at a n
The "naive" way to do this (for an arbitrary game where two players take turns doing a move) is to try each possible move recursively until you end up with a board where one is the winner, then back-track upwards in the tree marking the nodes as "O wins", "X wins" or "draw".
Each time you step up (one such step is usually called a ply), depending on who's move it is, assume the player chooses the move that is best for him/her. Since you are moving from the leaves and upwards, you will always know the optimum possible results for each child node.
When counting the number of possible winning or losing boards in a subtree, you are essentially assuming that each player will always make a random move. As you noted, this will not be very effective if you play against a smart player. The scheme I outlined above instead assumes that the opponent always makes a perfect move, trying to win.
The (usually) correct way to implement AI based on a decision tree is to use the "Minimax" algorithm:
Work your way up the tree, applying the following rules to each node:
Of course, even and odd might need to be reversed, depending on who you decide goes first.
You can read more at:
Tic-Tac-Toe can be solved using a greedy algorithm and doesn't really require a decision tree.
If you want to continue using your current algorithm, do as patros suggests, and minimize the possibility of losing at each decision.
If you want a simpler approach have the AI do the following each turn:
Rate each square for its desirability, for each other taken square (by the AI) on a line, add one point of desirability for that square. For each square taken by the opponent, remove one point of desirability.
For example, if the board is currently:
_|O|X
_|X|_
O| |
The top-left corner has a desirability of 0 (1 for the X in the same row, and 1 for the X in the diagonal, but -1 for each of the Os).
Play on the most desirable square. Breaking ties arbitrarily.
In the example from above, the AI would choose the mid-right square, since it has a desirability of 2, which would lead to a win the following turn.
If the game has just begun, play the center square, if the center square is taken, choose a corner at random.
This was my grade 10 Visual Basic term project. It's impossible to beat and requires far less memory than storing a decision tree.
Your existing algorithm is good, except you are forgetting one thing. Never choose any path where a move by the other player results in you being unable to at least tie.
So basically, discard any branch where the players next move could result in an un-tieable situation and then run your existing algorithm. This results in the highest chance of winning against a non-perfect opponent, while removing the possibility of losing.