-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
110 lines (102 loc) · 3.64 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
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
console.log("TIC TAC TOE by MR.Nobody");
console.log("Refference taken from code-with-harry yt");
console.log("Music credits: ");
console.log("Colorful Flowers by Tokyo Music Walker | https://soundcloud.com/user-356546060")
console.log("Music promoted by https://www.chosic.com/free-music/all/")
console.log("Creative Commons CC BY 3.0")
console.log("https://creativecommons.org/licenses/by/3.0/")
//music files
let music = new Audio("Colorful-Flowers(chosic.com).mp3");
//music.play();
let audioTurn = new Audio("turn.WAV");
let gameover = new Audio("gameover.mp3");
let turn = 'X';
let game_over = false;
const musicButton = document.getElementById('music');
musicButton.addEventListener('click', () => {
if (music.paused) {
music.play();
//musicButton.classList.remove('strikethrough');
musicButton.innerHTML = '🔊';
} else {
music.pause();
//musicButton.classList.add('strikethrough');
musicButton.innerHTML = '🔇';
}
});
//function for winner
const checkwin = () => {
let boxtext = document.getElementsByClassName('boxtext');
let wins = [
[0, 1, 2, 5, 5, 0],
[3, 4, 5, 5, 15, 0],
[6, 7, 8, 5, 25, 0],
[0, 3, 6, -5, 15, 90],
[1, 4, 7, 5, 15, 90],
[2, 5, 8, 15, 15, 90],
[0, 4, 8, 5, 15, 45],
[2, 4, 6, 5, 15, 135],
]
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";
game_over = true;
document.querySelector('.imgbox').getElementsByTagName('img')[0].style.width = "30vw";
// gameover.play();
//gameover.loop=true;
music.pause();
document.querySelector(".line").style.transform = `translate(${e[3]}vw,${e[4]}vw) rotate(${e[5]}deg)`
document.querySelector(".line").style.width = "20vw";
gameover.addEventListener('ended', function () {
this.currentTime = 0.1;
this.play();
}, false);
gameover.play();
}
})
}
//funtion to change turn
const changeturn = () => {
if (!game_over) {
return turn === "X" ? "0" : "X"
}
else {
return turn === " ";
}
}
//Game logic
let boxes = document.getElementsByClassName("box");
Array.from(boxes).forEach(element => {
let boxtext = element.querySelector('.boxtext');
element.addEventListener('click', () => {
if ( !game_over && boxtext.innerText === '') {
boxtext.innerText = turn;
turn = changeturn();
audioTurn.play();
checkwin();
if (!game_over) {
document.getElementsByClassName('info')[0].innerText = "Turn for " + turn;
}
}
})
})
//reset
reset.addEventListener('click', () => {
let boxtexts = document.querySelectorAll('.boxtext');
Array.from(boxtexts).forEach(element => {
element.innerText = ""
});
turn = 'X';
game_over = false;
document.querySelector(".line").style.width = "0px";
document.getElementsByClassName('info')[0].innerText = "Turn for " + turn;
gameover.pause();
music.play();
document.querySelector('.imgbox').getElementsByTagName('img')[0].style.width = "0px";
})
//dark mode
const checkbox = document.getElementById("checkbox")
checkbox.addEventListener("change", () => {
document.body.classList.toggle("dark")
})