-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
93 lines (76 loc) · 2.15 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
console.log("Welcome to Tic Tac Toe");
// Setting variables;
let turn = "X";
let reset = document.getElementById("reset");
let userTurn = document.getElementsByClassName("userTurn");
let winner = document.getElementById("winner");
let win = "False";
let gif = document.getElementById("gif");
// function for the reset button to reset everything.
const resetGame = () => {
turn = "X";
win = "False";
userTurn[0].textContent = `TURN: ${turn}`;
winner.textContent = "";
gif.style.opacity = 0;
allBoxes = document.getElementsByClassName("boxtext");
Array.from(allBoxes).forEach(element => {
element.innerText = "";
})
}
// function to change the turn;
const changeTurn = () => {
if (turn == "X") {
turn = "0";
}
else {
turn = "X";
}
return turn;
}
// Function to check whether the player wins or not.
const checkWin = () => {
// code to check
console.log("Checking for win")
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 ((boxes[e[0]].innerText === boxes[e[1]].innerText) && (boxes[e[1]].innerText === boxes[e[2]].innerText) && (boxes[e[0]].innerText != "")) {
win = "True";
}
})
if (win == "True") {
winner.textContent = `${turn} wins the match.`;
}
}
// Logic for the Game
// Logic for the Game
// Logic for the Game
let boxes = document.getElementsByClassName("box");
Array.from(boxes).forEach(element => {
element.addEventListener('click', () => {
let boxtext = element.querySelector('.boxtext');
if (boxtext.innerText === "") {
boxtext.innerText = turn;
checkWin();
turn = changeTurn();
if (win == "False"){
userTurn[0].textContent = `TURN: ${turn}`;
}
if (win == "True"){
userTurn[0].textContent = `Game Over!`;
gif.style.opacity = 1;
}
}
})
})
// if someone clicked the reset button.
reset.addEventListener('click', resetGame);