-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic_Tac_Toe
More file actions
222 lines (210 loc) · 4.87 KB
/
Copy pathTic_Tac_Toe
File metadata and controls
222 lines (210 loc) · 4.87 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic Tac Toe</title>
<link href="https://fonts.googleapis.com/css2?family=Raleway:wght@700&display=swap" rel="stylesheet">
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family:Georgia, 'Times New Roman', Times, serif
}
body {
height: 100vh;
background-image: url("https://source.unsplash.com/random");
background-size: cover;
justify-content: center;
align-items: center;
background-repeat: no-repeat;
}
html {
font-size: 16px;
}
.wrapper {
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
}
.container {
width: 70vmin;
height: 70vmin;
display: flex;
flex-wrap: wrap;
gap: 2vmin;
}
.button-option {
background:white;
height: 4cm;
width: 4cm;
border-width: 5px;
border-color: black;
border-radius: 6px;
font-size: 12vmin;
color: #000000;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
#restart {
font-size: 1.3em;
padding: 1em;
border-radius: 5px;
background-color: #0a0027;
color: #ffffff;
border: none;
position: relative;
margin: 1.5em auto 0 auto;
display: block;
}
.popup {
background-color: rgba(255, 123, 0, 0.95); /* Semi-transparent gold */
color: #333; /* Dark text color for readability */
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
display: flex;
z-index: 2;
align-items: center;
justify-content: center;
flex-direction: column;
gap: 2em;
font-size: 4vmin;
border-radius: 0;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
animation: fadeIn 0.5s ease-out;
padding: 2em;
background-image: url();
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
#new-game, #message {
font-size: 2em;
padding: 0.8em 1.6em;
background-color: #0a0027;
color: #ffffff;
border: none;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
transition: background-color 0.3s, transform 0.3s;
}
#new-game:hover, #restart:hover {
background-color: #f5f5f5;
color: #0a0027;
transform: scale(1.05);
}
#message {
margin-bottom: 1em;
}
.popup.hide {
display: none;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container">
<button class="button-option"></button>
<button class="button-option"></button>
<button class="button-option"></button>
<button class="button-option"></button>
<button class="button-option"></button>
<button class="button-option"></button>
<button class="button-option"></button>
<button class="button-option"></button>
<button class="button-option"></button>
</div>
<button id="restart">Restart</button>
</div>
<div class="popup hide">
<p id="message">Sample Message</p>
<button id="new-game">New Game</button>
</div>
<script>
let btnRef = document.querySelectorAll(".button-option");
let popupRef = document.querySelector(".popup");
let newgameBtn = document.getElementById("new-game");
let restartBtn = document.getElementById("restart");
let msgRef = document.getElementById("message");
let winningPattern = [
[0, 1, 2],
[0, 3, 6],
[2, 5, 8],
[6, 7, 8],
[3, 4, 5],
[1, 4, 7],
[0, 4, 8],
[2, 4, 6],
];
let xTurn = true;
let count = 0;
const disableButtons = () => {
btnRef.forEach((element) => (element.disabled = true));
popupRef.classList.remove("hide");
};
const enableButtons = () => {
btnRef.forEach((element) => {
element.innerText = "";
element.disabled = false;
});
popupRef.classList.add("hide");
};
const winFunction = (letter) => {
disableButtons();
if (letter == "X") {
msgRef.innerHTML = " X Wins";
} else {
msgRef.innerHTML = " O Wins";
}
};
const drawFunction = () => {
disableButtons();
msgRef.innerHTML = "It's a Draw";
};
newgameBtn.addEventListener("click", () => {
count = 0;
enableButtons();
});
restartBtn.addEventListener("click", () => {
count = 0;
enableButtons();
});
const winChecker = () => {
for (let i of winningPattern) {
let [element1, element2, element3] = [
btnRef[i[0]].innerText,
btnRef[i[1]].innerText,
btnRef[i[2]].innerText,
];
if (element1 != "" && (element2 != "") & (element3 != "")) {
if (element1 == element2 && element2 == element3) {
winFunction(element1);
}
}
}
};
btnRef.forEach((element) => {
element.addEventListener("click", () => {
if (xTurn) {
xTurn = false;
element.innerText = "X";
element.disabled = true;
} else {
xTurn = true;
element.innerText = "O";
element.disabled = true;
}
count += 1;
if (count == 9) {
drawFunction();
}
winChecker();
});
});
window.onload = enableButtons;
</script>
</body>
</html>