-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
131 lines (116 loc) · 4.1 KB
/
Copy pathscript2.js
File metadata and controls
131 lines (116 loc) · 4.1 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const click = new Audio("Click.mp3");
const winning = new Audio("Winning.mp3");
const draw = new Audio("draw.wav");
const reset = new Audio ("reset.wav");
const boxes = document.querySelectorAll(".box");
const resetBtn = document.querySelector("#reset-btn");
const newGameBtn = document.querySelector("#new-btn");
const msgContainer = document.querySelector(".msg-container");
const gif = document.querySelector(".gif");
const gif2 = document.querySelector(".gif2");
const msg = document.querySelector("#msg");
let currentPlayer = "X";
let gameOver = false;
const resetGame = () => {
boxes.forEach(box => {
box.classList.remove('winning-box');
});
currentPlayer = "X";
gameOver = false;
boxes.forEach(box => {
box.innerText = "";
box.disabled = false;
reset.play();
});
msgContainer.classList.add("hide");
gif.classList.add("hidden");
gif2.classList.add("hiddenn");
};
const winPatterns = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
const checkWin = () => {
return winPatterns.some(pattern => {
const [a, b, c] = pattern;
return boxes[a].innerText && boxes[a].innerText === boxes[b].innerText && boxes[a].innerText === boxes[c].innerText;
});
};
const checkDraw = () => {
return Array.from(boxes).every(box => box.innerText !== "");
};
const computerMove = () => {
const availableCells = Array.from(boxes).filter(box => box.innerText === "");
if (availableCells.length > 0 && !gameOver) {
const randomIndex = Math.floor(Math.random() * availableCells.length);
const randomCell = availableCells[randomIndex];
randomCell.innerText = currentPlayer;
if (checkWin(winPatterns)) {
winPatterns.forEach(pattern => {
const [a, b, c] = pattern;
if (boxes[a].innerText === currentPlayer &&
boxes[b].innerText === currentPlayer &&
boxes[c].innerText === currentPlayer) {
pattern.forEach(pos => {
boxes[pos].classList.add('winning-box');
});
}
});
msg.innerText = "Congratulations, the winner is " + currentPlayer;
msgContainer.classList.remove("hide");
gif.classList.remove("hidden");
gameOver = true;
winning.play();
} else if (checkDraw()) {
msg.innerText = "It's a draw!";
msgContainer.classList.remove("hide");
gif2.classList.remove("hiddenn");
gameOver = true;
draw.play();
} else {
currentPlayer = currentPlayer === "X" ? "O" : "X";
}
}
};
boxes.forEach(box => {
box.addEventListener("click", () => {
if (!box.innerText && !gameOver) {
box.innerText = currentPlayer;
click.play();
if (checkWin(winPatterns)) {
winPatterns.forEach(pattern => {
const [a, b, c] = pattern;
if (boxes[a].innerText === currentPlayer &&
boxes[b].innerText === currentPlayer &&
boxes[c].innerText === currentPlayer) {
pattern.forEach(pos => {
boxes[pos].classList.add('winning-box');
});
}
});
msg.innerText = "Congratulations, the winner is " + currentPlayer;
msgContainer.classList.remove("hide");
gif.classList.remove("hidden");
gameOver = true;
winning.play();
} else if (checkDraw()) {
msg.innerText = "It's a draw!";
msgContainer.classList.remove("hide");
gif2.classList.remove("hiddenn");
gameOver = true;
draw.play();
} else {
currentPlayer = currentPlayer === "X" ? "O" : "X";
setTimeout(computerMove, 300);
}
}
});
});
newGameBtn.addEventListener("click", resetGame);
resetBtn.addEventListener("click", resetGame);