You are on page 1of 5

Tic-Tac-Toe unbeatable AI using minimax algorithm

Omar Ahmed Fahmy Ahmed Omar Ayman Ahmed Nasr Omar Ahmed Saad Ali
dept. of Electronics and Electrical dept. of Electronics and Electrical dept. of Electronics and Electrical
Communications Engineering Communications Engineering Communications Engineering
Cairo University Cairo University Cairo University
Giza, Egypt Giza, Egypt Cairo, Egypt
omar.ahmed.fahmy2000@gmail.com omar.h142000@gmail.com omars98.5@gmail.com
Omar Adel Ahmed Mohamed Omar Hossam Eldin Fathy Ali
dept. of Electronics and Electrical Omar Ahmed Mostafa Ahmed dept. of Electronics and Electrical
Communications Engineering dept. of Electronics and Electrical Communications Engineering
Cairo University Communications Engineering Cairo University
Cairo, Egypt Cairo University Cairo, Egypt
omaradelkhedr@gmail.com Cairo, Egypt omar21hossam@gmail.com
Mohamed Yasser Mohamed Ali mory2018m@gmail.com Mohamed Mohamed Zaky Mohamed
dept. of Electronics and Electrical dept. of Electronics and Electrical
Communications Engineering Communications Engineering
Cairo University Cairo University
Giza, Egypt Cairo, Egypt
modiyasser30@gmail.com mohamedzaky177@gmail.com

