-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic.html
269 lines (242 loc) · 8.74 KB
/
tic.html
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tic Tac Toe</title>
<style>
body {
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: linear-gradient(to bottom right, #81c784, #4caf50);
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
canvas {
border: 4px solid rgba(255, 255, 255, 0.2);
border-radius: 20px;
}
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(5px);
color: white;
display: flex;
justify-content: left;
z-index: 1000; /* Ensure the nav is above other content */
}
nav a {
display: flex;
align-items: center;
padding: 2px 2px 2px 2px; /* Adjust padding as needed */
text-decoration: none;
color: white;
margin: 0.5px;
}
nav a img {
width: 22px; /* Adjust image width as needed */
margin: 0.2px; /* Add spacing between image and text */
border-radius: 5px;
}
nav a:hover {
background-color: rgba(255, 255, 255, 0.4);
}
nav a:nth-child(3) {
margin-left: auto;
}
</style>
<script
async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2749889536957447"
crossorigin="anonymous"
></script>
</head>
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-MDG8SVY9GB"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-MDG8SVY9GB");
</script>
<body>
<nav>
<a href="https://ronynn.github.io"
><img src="https://github.com/ronynn.png"
/></a>
<a href="https://ronynn.github.io/prototypes/tic.html"
>Material Tic-Tac-Toe</a
>
<a href="https://m.youtube.com/@ronynn89">Youtube</a>
<a href="https://ronynn.github.io/blog/">Blog</a>
<a href="https://ronynn.github.io/portfolio">Portfolio</a>
</nav>
<br />
<br />
<canvas id="ticTacToeCanvas" width="300" height="300"></canvas>
<script>
const canvas = document.getElementById("ticTacToeCanvas");
const ctx = canvas.getContext("2d");
const cellSize = 100;
let currentPlayer = "X";
let board = [
["", "", ""],
["", "", ""],
["", "", ""]
];
function drawBoard() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.lineWidth = 4;
ctx.strokeStyle = "rgba(255, 255, 255, 0.2)";
for (let i = 1; i < 3; i++) {
ctx.beginPath();
ctx.moveTo(i * cellSize, 0);
ctx.lineTo(i * cellSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * cellSize);
ctx.lineTo(canvas.width, i * cellSize);
ctx.stroke();
}
}
function drawX(x, y) {
ctx.strokeStyle = "#fff";
ctx.lineWidth = 6;
const offsetX = 20;
const offsetY = 20;
ctx.beginPath();
ctx.moveTo(x * cellSize + offsetX, y * cellSize + offsetY);
ctx.lineTo(
(x + 1) * cellSize - offsetX,
(y + 1) * cellSize - offsetY
);
ctx.moveTo(
(x + 1) * cellSize - offsetX,
y * cellSize + offsetY
);
ctx.lineTo(
x * cellSize + offsetX,
(y + 1) * cellSize - offsetY
);
ctx.stroke();
}
function drawO(x, y) {
ctx.strokeStyle = "#fff";
ctx.lineWidth = 6;
const centerX = x * cellSize + cellSize / 2;
const centerY = y * cellSize + cellSize / 2;
const radius = cellSize / 2 - 20;
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
ctx.stroke();
}
function checkWinner() {
for (let i = 0; i < 3; i++) {
if (
board[i][0] !== "" &&
board[i][0] === board[i][1] &&
board[i][1] === board[i][2]
) {
return board[i][0];
}
if (
board[0][i] !== "" &&
board[0][i] === board[1][i] &&
board[1][i] === board[2][i]
) {
return board[0][i];
}
}
if (
board[0][0] !== "" &&
board[0][0] === board[1][1] &&
board[1][1] === board[2][2]
) {
return board[0][0];
}
if (
board[0][2] !== "" &&
board[0][2] === board[1][1] &&
board[1][1] === board[2][0]
) {
return board[0][2];
}
return null;
}
canvas.addEventListener("click", event => {
const x = Math.floor(event.offsetX / cellSize);
const y = Math.floor(event.offsetY / cellSize);
if (board[y][x] === "") {
board[y][x] = currentPlayer;
currentPlayer = currentPlayer === "X" ? "O" : "X";
drawBoard();
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === "X") {
drawX(j, i);
} else if (board[i][j] === "O") {
drawO(j, i);
}
}
}
const winner = checkWinner();
if (winner) {
alert(`${winner} wins!`);
resetGame();
}
}
});
function resetGame() {
board = [
["", "", ""],
["", "", ""],
["", "", ""]
];
currentPlayer = "X";
drawBoard();
}
drawBoard();
</script>
<footer>
<br />
<br />
<hr />
<a
href="https://github.com/ronynn/prototypes"
title="Go to GitHub repo"
><img
src="https://img.shields.io/static/v1?label=ronynn&message=prototypes&color=blue&logo=github"
alt="ronynn - prototypes"
/></a>
<a href="https://github.com/ronynn/prototypes"
><img
src="https://img.shields.io/github/stars/ronynn/prototypes?style=social"
alt="stars - prototypes" /></a
><br />
</footer>
<script
type="text/javascript"
id="cookiebanner"
src="https://cdn.jsdelivr.net/gh/dobarkod/cookie-banner@1.2.2/dist/cookiebanner.min.js"
data-height="25px"
data-position="bottom"
data-moreinfo="https://ronynn.github.io/blog/privacy"
data-message="We use cookies to improve your app experience."
></script>
<noscript>
<div class="noscript" style="text-align: center; color: white">
It's not a server based app so you need javascript to run this.
</div>
</noscript>
</body>
</html>