-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
224 lines (190 loc) · 7.19 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
212
213
214
215
216
217
218
219
220
221
222
223
224
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => {
cell.addEventListener('click', cellClicked);
});
// initialize current player
let currentPlayer = 'X';
function cellClicked(e) {
// if gamemode has been selected
if (gameModeSelected === true) {
if (e.target.textContent !== ' ') {
alert('This cell is already filled');
}
else if (hasWinner === false) {
// Fill the cell with the current player X or O
e.target.textContent = currentPlayer;
// Change the current player
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
// Change the current player on game info
document.getElementById('current-player').textContent = "Current Player: " + currentPlayer;
// Check if the game is over
checkWinner(false);
// if single player game
if (singlePlayer === true && currentPlayer === 'O' && hasWinner === false) {
// computer plays
computerPlay();
}
}
}
}
var winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
var hasWinner = false;
// check if there is a winner
function checkWinner(checkingMove) {
// winner if there is a match in the winning combinations
for (let i = 0; i < winningCombinations.length; i++) {
const [a, b, c] = winningCombinations[i];
if (cells[a].textContent === cells[b].textContent && cells[b].textContent === cells[c].textContent && cells[a].textContent !== ' ') {
hasWinner = true;
// animateWinningCells();
if (checkingMove === false) {
animateWinningCells(a, b, c);
}
// change current-player to "Player X" or "Player O won"
document.getElementById('current-player').textContent = `Player ${cells[a].textContent} won!`;
break;
}
// if there is no winner
else if (cells[0].textContent !== ' ' && cells[1].textContent !== ' ' && cells[2].textContent !== ' ' && cells[3].textContent !== ' ' && cells[4].textContent !== ' ' && cells[5].textContent !== ' ' && cells[6].textContent !== ' ' && cells[7].textContent !== ' ' && cells[8].textContent !== ' ') {
hasWinner = true;
document.getElementById('current-player').textContent = `It's a tie!`;
break;
}
else {
hasWinner = false;
}
}
}
// animate on the winning cells
function animateWinningCells(a, b, c) {
let i = 0;
const intervalId = setInterval(() => {
if (i >= 3) {
clearInterval(intervalId);
cells[a].classList.add('green');
cells[b].classList.add('green');
cells[c].classList.add('green');
return;
}
cells[a + (b - a) * i].classList.add('green');
i++;
}, 500);
}
// Reset the game
const resetButton = document.getElementById('reset-button');
resetButton.addEventListener('click', () => {
for (const cell of cells) {
cell.textContent = ' ';
cell.classList.remove('green');
}
// Reset other game state variables
currentPlayer = 'X';
document.getElementById('current-player').textContent = "Current Player: " + 'X';
hasWinner = false;
// promt user to select gamemode
gameModeSelected = false;
document.getElementById("popup").style.display = "block";
});
// cant click on cells if gamemode has not been selected
let gameModeSelected = false;
// show popup when the page loads
window.onload = function () {
document.getElementById("popup").style.display = "block";
}
// hide popup when the user clicks on the button
function hidePopup() {
document.getElementById("popup").style.display = "none";
gameModeSelected = true;
}
// if single player button is clicked
function singlePlayerButton() {
hidePopup();
singlePlayer = true;
}
// if two player button is clicked
function twoPlayerButton() {
hidePopup();
singlePlayer = false;
}
// computer plays randomly
// function computerPlay () {
// if (singlePlayer === true) {
// // wait for 1 second before computer plays
// setInterval(() => {
// if (currentPlayer === 'O' && hasWinner === false) {
// // randomly select a cell that is not filled
// let randomCell = Math.floor(Math.random() * 9);
// while (cells[randomCell].textContent !== ' ' && singlePlayer === true) {
// randomCell = Math.floor(Math.random() * 9);
// }
// if (singlePlayer === true) {
// cells[randomCell].textContent = currentPlayer;
// currentPlayer = 'X'
// document.getElementById('current-player').textContent = "Current Player: " + currentPlayer;
// checkWinner();
// }
// }
// }, 1000);
// }
// }
// computer plays best move
function computerPlay () {
if (singlePlayer === true) {
// look at the board and find what moves can be made
let availableMoves = [];
for (let i = 0; i < cells.length; i++) {
if (cells[i].textContent === ' ') {
availableMoves.push(i);
}
}
// console log the available moves
console.log(availableMoves);
// look at the available moves and find if there is a winning move
// if there is a winning move, make that move
for (let i = 0; i < availableMoves.length; i++) {
let move = availableMoves[i];
cells[move].textContent = currentPlayer;
checkingMove = true;
checkWinner(checkingMove);
if (hasWinner === true) {
cells[move].textContent = currentPlayer;
currentPlayer = 'X'
document.getElementById('current-player').textContent = "Current Player: " + currentPlayer;
checkWinner(false);
return;
}
cells[move].textContent = ' ';
}
// look at the available moves and find if there is a blocking move
// if there is a blocking move, make that move
for (let i = 0; i < availableMoves.length; i++) {
let move = availableMoves[i];
cells[move].textContent = 'X';
checkingMove = true;
checkWinner(checkingMove);
if (hasWinner === true) {
cells[move].textContent = currentPlayer;
currentPlayer = 'X'
document.getElementById('current-player').textContent = "Current Player: " + currentPlayer;
checkWinner(false);
return;
}
cells[move].textContent = ' ';
}
// if there is no winning move or blocking move, make a random move
let randomCell = Math.floor(Math.random() * availableMoves.length);
cells[availableMoves[randomCell]].textContent = currentPlayer;
currentPlayer = 'X'
document.getElementById('current-player').textContent = "Current Player: " + currentPlayer;
checkWinner(false);
}
}