Abstract—Tic-Tac-Toe is a very well-known pencil-and- thinking patterns. Tic tac toe is a game of predictability.
paper game that is popular among all age groups. It is held Actions are believed to be very important and predictable.
between two players, where each player must choose either ‘X’ This predictability helps to develop children's strategic
or ‘O’ and the winner is the one who succeeds in placing 3 thinking. They can learn through observation what their
successive marks (of their respective marks) either vertically, opponents’ next move is and think how to block them. This
horizontally or diagonally. In this paper, A machine-learning- could be considered as a simple and effective version of
based Tic-Tac-Toe game is approached, where instead of just chess. Children think more logically in order to figure out
having two human players playing against each other, an what they can do to win the game. It is a simple game where
artificial Intelligence (AI) can be used for playing as a single
players soon discover that optimal moves from both sides
player mode (i.e. a player against an AI). The AI is taught to
make choices and choose the appropriate moves that win it the
will always lead to a draw. In other words, a game between
game, which is –basically- the idea of machine learning. The two expert players will always end up with a draw. Hence, it
concept of the game theory is based on the minimax algorithm is more popular among children who have not yet
which draws the ideal scenario for the AI not to lose any game. discovered the perfect strategy. So, how can we take the
This results in an unbeatable AI, where the game ends in a idea of the game to the next level from just being a pencil-
draw in the worst case, when the player is playing optimally. and-paper game between two human beings to be including
Once the player makes a mistake, the AI takes the advantage to an interactively thinking Artificial intelligence (AI)? How
win the game. can we make use of machine learning science and convince
the AI to make the right choices to avoid losing? How can
Keywords—decision theory, combinatorial game theory, we take in consideration every single possibility in order to
minimax algorithm, game tree. build an unbeatable AI? This is what we are intending to
approach in this research. We aim to minimize the
I. INTRODUCTION computational time by using a suitable machine-learning for
the purpose. We need to teach the AI how to think and how
Tic tac toe is a pencil-and-paper game that has been to choose the next move, which is the main idea of
known for many years. It is a game which is played between “Machine-Learning”.
two players, X and O, and whoever succeeds in placing
three of their respective mark win the game. There are three
possible types of moves at the beginning of the game, II. LITERATURE SURVEY
namely – Centre, Corner and Edge. As shown in Fig. 1. Tic tac toe is a classic game that has been known for an
position 4 is the “Centre”, (0,2,6,8) are the “Corners” and exceptionally long time. In fact, games that are played on a
(1,3,5,7) are the “Edges”. three-in-a-row board had their origin in Egypt, where traces of
these games’ boards have been actually found on ceiling tiles
from around 1300 BCE. The game has been called by many
names over time, such as “X's and O's”," “X-e O-zees” and
“Boxin' Oxen”. Its name can even differ from one country to
another. The game is named “Tic-Tac-Toe” in the US,
“Noughts and Crosses” in the UK and “Twiddles and Bears” in
Norway. The name which is recognized the most in many
countries is “Tic-Tac-Toe” which has other accepted spellings
as tick tack toe, tick tat toe and tit tat toe depending on the
region and the country you are in. Around the first century BC,
Fig. 1. Representation of cells in a 3x3 grid. an early version of the game was played in the Roman Empire.
Playing tic tac toe helps teaching computational thinking Their version was entirely different and more complicated
skills when played by school aged students, according to a
study [1]. The study shows that these kinds of games help
young children to better draw out and articulate algorithmic
than the version we know today. It was called Terni Lapilli player, assuming that the opponent is also playing
[2] back then and each player had only three pieces to play optimally. The players are called maximizer and minimizer.
with instead of possessing any number of pieces, so in order As the name suggests, the maximizer aims to get the highest
to keep playing, both players had to move these pieces possible score, while the minimizer does the opposite.
around. In addition to the Romanian civilization, tic tac toe’s Hence, each board state during the game has a certain value.
markings have been also found in many places in Rome. It is given a positive value if it’s the maximizer’s turn and a
However, computational intelligence techniques were first negative value for the minimizer’s. Fig. 2. shows an
used in games in 1952 [3]. A scientist called Samuel was example that illustrates how minimax works. Consider a
able to apply the first computational algorithm to a board
game state where the maximizer player needs to decide
game known as Checkers. Without human intervention,
whether to go left or right, assuming the opponent is playing
through just observing the sequences that led to a win and
the sequences that led to a loss and using the very little and optimally. Since minimax is a backtracking algorithm, it
limited hardware tools at that time, the algorithm was able to would try all possible moves, then backtracks and makes a
learn and apply strategies good enough that it was even able decision. The minimizer has two choices between 3 and 5
to beat its own creator. After the early success of Samuel, a on the left sub tree, and between 2 and 9 on the right sub
computer scientist developer called Alexander S. Douglas tree. The maximizer’s choice depends on what the
was able to create the first ever computerized version of tic minimizer will choose in both sub trees. Being the
tac toe in 1959. This was the first tic tac toe video game that minimizer, it will choose the least of any given values;
had an AI embedded inside. He generated an efficient hence it will choose 3 on the left sub tree and 2 on the right
strategy for not having to lose a game even once. This was sub tree. Now it is the maximizer’s turn which chooses the
the first AI to be embedded in a tic tac toe game. In 1975, most of any given values. The two choices for the
MIT students used tic tac toe to build a tinkertoy computer maximizer are 3 on the left and 2 on the right; hence it will
to illustrate the computational power of Tinkertoy elements. choose 3. So, the optimal move for the maximizer in this
A tinkertoy computer is a computer which is made only of case is to go left.
tinkertoys, which was able to play tic tac toe games
perfectly. Accordingly, the computer was placed at the
Museum of Science in Boston. Then, research in the field of
computational intelligence remained silent for a long time.
But as a part of artificial intelligence research, some
researchers started to apply many classical AI techniques to
board games such as Checkers and Chess. Eventually, this
type of research led to the very popular victory of a Chess
computer - known as deep blue [4] - against the world chess
champion Kasparov in 1997. Later, a scientist called
Hochmuth used a genetic algorithm to develop an ideal tic
tac toe strategy, which does not lose a single game. He was Fig. 2. Minimax algorithm working principle.
able to conclude that there exist 827 different unique game
states and hence concentrated on finding a no-loss strategy. So, it can be concluded that the minimax algorithm
The study reported a single no-loss strategy, but it did not works according to the flow char in Fig. 3. The algorithm
provide a detailed description for that strategy to know its can be thought of as a representation of the human thought
properties. of saying “OK, if I make this move, then my opponent will
block me with that move, but if I make that one, it will
After analyzing some of those previously applied definitely lead to a win. So, this is the right move to make”.
strategies using various algorithms and comparing them
with the existing methodologies, in this paper, our goal is to
revisit the use of the minimax algorithm – which will be
later discussed in detail – which proved to be the perfect
strategy and the most efficient algorithm in finding a no-loss
strategy and creating an unbeatable AI.

III. METHODOLOGY AND DATA DESCRIPTION


