AI TIC TAC TOE

 

Image for post
Image for post
AI X vs Human O

Table of Contents

About Tic Tac Toe


In a two-player (A vs B) game, if player A scores x points (utility units), player B loses x points. Total gains/losses always sum up to 0.

With that in mind, let’s proceed to the Minimax algorithm that’s suited for such cases.

Minimax algorithm

Minimax is a recursive algorithm which is used to choose an optimal move for a player assuming that the opponent is also playing optimally. As its name suggests, its goal is to minimize the maximum loss (minimize the worst case scenario).






Unbeatable Tic Tac Toe AI

Let’s look at the following board state. We will try to solve it as X.

Image for post
Image for post
Search tree for 0.0 state and X’s turn



Image for post

Tic Tac Toe AI would decide to go to the 1.0 node and win the game.


Due to the relatively small state space (3⁹ = 196839 possible board combinations), it can easily search the whole game tree for an optimal solution, treating the game as a fully deterministic environment.

On the other hand, chess for example has an enormously large state space of ~10¹²⁰ possibilities.

This is a figure far greater than the number of atoms in the observable universe.

With such extensive search spaces, we can still use Minimax algorithm, but we have to remember to limit the depth of our search. Otherwise, we can end up computing results for a very long time or even forever.


tl;dr Tic Tac Toe AI is unbeatable. You can draw at most and only with a perfect game. If you think that you can outsmart it and win the game, let me know how to do it in the comments section🙂.

Credits from medium article

Comments

Popular posts from this blog

Insertion Sort - Part 2 | C | HACKERRANK

Array Reversal problem solution | C | HackerRank

Insertion Sort - Part 1 | C | HackerRank |