-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsPersonApp.js
58 lines (50 loc) · 2.34 KB
/
vsPersonApp.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
var preloader =document.getElementById("preloader");
window.addEventListener("load" , function(){
preloader.style.display = "none";
})
let buttons = document.querySelectorAll(".game-btn");
let player = document.getElementById("player");
let usedBtns = [];
let currentPlayer = "X";
function btnClick(event) {
let clickedButton = event.target;
if (clickedButton.innerText === "") {
usedBtns.push(clickedButton.value);
clickedButton.innerText = currentPlayer;
if (
(buttons[0].innerText === currentPlayer && buttons[1].innerText === currentPlayer && buttons[2].innerText === currentPlayer) ||
(buttons[3].innerText === currentPlayer && buttons[4].innerText === currentPlayer && buttons[5].innerText === currentPlayer) ||
(buttons[6].innerText === currentPlayer && buttons[7].innerText === currentPlayer && buttons[8].innerText === currentPlayer) ||
(buttons[0].innerText === currentPlayer && buttons[4].innerText === currentPlayer && buttons[8].innerText === currentPlayer) ||
(buttons[2].innerText === currentPlayer && buttons[4].innerText === currentPlayer && buttons[6].innerText === currentPlayer) ||
(buttons[0].innerText === currentPlayer && buttons[3].innerText === currentPlayer && buttons[6].innerText === currentPlayer) ||
(buttons[1].innerText === currentPlayer && buttons[4].innerText === currentPlayer && buttons[7].innerText === currentPlayer) ||
(buttons[2].innerText === currentPlayer && buttons[5].innerText === currentPlayer && buttons[8].innerText === currentPlayer)
) {
setTimeout(() => {
currentPlayer = currentPlayer === "X" ? "O" : "X";
alert("Player " + currentPlayer + " Won !");
window.location.reload();
return;
}, 400);
}
else if(usedBtns.length == 9){
setTimeout(() => {
alert("Game Draw!");
window.location.reload();
return;
}, 400);
}
else{
}
currentPlayer = currentPlayer === "X" ? "O" : "X";
player.innerText = "Player " + currentPlayer + " Turn";
}
}
buttons.forEach(button => {
button.addEventListener('click', btnClick);
});
function btnReloadClick(){
location.reload();
return;
}