-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
116 lines (101 loc) · 3.06 KB
/
ui.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
//const parse = require('./TicTacToe.js');
const grid = document.querySelector('.board');
const overlay = document.querySelector('.overlay');
const winner = document.querySelector('.winner');
const player1 = document.querySelector('.player1');
const player2 = document.querySelector('.player2');
const game = new TicTacToe();
let volume = 0.8;
const playsound = function(id){
link = `./sounds/${id}.wav`;
const audio = document.createElement('audio');
audio.src = link;
audio.volume = volume;
audio.play();
};
grid.addEventListener('click' , (e) => {
if (e.target.nodeName == 'LI' || e.target.parentNode.nodeName == 'LI'){
const location = e.target.id || e.target.parentNode.id ;
console.log(location);
try {
game.update(location);
updateUI(location,game.turn);
playsound('click');
game.changeTurn();
handlewin(game.checkWin());
updateIndicators(game.turn);
} catch (error) {
console.log(error);
// error handling here
}
}
});
const updateUI = (location , turn) => {
const li = document.getElementById(location);
const img = li.children[0];
const filename = turn.toLowerCase();
img.src = `./img/${filename}.svg`;
//unhide the image
img.classList.remove("hidden");
//remove hover effects once cell is filled
li.classList.remove("hover:scale-105");
li.classList.remove("hover:bg-green-300");
//add color depending on x or o;
li.classList.remove("bg-cyan-300");
if (turn == 'X'){
li.classList.add("bg-amber-300");
} else {
li.classList.add("bg-rose-300");
}
};
const updateIndicators = (turn) => {
if (turn == 'X'){
player1.classList.add('scale-110');
player2.classList.remove('scale-110');
player1.classList.add('shadow-white');
player2.classList.remove('shadow-white');
} else {
player2.classList.add('scale-110');
player1.classList.remove('scale-110');
player2.classList.add('shadow-white');
player1.classList.remove('shadow-white');
}
};
const handlewin = (win) => {
if (win === 'X'){
displayOverlay(win);
stopGame();
} else if (win === 'O'){
displayOverlay(win);
stopGame();
} else if (win === 'Draw'){
displayOverlay(win);
stopGame();
} else {
win = game.checkWin();
}
}
const stopGame = () => {
grid.addEventListener("click", (e) => {
e.stopPropagation();
e.preventDefault();
} );
};
const displayOverlay = (win) => {
let text = '';
if (win == 'X'){
text = 'Player 1 Wins! 🏆';
playsound('win');
} else if (win == 'O'){
text='Player 2 Wins! 🏆';
playsound('win');
} else if (win == 'Draw'){
text= "It's a Tie! 🪢";
playsound('error');
}
winner.innerText = text;
console.log(winner);
overlay.classList.remove("hidden");
overlay.classList.add("flex");
overlay.classList.replace("opacity-0","opacity-100");
};