The tic tac toe game is known under the category of
“game tree” [5], which is a type of recursive function that
conducts and tests all possible moves in the game and
chooses the optimal one. Hence, tic tac toe can be modeled
as a tree with a root node which represents the initial state of
the game. The child represents the corresponding upcoming
possibilities of states. This tree is extended until all
possibilities of the outcome of each state are considered. So,
in order to conduct and test all possible moves, minimax
algorithm is used.
Minimax is a well-known backtracking algorithm [6] which
is commonly used in board games such as tic tac toe,
checkers, and chess. It tries to find the optimal move for a
Fig. 3. Minimax algorithm flow chart.
Having already defined the concept behind the minimax evaluated in the minimax algorithm search tree. It stops
algorithm, let’s study a random case during a tic tac toe game assessing a move when a particular perfect possibility has
by applying minimax. Consider the example in Fig. 4. It is been found and any further analysis would not lead to any
required to decide the optimal move for X at the initial state better possibility (i.e. any other possibility remaining will be
0.0, where X is the maximizing player and O is the opponent, worse than a previously examined move). Hence, once this
the minimizing player. For state 1.0, it is clear that it is a optimal move is found, there is no need to analyze other
winning chance for X; hence it is given a positive value +1. moves. When applied to a minimax search tree, it would
State 1.1 offers the opponent 2 choices, state 2.0 and state 2.1. definitely return the same move as minimax would, but the
State 2.0 is a winning state for the opponent, so it will be given advantage here is that it prunes branches that won’t
a negative value -1. State 2.1 can only give one possibility state influence the final move decision away; hence decrease the
3.0, which is a winning state (+1). State 1.2 offers the opponent computational time needed. Fig. 5. shows an example that
a chance to win in state 2.2 (-1) and state 2.3 which leads to a illustrates the working principle of the alpha-beta pruning
winning state 3.1 (+1). Now that we have the values of each algorithm. Using the same analysis we’ve used in the game
state, we can apply the maximizing/minimizing theory. Since in tree in Fig. 2., it can be concluded that 6 will be the best
level 2 the opponent has the upper hand, then it will always move for the maximizer. However, it is clear that some
choose the least score. For state 2.0 (-1) and state 2.1 (+1), the branches are grayed out. These branches need not to be
opponent will choose state 2.0, hence state 1.1 now has the assessed (when moves are assessed from left to right) as
value -1. For state 2.2 (-1) and state 2.3 (+1), the opponent will they would not lead to any better options. For example, the
choose state 2.2, hence state 1.2 has the value -1. Now that we last minimizer in the second sub tree has a choice between
have state 1.0 (+1), state 1.1 (-1) and state 1.2 (-1), and it is X‟s 7, 4 and 5 while the maximizer has a choice between 5 and
turn, then it will choose the highest possible score, which is the number will be chosen by the minimizer; hence the
state 1.0. Hence the optimal move for X at state 0.0 is state 1.0. maximizer’s choice will be greater than or equal 5. It will be
greater than 5 in case the minimizer chose a number greater
than 5. Once a number less than 5 appeared in the
minimizer’s choices, the maximizer will choose 5. The first
choice for the minimizer is assessed to be 7, so further
analysis is needed. The second choice is assessed to 4, at
this point no further analysis is needed and there is no point
to assess the third choice because in this case the
minimizer’s choice will be less than or equal 4, which
would force the maximizer to choose 5 whatever the value
of this third choice.

Fig. 5. Alpha-beta pruning algorithm

IV. EXPERIMENTAL RESULTS AND ANALYSIS


