Search code examples
algorithmartificial-intelligencetic-tac-toe

Best algorithm for 5x5 tictactoe ai using 4 in a row


What would be the best algorithm to use if i am creating a 5x5 tictactoe ai using 4 in row. The original algorithm that we were supposed to use was the minimax but we are only given 10 seconds per turn.


Solution

  • If you want to build a high-quality player there are many important enhancements you can implement.

    First, of course, is alpha-beta pruning, but there are several other techniques that will make alpha-beta pruning even more effective.

    Second, as the time limit is important, you should add iterative deepening. That is, you search first to depth 1, then to depth 2, etc. When you run out of time you take the best move from the previously completed iteration. Because the tree grows exponentially, you don't really lose anything from your previously iterations. (With a branching factor of 2 the overhead is a factor of 2, but as the branching factor increases further this overhead drops to nothing.)

    Third, use the history heuristic to order your search. On the small iterations you'll learn a better ordering of states so that you get closer to the optimal ordering (for alpha-beta pruning) on the later iterations.

    Fourth, use a transposition table to avoid duplicate states. There are many transpositions that occur when searching the tree, and detecting them early will result in significant savings. (This will probably have a bigger impact than the history heuristic.)

    Finally, build the best evaluation function possible. The better you are at evaluating states the better you play. (In the limit a perfect evaluation would then only require a 1-ply search to play perfectly.)

    Of course, if you can just solve the game, do that. There are only 3^25 (847,288,609,443) possible states in 5x5 tic-tac-toe, so with a decently powered machine you can solve the game, giving you the perfect evaluation function.