CHAPTER 1: INTRODUCTION
Tic-tac-toe also known as noughts and crosses is a paper and pencil game for two players, who
take turns marking the spaces in a 3 x 3 grid traditionally. The player who succeeds in placing
three of their marks in a horizontal, vertical or diagonal row wins the game. It is a zero-sum of
perfect information game. This means that it is deterministic, with fully observable environments
in which two agents act alternately and the utility values at the end of the game are always equal
and opposite. Because of the simplicity of tic-tac-toe, it is often used as pedagogical tool in
artificial intelligence to deal with searching of game trees. The optimal move for this game can
be gained by using minimax algorithm, where the opposition between the utility functions makes
the situation adversarial, hence requiring adversarial search supported by minimax algorithm
with alpha beta pruning concept in artificial intelligence.
1.1 OBJECTIVES
1. To develop Artificial intelligence-based tic-tac-toe game for human Vs AI by
implementing minimax algorithm with adversarial search concept.
2. To analyze the complexity of minimax algorithm through 4x4 tic tac toe game.
3. To study and implement alpha-beta pruning concept for improved speed of searching the
optimal choice in tic-tac toe game.
4. To study optimizing methods for alpha-beta pruning using heuristic evaluation function.
CHAPTER 2: AGENT
2.1 Agent
An agent perceives its environment through sensors and acts on the environment through
actuators. Its behavior is described by its function that maps the percept to action. In tic tac toe
there are two agents, the computer system and human. In the AI-based tic-tac toe game the
function is mapped using the minimax algorithm alongside alpha beta pruning. The agent can be
further described by following factors:
Performance Measure: Number of wins or draws.
Environment: A 4x4 grid with 16 cells respectively, with opponent (human agent). The
cell once occupied by a mark cannot be used again. Task environment is deterministic,
fully observable, static, multi-agent, discrete, sequential.
Actuators: Display, Mouse (human agent)
Sensors: The opponent’s (human agent) input as a mouse pointer on a cell
Furthermore, it is a utility-based agent since it uses heuristics and pruning concept as utility
function that measures its preferences among the states and chooses the action that leads to the
best expected utility i.e. winning or tie condition for the computer system agent. The utility
function is taken as +10 for winning situation of maximizing agent or AI, -10 for winning
situation of minimizing agent or human player and 0 for draw condition.
CHAPTER 3: PROBLEM SPECIFICATION
2.2 Agent Environment Description
The basic environment for tic tac toe game agent is a 4x4 grid with 16 cells respectively with
opponent as human agent. The cell once occupied by a mark cannot be used again. The agent has
access to complete state of environment at any point and can detect all aspects that are relevant to
the choice of action making the environment fully observable. Also, the next state of
environment can be completely determined by current state and action executed which means
environment is deterministic. It is a two-player game with the opponent as human agent, so it is a
multi-agent environment. Beside the game environment has a finite number of states, percepts
and actions, making the environment static and discrete. Also, the environment is sequential
since the current choice of cell could affect all future decisions. Thus, the task environment for
the tic tac toe system agent is sequential, deterministic, fully observable, static, discrete and a
multi-agent.
CHAPTER 3: PROBLEM SPECIFICATION
Tic tac toe game has 16 cells for a 4x4 grid. The two players with their respective marks as ‘X’
and ‘O’ are required to place their marks in their turns one by one. Once the cell is occupied by a
mark it cannot be used again. The game is won if the agent is able to make a row or column or a
diagonal occupied completely with their respective marks. The game terminates once the winning
situation is gained or the cells are fully occupied. The problem specification for this game is given
below:
Problem: Given a 4x4 grid, the agents has to find the optimal cell to fill with respective marks.
Goals: To find the optimal cell to fill with respective marks and in order to win the game, the cell
must be filled such that one of the following criteria is satisfied:
1. A row is completely filled by a mark ‘X’ or ‘O’.
2. A diagonal is completely filled by a mark ‘X’ or ‘O’.
3. A column is completely filled by a mark ‘X’ or ‘O’.
If these criteria are not satisfied by both the agents, the game is terminated with a tie situation.
Constraints:
1. Once the cell is occupied by a mark, it cannot be reused.
2. Agents place the mark alternatively. So, consecutive moves from any agent is not allowed.
CHAPTER 4: STATE SPACE REPRESENATION
Figure 1: State Space Representation for a instance
CHAPTER 6: ALGORITHM USED
6.1 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. Its objective is to minimize the maximum loss. This
algorithm is based on adversarial search technique. In a normal search problem, the optimal
solution would be a sequence of actions leading to a goal state. Rather in adversarial search
MAX finds the contingent strategy, which specifies the MAX’s moves in the initial state, then
MAX’s moves in the states resulting from every possible response by MIN and continues till the
termination condition comes alternately. Furthermore, given a choice, MAX prefers to move to a
state of maximum value whereas MIN prefers a state of minimum value.
6.2 Alpha-Beta Pruning
The problem with minimax search is that the number of game states it has to examine is
exponential in the depth of the tree. The minimax algorithm recursively calls itself until any one
of the agent wins or the board is full which takes a lot of computation time and makes it
impossible to solve the 4X4 grid using standard minimax algorithm.
To solve this issue, we can use alpha-beta pruning algorithm which eliminates large parts of the
tree from considerations by pruning the tree. When applied to a standard minimax tree, it returns
the same move as minimax would, but it prunes away branches that cannot possibly influence the
final decision. Basically, this algorithm applies the principle that there is no use expending
search time to find out exactly how bad an alternative is if you have a better alternative. Alpha-
beta pruning gets its name from the parameters that bound on the backed-up values that appears
anywhere along the path:
α= the value of the best choices (i.e. highest value) so far at any choice point along the path for
MAX
β= the value of the best choice (i.e. lowest value) so far at any choice point along the path for MIN
CHAPTER 5: OUTCOME ANALYSIS
The minimax algorithm performs a complete depth-first exploration of the game tree. It
recursively calls itself until a winning state of any agent is found or the grid is full. If the
maximum depth of the tree is m and there are b legal moves at each point, then the time
complexity of the minimax algorithm is O (bm). The space complexity is O(bm) for generating all
actions at once. For 3X3 board, the possible number of moves is (3 * 3)! = 9! If the board size is
increased to 4X4 there is exponential growth and we experience an explosion of computational
time. This means we now get 16! Possible states which is an incredibly large amount. So, we can
estimate that computational time will be million times higher than 3X3 board. Hence, the time
cost is totally impractical, making this algorithm infeasible in 4X4 board, but it serves as the
basis for the mathematical analysis of games and for more practical algorithm.
To implement the minimax algorithm for 4X4 board we have applied the concept of alpha beta
pruning for eliminating the solutions which would not make impact on the final decision.
Considering above parameters the time complexity or alpha-beta pruning is O (bm/2) in best case
i.e. depth is reduced to half in the best possible scenario. Even though there is such huge
reduction in time, the time complexity is still very large considering our board. So, to decrease
the computation time, the search can be limited to certain level i.e. reduce the depth of search
tree. The maximum depth is taken as 9 in our system. But when reducing the depth of search
tree, the output is not optimal as we have cut-off 9 levels of our search tree which is a very large
number of nodes. Hence, we have used heuristic evaluation function that approximates the true
utility of a state without doing a complete search. For this problem, the heuristic function used is
given by:
E(n) = M(n) – O(n)
where, M(n) is total of possible winning path of AI
O(n) is total of possible winning path of human player
E(n) is total evaluation for state n
CHAPTER 5: OUTCOME ANALYSIS
This heuristic gives positive value if AI i.e. maximizing agent has more chance or winning and
negative value if the human player i.e. minimizing agent has more chance of winning.
With the basis of minimax algorithm for mathematical analysis alongside speeding up the
computation by alpha beta pruning concept and optimizing the utility function using heuristic
function, the 4x4 tic tac toe game was developed. We explored that a 4x4 tic tac toe, an
adversary search technique game in artificial intelligence, can be developed using these
techniques. Increasing the size of this game would create a huge time complexity issue with the
same algorithm and techniques, for which other logics must be further researched.
10
Background:
Major cities entered lock downs and quarantines as the coronavirus expanded throughout
the globe. These efforts slowed down the spread of the virus, however, they did not stop it from
becoming a pandemic. The pandemic forced governments to close businesses to ensure the
well-being of their citizens. This put industries in difficult financial situations because they
struggle to generate revenue. However, one industry does not share the same financial situation
as the others. The entertainment industry flourished despite the pandemic. Netflix’s stock went
up by 30% and gained millions of subscribers since the beginning of the quarantine. Video
games also benefited from the pandemic. CNBC wrote an article describing how video games
sales soar due to the pandemic. They also reported that Activision Blizzard’s Call of Duty:
Warzone gained 30 million players in 10 days because people try to socialize through online
based games. This shows that the entertainment industry remains strong even during the
quarantine.
10
Objective:
Team 7 did not want to miss the opportunity to join this flourishing industry, therefore,
the decision to join the market was made. The project’s objective is to develop a game by
implementing mechanical, electronical, and software engineering together. The game will also
utilize two microcontrollers to enhance user interaction and achieve better performance. The
game’s name is Tic Tac Toe and aims to be engaging and competitive so people can have fun
and forget all the craziness happening right now.
Methods & Plans:
Tic Tac Toe works by drawing Xs and Os in a 3x3 grid. Two players choose between one
of the given symbols (X or O) and then the game starts. Players take turns to draw their symbols,
but they can only draw once per turn. The player who first draws his/her respective symbol
horizontally, vertically, or diagonally wins the game. The following picture depicts how the 3x3
grid looks after a player won the game.
10
Tic Tac Toe is usually played on paper, however, this project aims to utilize different engineering
disciplines and combine them together. Therefore, the following materials are necessary:
● Arduino Uno
● Propeller Activity Board
● Push buttons
● LCD
● LEDs
● 7-Segment Display
The previously mentioned materials come together and follow the instructions given in the
codes to create the game. The game starts by randomly selecting who goes first. This is
accomplished by using the random function which is given minimum and maximum values (1
and 3 respectively). The random function chooses a value within the given parameters and places
it in the variable “initial”. The “initial” variable is used as a condition for an if statement that sets
up the LCD to display the proper message depending on who goes first. Then the players can
choose between 9 positions and the positions are visible in a 3x3 matrix. The 3x3 matrix consists
of 9 LEDs (initially turned off) and they are connected to the Arduino Uno.
Players can swap between different positions by using a push button and there is a second
push button that confirms the desired position. Both push buttons use pull up resistors which
invert their states and that is used as conditions in if statements. The first if statement says if the
button is pressed (low) add 1 to the iteration variable “num” and display it in the LCD. However,
“num” is reset to zero if it happens to go beyond 9. That guarantees that the players only see
numbers from 1 to 9. The second if statement says if the second push button is pressed (low) turn
on the LED with the same number as “num”. Additionally, 1 is subtracted from “num” within the
“winarray” to select a position (within the array) and change its value. The “winarray” was
initialized at the beginning of the code, but its main function comes later when triggering the
winning condition. This process occurs in the Arduino Uno, but the Propeller goes through a
similar process. The propeller is where player 2 inputs his desired position and follows similar
conditions to swap and confirm his desired position. This leads to the next point which is the
communication between the microcontrollers.
10
The communication between the two microcontrollers is possible through libraries.
Arduino Uno uses the “Software.Serial” library which allows digital pins to access serial
communication. It lets them receive and transmit information as if they were the RX and TX
pins. Similarly, the propeller uses the “fdserial” header to transmit and receive messages from
other microcontrollers. The Arduino possesses the variable “num2” to exclusively take input
from the Propeller and the Propeller has an exclusive variable named “num” to send the
information to the Arduino Uno. This is what allows player 2 to participate because player 2’s
input is taken only by the propeller.
The two players will continue to take turns and choose their desired positions until the
winning condition is triggered. The condition consists of 8 sub conditions that say the same three
symbols must be placed horizontally, vertically or diagonally to win the game. This is when the
“winarray” comes into play because it stores the symbols of player 1 and 2 on their chosen
positions. The LCD will display “Player 1 Wins” or “Player 2 Wins” depending on who met the
winning condition first. However, there is a possibility that there will be no winner and the game
ends up in a tie. This scenario is addressed by the following if statement that says if the variable
“turn” equals 10 then the LCD displays “It’s a tie”. “Turn” initially starts at 0, but 1 is added to
“turn” every time a player confirms the position they want to place their symbol. This means
“turn” is in the second if statements where players pressed the second push button.
Everything up to this point, except selecting which player goes first, is inside an if
statement. The if statement has the condition to end the game when the variable “over” becomes
true. “Over” is a boolean variable and its initial value is false, but the winning or tieing condition
changes that value to true. That ends the game and to play again, one must press the reset button
on the Arduino Uno. Additionally, the symbols are preselected for each player. Player 1 has the
static LEDs which present Xs while player 2 has the blinking LEDS that represent Os. This is
how players know their symbols during the game.
10
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tic Tac Toe</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<nav>
<ul>
<li>MyTicTacToe.com</li>
</ul>
</nav>
<div class="gameContainer">
<div class="container">
<div class="line"></div>
<div class="box bt-0 bl-0"><span class="boxtext"></span></div>
<div class="box bt-0"><span class="boxtext"></span></div>
<div class="box bt-0 br-0"><span class="boxtext"></span></div>
<div class="box bl-0"><span class="boxtext"></span></div>
<div class="box"><span class="boxtext"></span></div>
<div class="box br-0"><span class="boxtext"></span></div>
<div class="box bl-0 bb-0"><span class="boxtext"></span></div>
<div class="box bb-0"><span class="boxtext"></span></div>
10
<div class="box bb-0 br-0"><span class="boxtext"></span></div>
</div>
<div class="gameInfo">
<h1>Welcome to Tic Tac MyTicTacToe</h1>
<div>
<span class="info">Turn for X</span>
<button id="reset">Reset</button>
</div>
<div class="imgbox">
<img src="excited.gif" alt="" />
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
10
Reference:
[1] K. Kask. [Online]. Available: https://www.ics.uci.edu/~kkask/Fall-
2016%20CS271/slides/04-games.pdf. [Accessed 02 01 2020].
[2] G. Surma. [Online]. Available: https://towardsdatascience.com/tic-tac-toe-creating- unbeatable-ai-
with-minimax-algorithm-8af9e52c1e7d. [Accessed 20 12 2019].
[3] P. G. ,. P. S. P. Sunil Karamchandani, "A Simple Algorithm For Designing An Artificial Intelligence
Based Tic Tac Toe Game".
[4] 12 09 2019. [Online]. Available: https://www.edureka.co/blog/alpha-beta-pruning-in-ai.
[Accessed 20 11 2019].
10
Acknowledgement
It is indeed with a great sense of pleasure and immense sense of gratitude that I acknowledge
the help of these individuals which led to the successful completion of this mini-project.
I would like to thank our Head of the Department Dr. M. M. Dhobe for her support throughout
my project work.
I would like to thank our Guide Prof. S. A. Khusro for their support and advice while
completing this mini-project. Their guidance really helped me learn new things.
I am extremely great full to my department staff members and friends who helped me in the
successful completion of this mini-project. All the staff members really helped in every single
doubt which occurred while completion of this mini-project.
Rohan More 14
Aniket Sonawane 13
10
1
8