-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (77 loc) · 2.62 KB
/
app.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
const gameBoard = document.getElementById('game');
let gameElements = [];
let gameSquares = [0, 0, 0, 0, 0, 0, 0, 0, 0];
let gameOver = false;
for (let i of gameBoard.children) {
gameElements.push(i);
}
//Place X
for (let i = 0; i < gameElements.length; i++) {
gameElements[i].addEventListener("click", () => {
if (gameSquares[i] == 0 && gameOver == false) {
const span = document.createElement('span');
span.classList.add('cross');
gameElements[i].appendChild(span);
gameSquares[i] = 1;
checkWinner("Player 1");
compTurn();
}
});
}
//Computer places O
let compTurn = () => {
if (gameOver == false) {
let randNum = Math.floor(Math.random() * 8);
while (gameSquares[randNum] != 0) {
randNum = Math.floor(Math.random() * 8);
}
if (gameSquares[randNum] == 0) {
const span = document.createElement('span');
span.classList.add('circle');
gameElements[randNum].appendChild(span);
gameSquares[randNum] = 2;
checkWinner("Player 2");
}
}
}
//Check for winner (can refactor this)
let checkWinner = (winner) => {
if (gameSquares[0] != 0 && gameSquares[0] == gameSquares[1] && gameSquares[1] == gameSquares[2]) {
endGame(winner);
}
else if (gameSquares[3] != 0 && gameSquares[3] == gameSquares[4] && gameSquares[4] == gameSquares[5]) {
endGame(winner);
}
else if (gameSquares[6] != 0 && gameSquares[6] == gameSquares[7] && gameSquares[7] == gameSquares[8]) {
endGame(winner);
}
else if (gameSquares[0] != 0 && gameSquares[0] == gameSquares[3] && gameSquares[3] == gameSquares[6]) {
endGame(winner);
}
else if (gameSquares[1] != 0 && gameSquares[1] == gameSquares[4] && gameSquares[4] == gameSquares[7]) {
endGame(winner);
}
else if (gameSquares[2] != 0 && gameSquares[2] == gameSquares[5] && gameSquares[5] == gameSquares[8]) {
endGame(winner);
}
else if (gameSquares[0] != 0 && gameSquares[0] == gameSquares[4] && gameSquares[4] == gameSquares[8]) {
endGame(winner);
}
else if (gameSquares[2] != 0 && gameSquares[2] == gameSquares[4] && gameSquares[4] == gameSquares[6]) {
endGame(winner);
}
}
//Actions to take once the game ends
let endGame = (winner) => {
gameOver = true;
console.log(`Winner is: ${winner}!`);
alert(`Winner is: ${winner}!`);
}
//Reset game
let resetGame = () => {
gameSquares = [0, 0, 0, 0, 0, 0, 0, 0, 0];
for (let i of gameElements) {
i.replaceChildren();
}
gameOver = false;
}