-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
217 lines (173 loc) · 5.6 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
let player = 'X';
let currentMainBox;
let prevMainBox = 0;
let subGameBoard = generateGameBoard();
let mainGameBoard = generateGameBoard();
let mainBoxesMap = {
"00" : 0,
"01" : 0,
"02" : 0,
"03" : 0,
"04" : 0,
"05" : 0,
"06" : 0,
"07" : 0,
"08" : 0
};
function generateGameBoard() {
let gameTable = [];
for(let i = 0; i < 3; i++)
gameTable[i] = [];
return gameTable;
}
function changePlayer() {
player === 'X' ? player = 'O' : player = 'X';
}
function checkWinner(board) {
if(board[0][0] !== undefined && board[0][0] !== '=' && board[0][0] === board[0][1] && board[0][1] === board[0][2])
return true;
if(board[1][0] !== undefined && board[1][0] !== '=' && board[1][0] === board[1][1] && board[1][1] === board[1][2])
return true;
if(board[2][0] !== undefined && board[2][0] !== '=' && board[2][0] === board[2][1] && board[2][1] === board[2][2])
return true;
if(board[0][0] !==undefined && board[0][0] !== '=' && board[0][0] === board[1][0] && board[1][0] === board[2][0])
return true;
if(board[0][1] !== undefined && board[0][1] !== '=' && board[0][1] === board[1][1] && board[1][1] === board[2][1])
return true;
if(board[0][2] !== undefined && board[0][2] !== '=' && board[0][2] === board[1][2] && board[1][2] === board[2][2])
return true;
if(board[0][0] !== undefined && board[0][0] !== '=' && board[0][0] === board[1][1] && board[1][1] === board[2][2])
return true;
return (board[2][0] !== undefined && board[2][0] !== '=' && board[2][0] === board[1][1] && board[1][1] === board[0][2]);
}
function draw(id) {
drawXO(id, 0,"40px", "40px");
evaluateSubGame(id);
evaluateMainGame();
changePlayer();
}
function stalemate(board) {
let cnt = 0;
for(let i = 0; i < board.length; i++)
for(let j = 0; j < board.length; j++){
if(board[i][j] === 'X' || board[i][j] === 'O' || board[i][j] === '=')
cnt++;
}
return cnt === 9;
}
function evaluateSubGame(id) {
let parent = document.getElementById(id).parentNode;
currentMainBox = parent.id;
if(currentMainBox !== prevMainBox){
subGameBoard = generateGameBoard();
prevMainBox = currentMainBox;
}
lockSubGame(parent);
subGameBoard[parseInt(id[1])][parseInt(id[2])] = player;
if(checkWinner(subGameBoard)) {
mainGameBoard[parseInt(parent.id[0])][parseInt(parent.id[1])] = player;
addCss(parent, 1);
unlockSubGame();
} else if(stalemate(subGameBoard)){
mainGameBoard[parseInt(parent.id[0])][parseInt(parent.id[1])] = '=';
addCss(parent, 2);
unlockSubGame();
}
console.log(subGameBoard);
}
function drawXO(id, flag, height, width) {
let div = document.getElementById(id);
if(div.innerHTML !== '' && flag === 0) // check if the box is populated
return;
div.style.pointerEvents = "none";
let img = document.createElement("img"); //draw image in box
img.style.height = height;
img.style.width = width;
div.innerHTML = '';
(player === 'X') ? img.src = "images/cross.png" : img.src = "images/circle.png";
if(flag === 2) {
img.src = "images/equal.png";
}
div.style.boxShadow = "inset 5px 5px 10px #7b2828,\n" +
" inset -5px -5px 10px #b93c3c";
div.appendChild(img);
}
function addCss(parent, flag) {
parent.style.pointerEvents = "none";
parent.style.display = "flex";
parent.style.alignItems = "center";
parent.style.justifyContent = "center";
// parent.style.borderStyle = "inset";
drawXO(currentMainBox, flag,"120px", "120px");
}
function lockSubGame(current) { //this function is responsible for removing all click events from other boxes
let box;
for(let i = 0; i < 3; i++)
for(let j = 0; j < 3; j++) {
box = document.getElementById(String(i) + String(j));
if(box.id === current.id){
mainBoxesMap[current.id] = 1;
continue;
}
box.style.pointerEvents = "none";
}
}
function unlockSubGame() {
let box;
for(let i = 0; i < 3; i++)
for(let j = 0; j < 3; j++) {
box = document.getElementById(String(i) + String(j));
if(mainBoxesMap[box.id] === 1) {
continue;
}
box.style.pointerEvents = "";
}
}
function evaluateMainGame() {
checkEarlyWinner(mainGameBoard);
if(checkWinner(mainGameBoard)){
lockSubGame(currentMainBox);
console.log(player);
} else if(stalemate(mainGameBoard))
console.log(checkSpecialWinner(mainGameBoard));
}
function checkSpecialWinner(board) {
let X = 0;
let O = 0;
for(let i = 0; i < 3; i++)
for(let j = 0; j < 3; j++) {
if(board[i][j] === 'X')
X++;
if(board[i][j] === 'O')
O++;
}
if(X > O)
return "X";
if(O > X)
return "O";
if(X === O)
return "=";
}
function checkEarlyWinner(board) {
let X = 0;
let O = 0;
let equal = 0;
let freeBoxes;
for(let i = 0; i < 3; i++)
for(let j = 0; j < 3; j++) {
if(board[i][j] === 'X')
X++;
if(board[i][j] === 'O')
O++;
if(board[i][j] === '=')
equal++;
}
freeBoxes = 9 - (X + O + equal);
if(X + equal > O + freeBoxes + equal) {
console.log("X");
lockSubGame(currentMainBox);
} else if(O + equal > X + freeBoxes + equal) {
console.log("O");
lockSubGame(currentMainBox);
}
}