CPP Sarthak
CPP Sarthak
A PROJECT REPORT ON
“Tic-Tac-Toe Game”
-Submitted To -
2023-2024
-Submitted By-
1|Page
Table of Contents
2|Page
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 makesthe situation adversarial, hence requiring adversarial search supported
by minimax algorithm with alpha beta pruning concept in artificial intelligence
1.1 OBJECTIVES
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
4. To study optimizing methods for alpha-beta pruning using heuristic evaluation function.
3|Page
CHAPTER 2: 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.
4|Page
2.3 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.
5|Page
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:
If these criteria are not satisfied by both the agents, the game is terminated with a tie
situation.
6|Page
CHAPTER 4: STATE SPACE REPRESENATION
7|Page
CHAPTER 5: ALGORITHM USED
8|Page
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
9|Page
CHAPTER 6: OUTCOME ANALYSIS
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
generating all actions at once. For 3X3 board, the possible number of
moves is (3 * 3)!
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.
10 | P a g e
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
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
without doing a complete search. For this problem, the heuristic function
This heuristic gives positive value if AI i.e. maximizing agent has more
12 | P a g e
CHAPTER 7: coding implementation
index.html
<!DOCTYPE html>
<html>
<head>
"width=device-width, initial-scale=1.0">
<script src="game.js"></script>
</head>
<body>
<div id="main">
<p id="ins">
13 | P a g e
Game starts by just Tap on
<b>Player 0</b>
</p>
<br><br>
<div class="row">
readonly>
readonly>
onclick="myfunc_5(); myfunc();"
readonly>
</div>
14 | P a g e
<div class="row">
readonly>
readonly>
readonly>
</div>
<div class="row">
readonly>
class="cell" onclick="myfunc_10();myfunc();"
readonly>
15 | P a g e
<input type="text" id= "b9"
class="cell" onclick="myfunc_11();myfunc();"
readonly>
</div>
</div>
<br><br><br>
<br><br>
<p id="print"></p>
</div>
</body>
</html>
game.js
16 | P a g e
// Function called whenever user tab on any box
function myfunc() {
var b1, b2, b3, b4, b5, b6, b7, b8, b9;
b1 = document.getElementById("b1").value;
b2 = document.getElementById("b2").value;
b3 = document.getElementById("b3").value;
b4 = document.getElementById("b4").value;
b5 = document.getElementById("b5").value;
b6 = document.getElementById("b6").value;
b7 = document.getElementById("b7").value;
b8 = document.getElementById("b8").value;
b9 = document.getElementById("b9").value;
17 | P a g e
b1btn = document.getElementById("b1");
b2btn = document.getElementById("b2");
b3btn = document.getElementById("b3");
b4btn = document.getElementById("b4");
b5btn = document.getElementById("b5");
b6btn = document.getElementById("b6");
b7btn = document.getElementById("b7");
b8btn = document.getElementById("b8");
b9btn = document.getElementById("b9");
document.getElementById('print')
b4btn.disabled = true;
b5btn.disabled = true;
18 | P a g e
b6btn.disabled = true;
b7btn.disabled = true;
b8btn.disabled = true;
b9btn.disabled = true;
b1btn.style.color = "red";
b2btn.style.color = "red";
b3btn.style.color = "red";
document.getElementById('print')
b2btn.disabled = true;
b3btn.disabled = true;
b5btn.disabled = true;
b6btn.disabled = true;
b8btn.disabled = true;
19 | P a g e
b9btn.disabled = true;
b1btn.style.color = "red";
b4btn.style.color = "red";
b7btn.style.color = "red";
document.getElementById('print')
b1btn.disabled = true;
b2btn.disabled = true;
b3btn.disabled = true;
b4btn.disabled = true;
b5btn.disabled = true;
b6btn.disabled = true;
20 | P a g e
b7btn.style.color = "red";
b8btn.style.color = "red";
b9btn.style.color = "red";
document.getElementById('print')
b1btn.disabled = true;
b2btn.disabled = true;
b4btn.disabled = true;
b5btn.disabled = true;
b7btn.disabled = true;
b8btn.disabled = true;
b3btn.style.color = "red";
b6btn.style.color = "red";
21 | P a g e
b9btn.style.color = "red";
document.getElementById('print')
b2btn.disabled = true;
b3btn.disabled = true;
b4btn.disabled = true;
b6btn.disabled = true;
b7btn.disabled = true;
b8btn.disabled = true;
b1btn.style.color = "red";
b5btn.style.color = "red";
b9btn.style.color = "red";
document.getElementById('print')
b1btn.disabled = true;
b2btn.disabled = true;
b4btn.disabled = true;
b6btn.disabled = true;
b8btn.disabled = true;
b9btn.disabled = true;
b3btn.style.color = "red";
b5btn.style.color = "red";
b7btn.style.color = "red";
document.getElementById('print')
b2btn.disabled = true;
b4btn.disabled = true;
b6btn.disabled = true;
b7btn.disabled = true;
b9btn.disabled = true;
b2btn.style.color = "red";
b5btn.style.color = "red";
b8btn.style.color = "red";
document.getElementById('print')
b1btn.disabled = true;
b2btn.disabled = true;
b3btn.disabled = true;
24 | P a g e
b7btn.disabled = true;
b8btn.disabled = true;
b9btn.disabled = true;
b4btn.style.color = "red";
b5btn.style.color = "red";
b6btn.style.color = "red";
document.getElementById('print')
b4btn.disabled = true;
b5btn.disabled = true;
25 | P a g e
b6btn.disabled = true;
b7btn.disabled = true;
b8btn.disabled = true;
b9btn.disabled = true;
b1btn.style.color = "red";
b2btn.style.color = "red";
b3btn.style.color = "red";
document.getElementById('print')
b2btn.disabled = true;
b3btn.disabled = true;
b5btn.disabled = true;
b6btn.disabled = true;
b8btn.disabled = true;
26 | P a g e
b9btn.disabled = true;
b1btn.style.color = "red";
b4btn.style.color = "red";
b7btn.style.color = "red";
document.getElementById('print')
b1btn.disabled = true;
b2btn.disabled = true;
b3btn.disabled = true;
b4btn.disabled = true;
b5btn.disabled = true;
b6btn.disabled = true;
b7btn.style.color = "red";
27 | P a g e
b8btn.style.color = "red";
b9btn.style.color = "red";
document.getElementById('print')
b1btn.disabled = true;
b2btn.disabled = true;
b4btn.disabled = true;
b5btn.disabled = true;
b7btn.disabled = true;
b8btn.disabled = true;
b3btn.style.color = "red";
b6btn.style.color = "red";
b9btn.style.color = "red";
document.getElementById('print')
b2btn.disabled = true;
b3btn.disabled = true;
b4btn.disabled = true;
b6btn.disabled = true;
b7btn.disabled = true;
b8btn.disabled = true;
b1btn.style.color = "red";
b5btn.style.color = "red";
b9btn.style.color = "red";
document.getElementById('print')
b2btn.disabled = true;
b4btn.disabled = true;
b6btn.disabled = true;
b8btn.disabled = true;
b9btn.disabled = true;
b3btn.style.color = "red";
b5btn.style.color = "red";
b7btn.style.color = "red";
document.getElementById('print')
b1btn.disabled = true;
b3btn.disabled = true;
b4btn.disabled = true;
30 | P a g e
b6btn.disabled = true;
b7btn.disabled = true;
b9btn.disabled = true;
b2btn.style.color = "red";
b5btn.style.color = "red";
b8btn.style.color = "red";
document.getElementById('print')
b1btn.disabled = true;
b2btn.disabled = true;
b3btn.disabled = true;
b7btn.disabled = true;
b8btn.disabled = true;
b9btn.disabled = true;
31 | P a g e
b4btn.style.color = "red";
b5btn.style.color = "red";
b6btn.style.color = "red";
document.getElementById('print')
else {
32 | P a g e
// Here, Printing Result
if (flag == 1) {
document.getElementById('print')
else {
document.getElementById('print')
function myfunc_2() {
location.reload();
b1 = b2 = b3 = b4 = b5 = b6 = b7 = b8 = b9 = '';
}
33 | P a g e
// Here onwards, functions check turn of the player
flag = 1;
function myfunc_3() {
if (flag == 1) {
document.getElementById("b1").value = "X";
document.getElementById("b1").disabled = true;
flag = 0;
else {
document.getElementById("b1").value = "0";
document.getElementById("b1").disabled = true;
flag = 1;
function myfunc_4() {
34 | P a g e
if (flag == 1) {
document.getElementById("b2").value = "X";
document.getElementById("b2").disabled = true;
flag = 0;
else {
document.getElementById("b2").value = "0";
document.getElementById("b2").disabled = true;
flag = 1;
function myfunc_5() {
if (flag == 1) {
document.getElementById("b3").value = "X";
document.getElementById("b3").disabled = true;
flag = 0;
}
35 | P a g e
else {
document.getElementById("b3").value = "0";
document.getElementById("b3").disabled = true;
flag = 1;
function myfunc_6() {
if (flag == 1) {
document.getElementById("b4").value = "X";
document.getElementById("b4").disabled = true;
flag = 0;
else {
document.getElementById("b4").value = "0";
document.getElementById("b4").disabled = true;
flag = 1;
}
36 | P a g e
}
function myfunc_7() {
if (flag == 1) {
document.getElementById("b5").value = "X";
document.getElementById("b5").disabled = true;
flag = 0;
else {
document.getElementById("b5").value = "0";
document.getElementById("b5").disabled = true;
flag = 1;
function myfunc_8() {
if (flag == 1) {
document.getElementById("b6").value = "X";
37 | P a g e
document.getElementById("b6").disabled = true;
flag = 0;
else {
document.getElementById("b6").value = "0";
document.getElementById("b6").disabled = true;
flag = 1;
function myfunc_9() {
if (flag == 1) {
document.getElementById("b7").value = "X";
document.getElementById("b7").disabled = true;
flag = 0;
else {
document.getElementById("b7").value = "0";
38 | P a g e
document.getElementById("b7").disabled = true;
flag = 1;
function myfunc_10() {
if (flag == 1) {
document.getElementById("b8").value = "X";
document.getElementById("b8").disabled = true;
flag = 0;
else {
document.getElementById("b8").value = "0";
document.getElementById("b8").disabled = true;
flag = 1;
39 | P a g e
function myfunc_11() {
if (flag == 1) {
document.getElementById("b9").value = "X";
document.getElementById("b9").disabled = true;
flag = 0;
else {
document.getElementById("b9").value = "0";
document.getElementById("b9").disabled = true;
flag = 1;
Game.css
h1 {
color: orangered;
margin-bottom: -5px;
}
40 | P a g e
p{
margin-bottom: -10px;
.ui {
display: flex;
flex-direction: column;
align-items: center;
.row {
display: flex;
.cell {
border: none;
width: 80px;
height: 80px;
display: flex;
align-items: center;
justify-content: center;
41 | P a g e
font-size: 24px;
text-align: center;
cursor: pointer;
.cell:active {
outline: none;
/* 3*3 Grid */
#b1{
#b2 {
}
42 | P a g e
#b3 {
#b4 {
#b5 {
#b6 {
#b7 {
#b8 {
#b9 {
/* Reset Button */
#but {
box-sizing: border-box;
width: 95px;
height: 40px;
margin-left: auto;
border-radius: 8px;
font-family: Verdana,
background-color: whitesmoke;
color: dodgerblue;
font-size: 20px;
cursor: pointer;
45 | P a g e
/* Player turn space */
#print {
font-family: Verdana,
color: dodgerblue;
font-size: 20px;
/* Main Container */
#main {
text-align: center;
#ins {
font-family: Verdana,Geneva,
Tahoma, sans-serif;
color: dodgerblue; }
46 | P a g e
Final output
47 | P a g e
48 | P a g e
49 | P a g e
DESCRIPTION:
playing the game and both players make their moves in consecutive
turns. The player who makes a straight 3-block chain wins the game.
This game is built on the front-end using simple logic and validation
checks only.
Project Structure:
• index.html
• game.css
• game.js
index.html: This file contains the structure of the project. game.css: This
file contains the styling of the application. game.js: This file contains the
51 | P a g e
References
[Accessed 20 12 2019].
beta-pruning-in-ai. [Accessed
20 11 2019].
52 | P a g e