-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
71 lines (56 loc) · 1.95 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
console.log("welcome to Tic Tac Toe")
let audioturn = new Audio("ding.mp3.mpeg")
let gameover = new Audio("gameover.mp3.mpeg")
let turn = "X"
let Isgameover = false;
function changeturn() {
return turn === "X"? "0": "X"
}
// function to check for win
const checkwin = () =>{
let boxtext = document.getElementsByClassName('boxtext');
let wins=[
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6],
]
wins.forEach(e =>{
if ((boxtext[e[0]].innerText === boxtext[e[1]].innerText) && (boxtext[e[2]].innerText === boxtext[e[1]].innerText) && (boxtext[e[0]].innerText !== '') ){
document.querySelector('.info').innerText= boxtext[e[0]].innerText + " won "
gameover = true
document.querySelector(".imgBox").getElementsByTagName('img')[0].style.width ="200px"
}
})
}
// game logic
let boxes = document.getElementsByClassName("box");
Array.from(boxes).forEach(element =>{
let boxtext = element.querySelector('.boxtext');
element.addEventListener('click', ()=> {
if (boxtext.innerText === '') {
boxtext.innerText = turn;
turn = changeturn();
audioturn.play();
checkwin();
if (Isgameover){
document.getElementsByClassName("Info")[0].innertext = "turn for" + turn;
}
}
})
})
// add onclick listner to reset button
reset.addEventListener('click',()=>{
let boxtexts = document.querySelectorAll('.boxtext');
Array.from(boxtexts).forEach(element=> {
element.innerText = " "
});
turn = "x";
Isgameover= false
document.getElementsByClassName("Info")[0].innertext = " turn for " + turn;
document.querySelector('.imgBox').getElementsByTagName('img')[0].style.width ="0"
})