Fig. 4. A search tree for state 0.0.
In this section, we would like to first introduce some
So, it can be concluded that the minimax algorithm consists general statistical analysis about the game. We do so to
mainly of four steps: show how minimax algorithm performs and attains the
• It generates the whole game tree at a certain game state, target of an unbeatable AI that plays no-loss game. Next, we
all the way down to the terminal states. show some results from our program that agree with these
conclusions and analyses.
• Determine the value of each child from each state.
Tic tac toe game is a classic game that is still being
• Recursively, it uses the values of each child to determine played to this day. Almost all algorithms that are being used
the value of the state which is one level higher. in this game aim for the same purpose, to block the
opponent’s way. Each programmer or developer has his own
• Finally, chooses the move that leads to the highest style of algorithm. However, according to Newell’s and
possible score. Simon’s 1972 popular tic tac toe program, the most
An optimizing algorithm called “Alpha-beta pruning” important thing in your algorithm is to include all the
algorithm is also added to the minimax algorithm in order to aspects of the game, not leaving a single probability.
enhance its functionality [7]. Alpha–beta pruning is a search
algorithm that aims to diminish the number of nodes
So, to completely analyze the simple game of tic tac toe, will always be the winning player in the 4! =24 cases no
we have to ask ourselves some questions: matter what. However, as the grid size increments over 3x3
grid, the probability of having the first player winning starts
• How complicated is tic tac toe? to decrease and the tie probability starts to increase.
• Does the first mover have an advantage to win? For a 4x4 grid, the histogram in Fig. 7. shows that there
• How much luck is involved? becomes a higher chance of a draw -as expected- about
42%, first mover still has a higher chance than the second
• What are the best starting squares? mover, but the difference between them is less dramatic as
the ratio became 31% - 26%. Notice that the first and
• How did we enhance our algorithm to perform better?
second mover distributions are about to overlap at the edges.
• Why did we choose minimax algorithm?
The answers of these questions will be based on brute-
force search [8], which is a general exhaustive search
technique for problem-solving that draws all the possible
candidates of a problem and checks whether or not these
candidates satisfy the solution for the problem. For example,
if it is given the number of the remaining empty squares,
then it would tell us the probability of having the first player
winning, the probability of having the second player
winning, and the probability that the game ends with a draw.
How complicated is tic tac toe? One way to answer Fig. 7. Percentages histogram for a 4x4 grid game.
such question is to actually know exactly how many
possible games there are. The player starting can choose any For a larger 5x5 grid, the probability of a draw gets even
of 9 moves, the second has 8 options, then the first has 7, higher as shown in Fig. 8. It increases dramatically to 60%.
etc. So, it can be said that there are 9! possible games or, The first mover’s percentage of win became 25% only while
similarly, 362,880 games. Actually, many of these games the second mover’s became 13%.
are symmetrical, meaning that they are just the reflection of
each other, so they can be excluded. The symmetric games
are evaluated to be 107,712 games [9] according to brutal-
force search, so we can say that there are only 255,168
different scenarios for a tic tac toe game.
Does the first mover have an advantage to win? At the
first glance and by intuition we would say, yes. That’s
because the first player gets 5 moves, while the second gets
only 4. That is theoretically speaking, which can be
confirmed by analysis. Using brute force, the first player
wins 58.4% of the games, the second player wins 28.8% and
it ends with a draw in 12.7%. Fig. 6. shows these winning Fig. 8. Percentages histogram for a 5x5 grid game.
percentages histogram on a 3x3 grid for draws (green),
In Fig. 9., as we reach a 6x6 grid, the first player and the
second player (blue) and first player (orange).
second player chance to win are almost the same in the
range of 12-15%, while their tie probability increases much
to 75%. So as the grid size increases, there would no
advantage to go first.

Fig. 6. Percentages histogram for a 3x3 grid game


This actually shows that the first mover has a higher chance Fig. 9. Percentages histogram for a 6x6 grid game.
to win the game. These results are based on two players who
are playing completely random moves. Practically, if two How much luck is involved? To know exactly if luck has
players are playing perfect moves, the game will obviously end a role during a game, we need to study a case where a game is
with a draw. The previous results are based on a 3x3 grid tic tac between a player who is playing perfectly and a player who is
toe games. However, tic tac toe can be played on any NxN grid, playing randomly, and see the winning percentage for the
and each grid has its own statistical analysis and probabilities random player. When the random player is first to start, his
according to [10]. A 2x2 grid game is actually called the trivial winning percentage is 0.00%, while the perfect player’s
case, as the first player winning percentage is 80.64%, and the game
tends to be a draw in 19.36% of the games. So, that is in simple games like tic tac toe without the need of machine
equivalent to a 90.32% - 9.68% advantage. On the other learning classifiers [11].
hand, if the perfect player is going first, then his winning
chance is 99.47% while the random player is 0.0%, and the V. CONCLUSION AND FUTURE WORK
draw percentage is 0.53%. This is equivalent to a 99.74% -
0.26% advantage. Combining these two interesting results, Tic tac toe is a simple game that has been played for
we find that the perfect player has a 95.03% - 4.97% thousands of years. The winner is the player that succeeds in
advantage over the random player. This shows that luck is placing three of their marks either vertically, horizontally or
not that much involving in tic tac toe game. diagonally. Players soon notice that optimal moves from
both sides will always lead to a tie; hence it is more popular
What are the best starting squares? By experience and among children who have not discovered the trick yet.
many trials, the first mover best starting square by far is any Many researches have been made in field of computer
corner, which opens a chance for either a win or a tie science and machine learning in order to reach an optimal
depending on the opponent’s next move. If the opponent computerized algorithm for an AI playing tic tac toe.
chose any other square than the center, then this game will Minimax is the optimal algorithm that decides a stable
obviously be a winning game for the first mover, assuming solution and can implement a no-loss game strategy for an
he is playing optimally. Fig. 10. shows such a case where AI. There is a possibility of a game, where the number of
the second mover (O) chose the upper edge instead of the moves is so vast that you could not possibly compute the
center which led to a win for the AI (X). This explains why whole tree for minimax. Chess is an example of such game.
we programmed the AI in our program to always choose a As a result, you will need some heuristic or estimated guess
corner, whenever it is the starting player. On the opposite of what the score could be with any given state so that you
side, the second mover can always force a tie for the game do not actually stop at the end of the tree. Instead, you stop
by choosing the center, if it is not already taken by the first at predetermined depth and try to make an estimate. In this
player. The only chance of the second player to win is that case, some machine learning classifiers (supervised
the first player makes a mistake. This demonstrates why we problem) are suggested to make that estimate.
stated that if the two players are playing optimally, the game
will always end with a tie.
REFERENCES

