-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.js
74 lines (72 loc) · 2.58 KB
/
style.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
val1=prompt("Enter name of player 1:");
val2=prompt("Enter name of player 2:");
document.querySelector('.info').textContent = `${val1}'s turn`
let active=true;
console.log("Welcome to Tic Tac Toe")
let music = new Audio("music.mp3")
let audioTurn = new Audio("ting.mp3")
let gameover = new Audio("gameover.mp3")
let turn = "X"
let isgameover = false;
// Function to change the turn
const changeTurn = ()=>{
return turn === "X"? "0": "X"
}
// Function to check for a win
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 !== "") ){
var print_name=boxtext[e[0]].innerText==="X"?val1:val2
document.querySelector('.info').innerText = val1 + " Won"
active =false;
isgameover = true
document.querySelector('.imgbox').getElementsByTagName('img')[0].style.width = "200px";
document.querySelector(".line").style.transform = `translate(${e[3]}vw, ${e[4]}vw) rotate(${e[5]}deg)`
document.querySelector(".line").style.width = "20vw";
}
})
}
if(active===true){
// Game Logic
// music.play()
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();
var print_name=turn==="X"?val1:val2;
checkWin();
if (!isgameover){
document.getElementsByClassName("info")[0].innerText = `${print_name}'s Turn`
}
}
})
})
// Add onclick listener to reset button
reset.addEventListener('click', ()=>{
let boxtexts = document.querySelectorAll('.boxtext');
Array.from(boxtexts).forEach(element => {
element.innerText = ""
});
turn = "X";
isgameover = false
document.querySelector(".line").style.width = "0vw";
var print_name=turn==="X"?val1:val2;
document.getElementsByClassName("info")[0].innerText = `${print_name}'s Turn`;
document.querySelector('.imgbox').getElementsByTagName('img')[0].style.width = "0px"
})
}