-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (139 loc) · 4.29 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const cells = document.querySelectorAll(".cell");
const statusText = document.querySelector("#statusText");
const restartBtn= document.querySelector("#restartBtn");
const clickSound = document.getElementById("click");
const winSound = document.getElementById("winsound");
const drawSound = document.getElementById("draw");
const winConditions=[
[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 winline=[
[1, -115, 0],
[0, -35, 0],
[0, 45, 0],
[-80, -30, 90],
[-1, -30, 90],
[80, -30, 90],
[0, -37, 45],
[-2, -35, 135]
];
let options=["","","","","","","","",""];
let currentPlayer="X";
let running=false;
initializeGame();
function initializeGame(){
cells.forEach(cell=>cell.addEventListener("click",cellClicked));
restartBtn.addEventListener("click",restartGame);
statusText.textContent = `Player ${currentPlayer}'s turn... 🧐`;
running=true;
}
function cellClicked(){
const cellIndex=this.getAttribute("cellIndex");
if(options[cellIndex] != "" || !running){
return;
}
updateCell(this, cellIndex);
playercolor(this, cellIndex);
checkWinner();
playSound()
}
function updateCell(cell, index){
options[index] = currentPlayer;
cell.textContent = currentPlayer;
}
function changePlayer(){
currentPlayer=(currentPlayer=="X") ? "O" : "X";
statusText.textContent=`Player ${currentPlayer}'s turn... 🧐`;
}
function checkWinner(){
let roundWon=false;
for(let i =0; i<winConditions.length;i++){
const condition=winConditions[i];
const cellA=options[condition[0]];
const cellB=options[condition[1]];
const cellC=options[condition[2]];
if(cellA==""||cellB==""||cellC==""){
continue;
}
if(cellA==cellB&&cellB==cellC){
drawline();
roundWon=true;
break;
}
}
if(roundWon){
statusText.textContent=`Player ${currentPlayer} wins! 🎉`;
document.querySelector(".line").style.width="320px";
playWinSound();
running=false;
}
else if(!options.includes("")){
statusText.textContent=`Draw... 🥲`;
playDrawSound();
running=false;
}
else{
changePlayer();
}
}
function drawline(){
const lineDiv = document.querySelector(".line");
// Find the index of the winning condition
let winIndex = -1;
for (let i = 0; i < winConditions.length; i++) {
const condition = winConditions[i];
const cellA = options[condition[0]];
const cellB = options[condition[1]];
const cellC = options[condition[2]];
if (cellA != "" && cellA === cellB && cellB === cellC) {
winIndex = i;
break;
}
}
// If a winning condition is found, draw the line
if (winIndex !== -1) {
const transformation = winline[winIndex];
applyTransformation(transformation);
}
}
// Function to apply transformation to the div
function applyTransformation(transformation) {
const lineDiv = document.querySelector(".line");
lineDiv.style.transform = `translate(${transformation[0]}px, ${transformation[1]}px) rotate(${transformation[2]}deg)`;
}
function restartGame(){
currentPlayer="X";
options=["","","","","","","","",""];
document.querySelector(".line").style.width="0px";
statusText.textContent=`Player ${currentPlayer}'s turn... 🧐`;
cells.forEach(cell=>cell.textContent="");
running=true;
}
function playercolor(cell, index){
// cells.forEach(cell=>cell.textContent="");
if(currentPlayer=="X"){
// cells.forEach(cellIndex=>cell.style.color = "#FF3939");
cells.forEach(cellIndex=>cell.style.textShadow= "2px 2px 5px red");
}
else if(currentPlayer=="O"){
// cells.forEach(cellIndex=>cell.style.color = "#397EFF");
cells.forEach(cellIndex=>cell.style.textShadow= "2px 2px 5px blue");
// document.querySelector(".cell").style.color = "blue";
}
}
function playSound() {
click.play();
}
function playWinSound() {
winsound.play();
}
function playDrawSound() {
draw.play();
}