[1] Tak Yeon Lee, Matthew Louis Mauriello. “Computational thinking


with games in school age children”. International Journal of Child-
Computer Interaction, vol. 2, issue 1, Jan. 2014
[2] Kowalski, Wladyslaw. (2004). Board Games of the Ancient World. P
Art and Culture. Autumn. 2-25.
[3] E. Soedarmadji. “Decentralized decision making in the game of tic-
Fig. 10. A tic tac toe game case tac-toe”. In Proceedings of the 2006 IEEE Symposium on
Computational Intelligence and Games, pages 34–38, 2005.
How did we enhance our algorithm to perform [4] Murray Campbell, A. Joseph Hoane Jr, Feng-hsiung Hsu. “deep
better? We have made some developments in our program blue”. Artificial intelligence, vol. 134, pp. 57-83, Jan. 2002.
to improve the performance. First development is alpha-beta [5] T. A. Marsland and F. Popowich, "Parallel Game-Tree Search," in
pruning. Alpha-beta pruning refers to a situation during the IEEE Transactions on Pattern Analysis and Machine Intelligence,
processing of the algorithm where an optimal path is found vol. PAMI-7, no. 4, pp. 442-452, July 1985.
and it is known that all other paths or possibilities are going [6] G.C Stockman. “A minimax algorithm better than alpha-beta?”.
to be worse, so there is no point to go forth and check every Artificial intelligence, vol. 12, pp. 179-196, Aug. 1979.
possibility. Second development is that we considered the [7] Richard E. Korf. “Multi-player alpha-beta pruning.” Artificial
intelligence, vol. 48, pp. 99-111, Feb. 1991.
depth, meaning that if there are several possible ways to win
[8] Robin “Brute force search”. Internet:
the game, our AI always go for the shorter path. And the intelligence.worldofcomputing.net/ai-search/brute-force-
benefit of the depth aspect appears more obvious when we search.html#.XoIuoexubIV, Dec. 14, 2009 [Mar. 24, 2020].
have the case of a larger tic tac toe grid (6x6 for example). [9] Kevin Gong “Analysis of Tic-Tac-Toe”. Internet:
http://kevingong.com/Math/TicTacToe.html, Jul. 29, 2006 [Apr. 1,
Why did we choose minimax algorithm? We have chosen 2020].
the minimax algorithm as it is the most popular algorithm in all [10] Amachwe “Tic Tac Toe: Statistical Analysis with arbitrary Grid
kinds of board games like tic tac toe, connect four, chess, etc. It Size”. Internet: fisheyefocus.com/fisheyeview/?p=557, May 9, 2017
is a game theory algorithm whose strategy always succeeds [Mar. 30, 2020].
against intelligent opponent with no failures, especially when [11] S. B. Kotsiantis. “Supervised Machine Learning: A Review of
you could compute the whole tree Classification Techniques.” Informatica, vol. 33, pp. 56-99, Jul. 2007.

You might also like