-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
129 lines (110 loc) · 3.64 KB
/
main.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
const turnA = "❌"
const turnB = "❤️"
const gameOverBoad = "rgb(255 159 159 / 51%)"
const board = document.querySelectorAll('.board')
const gameOverEl = document.getElementById("gameOver");
const player = document.getElementById("player");
const popTextCongo = document.getElementById("pop-text-congo");
const tie = new Audio('./assets/tie.mp3')
const playerClicked = new Audio('./assets/playerClicked.mp3')
const error = new Audio('./assets/error.mp3')
const playerWin = new Audio('./assets/playerWin.mp3')
let isGameOver = false;
let count = 0;
for (let i = 0; i < 9; i++) {
board[i].addEventListener('click', () => { turn(i) })
}
const turn = (i) => {
if (board[i].innerText == "" && isGameOver == false) {
playerClicked.play();
if (count % 2 == 0)
board[i].innerText = turnA;
else
board[i].innerText = turnB;
count++;
if (count >= 5) {
xWinValue = checkWin(turnA)
oWinValue = checkWin(turnB)
if (xWinValue != -1) {
gameOver(xWinValue);
playerWin.play();
}
else if (oWinValue != -1) {
gameOver(oWinValue);
playerWin.play();
}
else if (count == 9) {
tie.play()
popTextCongo.innerText = "Tie"
gameOver();
}
}
}
if (isGameOver == true) {
error.play();
}
}
const gameOver = (win = 0) => {
gameOverEl.style.display = "flex";
player.innerText = win;
isGameOver = true
}
const changeBoardColor = (i, j, k) => {
board[i].style.backgroundColor = gameOverBoad;
board[j].style.backgroundColor = gameOverBoad;
board[k].style.backgroundColor = gameOverBoad;
}
const checkWin = (e) => {
// Horizontal
if (board[0].innerText == board[1].innerText && board[1].innerText == board[2].innerText && board[2].innerText == e) {
changeBoardColor(0, 1, 2);
return e;
}
else if (board[3].innerText == board[4].innerText && board[4].innerText == board[5].innerText && board[5].innerText == e) {
changeBoardColor(3, 4, 5);
return e;
}
else if (board[6].innerText == board[7].innerText && board[7].innerText == board[8].innerText && board[8].innerText == e) {
changeBoardColor(6, 7, 8);
return e;
}
// Vertical
else if (board[0].innerText == board[3].innerText && board[3].innerText == board[6].innerText && board[6].innerText == e) {
changeBoardColor(0, 3, 6);
return e;
}
else if (board[1].innerText == board[4].innerText && board[4].innerText == board[7].innerText && board[7].innerText == e) {
changeBoardColor(1, 4, 7);
return e;
}
else if (board[2].innerText == board[5].innerText && board[5].innerText == board[8].innerText && board[8].innerText == e) {
changeBoardColor(2,5,8);
return e;
}
// Diagonally
else if (board[0].innerText == board[4].innerText && board[4].innerText == board[8].innerText && board[8].innerText == e) {
changeBoardColor(0, 4, 8);
return e;
}
else if (board[2].innerText == board[4].innerText && board[4].innerText == board[6].innerText && board[6].innerText == e) {
changeBoardColor(2, 4, 6);
return e;
}
else
return -1;
}
function restart() {
location.reload();
}
function replay() {
count = 0;
isGameOver = false;
gameOverEl.style.display = "none";
for (let i = 0; i < 9; i++) {
board[i].innerText = "";
board[i].style.backgroundColor = "";
}
}
function cross() {
gameOverEl.style.display = "none";
}