-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
259 lines (241 loc) · 7.37 KB
/
index.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const player = "X";
const computer = "O";
let board_full = false;
let play_board = ["", "", "", "", "", "", "", "", ""];
const board_container = document.querySelector(".play-area");
const winner_statement = document.getElementById("winner");
check_board_complete = () => {
let flag = true;
play_board.forEach(element => {
if (element != player && element != computer) {
flag = false;
}
});
board_full = flag;
};
// helper function used in check_line() to mark the winning blocks
const win_line = (winLine) => {
for(i=0;i<3;i++){
document.querySelector(`#block_${winLine[i]}`).classList.add("win-block");
}
}
// helper function used in check_match() functon
const check_line = (a, b, c) => {
var res = (
play_board[a] == play_board[b] &&
play_board[b] == play_board[c] &&
(play_board[a] == player || play_board[a] == computer)
);
return res;
};
const check_match = () => {
for (i = 0; i < 9; i += 3) {
if (check_line(i, i + 1, i + 2)) {
return [play_board[i],[i, i + 1, i + 2]];
}
}
for (i = 0; i < 3; i++) {
if (check_line(i, i + 3, i + 6)) {
return [play_board[i],[i, i + 3, i + 6]];
}
}
if (check_line(0, 4, 8)) {
return [play_board[0],[0, 4, 8]];
}
if (check_line(2, 4, 6)) {
return [play_board[2],[2, 4, 6]];
}
return ["",[-1,-1,-1]];
};
const check_for_winner = () => {
let res = check_match()
if (res[0] == player) {
winner.innerText = "Player Won!!";
winner.classList.add("playerWin");
win_line(res[1]);
board_full = true
// Confetti Code here
var confettiElement = document.getElementById('my-canvas');
var confettiSettings = { target: confettiElement };
var confetti = new ConfettiGenerator(confettiSettings);
confetti.render();
setTimeout(() => {confetti.clear()}, 3000); // clearing after 3
} else if (res[0] == computer) {
winner.innerText = "Computer Won";
winner.classList.add("computerWin");
win_line(res[1]);
board_full = true
} else if (board_full) {
winner.innerText = "It's a Draw!";
winner.classList.add("draw");
}
};
const render_board = () => {
board_container.innerHTML = ""
play_board.forEach((e, i) => {
board_container.innerHTML += `<div id="block_${i}" class="block" onclick="addPlayerMove(${i})">${play_board[i]}</div>`
if (e == player || e == computer) {
document.querySelector(`#block_${i}`).classList.add("occupied");
}
});
};
const game_loop = () => {
render_board();
check_board_complete();
check_for_winner();
}
const addPlayerMove = e => {
if (!board_full && play_board[e] == "") {
play_board[e] = player;
game_loop();
addComputerMove();
}
};
const addComputerMove = () => {
if (!board_full) {
selected = computeMoveAlphaBeta(play_board)[1]
play_board[selected] = computer;
game_loop();
}
};
// Artificial Intelligence based MiniMax Algorithm
const computeMoveMiniMax = (play_board, depth = 0, isComputer = true) => {
let res = check_match()
let bestScore;
let bestMove;
let possibleMoves
if (res[0] == player) {
return [-10 + depth, null]
}
else if (res[0] == computer) {
return [10 - depth, null]
}
check_board_complete();
if (board_full) {
return [0, null]
}
else {
if (isComputer) {
bestScore = -9
possibleMoves = []
for (i = 0; i < play_board.length; i++) {
if (play_board[i] == "") {
possibleMoves.push(i);
}
}
possibleMoves.forEach((move) => {
play_board[move] = computer;
score = computeMoveMiniMax(play_board, depth + 1, false)[0]
if (score > bestScore) {
bestScore = score;
bestMove = move;
}
play_board[move] = "";
});
return [bestScore, bestMove]
} else {
bestScore = 9
possibleMoves = []
for (i = 0; i < play_board.length; i++) {
if (play_board[i] == "") {
possibleMoves.push(i);
}
}
possibleMoves.forEach((move) => {
play_board[move] = player;
score = computeMoveMiniMax(play_board, depth + 1, true)[0]
if (score < bestScore) {
bestScore = score;
bestMove = move;
}
play_board[move] = "";
})
return [bestScore, bestMove]
}
}
}
// Artificial Intelligence based Alpha-Beta-pruning Algorithm
const computeMoveAlphaBeta = (play_board, depth = 0, alpha = -Infinity, beta = +Infinity, isComputer = true) => {
let res = check_match()
let bestScore;
let bestMove;
let possibleMoves
if (res[0] == player) {
return [-10 + depth, null]
}
else if (res[0] == computer) {
return [10 - depth, null]
}
check_board_complete();
if (board_full) {
return [0, null]
}
else {
if (isComputer) {
bestScore = -9
possibleMoves = []
for (i = 0; i < play_board.length; i++) {
if (play_board[i] == "") {
possibleMoves.push(i);
}
}
possibleMoves.forEach((move) => {
play_board[move] = computer;
score = computeMoveAlphaBeta(play_board, depth + 1,alpha,beta, false)[0]
// alpha = max(alpha, score)
if(score>alpha){
alpha=score;
}
if (score > bestScore) {
bestScore = score;
bestMove = move;
}
play_board[move] = "";
if (beta < alpha) {
return 0,0;
}
});
return [bestScore, bestMove]
} else {
bestScore = 9
possibleMoves = []
for (i = 0; i < play_board.length; i++) {
if (play_board[i] == "") {
possibleMoves.push(i);
}
}
possibleMoves.forEach((move) => {
play_board[move] = player;
score = computeMoveAlphaBeta(play_board, depth + 1,alpha,beta ,true)[0]
if(score<beta){
beta=score
}
if (score < bestScore) {
bestScore = score;
bestMove = move;
}
play_board[move] = "";
if(beta<=alpha){
return 0,0;
}
})
return [bestScore, bestMove]
}
}
}
const reset_board = () => {
play_board = ["", "", "", "", "", "", "", "", ""];
board_full = false;
winner.classList.remove("playerWin");
winner.classList.remove("computerWin");
winner.classList.remove("draw");
winner.innerText = "";
render_board();
};
//initial render
render_board();
//dark mode
const myFunction = ()=> {
var element = document.body;
element.classList.toggle("dark");
}