-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
510 lines (480 loc) · 17.7 KB
/
index.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unbeatable Tic Tac Toe - Proof of concept by github.com/g-30</title>
<style>
@import url('https://fonts.googleapis.com/css?family=Delicious+Handrawn');
:root {
--grid-gap: .1em;
--strike-through-width: .1em;
--bg-color: #44674F;
}
body {
width: 100%;
height: 100%;
min-height: 100vh;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
background: var(--bg-color);
background: radial-gradient(ellipse at center, #63856a 1%,#3c5a40 100%);
box-shadow: inset 0 0 4vw rgba(0, 0, 0, .1);
}
body:before {
content: '';
position: absolute;
width: 100%;
height: 100%;
/** inlined noise image */
background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAAUVBMVEWFhYWDg4N3d3dtbW17e3t1dXWBgYGHh4d5eXlzc3OLi4ubm5uVlZWPj4+NjY19fX2JiYl/f39ra2uRkZGZmZlpaWmXl5dvb29xcXGTk5NnZ2c8TV1mAAAAG3RSTlNAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAvEOwtAAAFVklEQVR4XpWWB67c2BUFb3g557T/hRo9/WUMZHlgr4Bg8Z4qQgQJlHI4A8SzFVrapvmTF9O7dmYRFZ60YiBhJRCgh1FYhiLAmdvX0CzTOpNE77ME0Zty/nWWzchDtiqrmQDeuv3powQ5ta2eN0FY0InkqDD73lT9c9lEzwUNqgFHs9VQce3TVClFCQrSTfOiYkVJQBmpbq2L6iZavPnAPcoU0dSw0SUTqz/GtrGuXfbyyBniKykOWQWGqwwMA7QiYAxi+IlPdqo+hYHnUt5ZPfnsHJyNiDtnpJyayNBkF6cWoYGAMY92U2hXHF/C1M8uP/ZtYdiuj26UdAdQQSXQErwSOMzt/XWRWAz5GuSBIkwG1H3FabJ2OsUOUhG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jTok8i+algT1lTHi4SxFvONKNrgQFAq2/gFnWMXgwffgYMJpiKYkmW3tTg3ZQ9Jq+f8XN+A5eeUKHWvJWJ2sgJ1Sop+wwhqFVijqWaJhwtD8MNlSBeWNNWTa5Z5kPZw5+LbVT99wqTdx29lMUH4OIG/D86ruKEauBjvH5xy6um/Sfj7ei6UUVk4AIl3MyD4MSSTOFgSwsH/QJWaQ5as7ZcmgBZkzjjU1UrQ74ci1gWBCSGHtuV1H2mhSnO3Wp/3fEV5a+4wz//6qy8JxjZsmxxy5+4w9CDNJY09T072iKG0EnOS0arEYgXqYnXcYHwjTtUNAcMelOd4xpkoqiTYICWFq0JSiPfPDQdnt+4/wuqcXY47QILbgAAAABJRU5ErkJggg==')
0 0 / 4rem repeat;
mix-blend-mode: soft-light;
opacity: .75;
pointer-events: none;
}
body > div {
display: flex;
flex-direction: column;
}
/** Game wrap, SVG & animation */
#playground {
position: relative;
width: 70vh;
height: 70vh;
max-width: 32rem;
max-height: 32rem;
transition: opacity 300ms linear;
opacity: 0;
}
#playground.visible {
opacity: 1;
}
.line_animation svg line {
animation: dash 300ms ease-in;
animation-fill-mode: forwards;
}
svg line:nth-of-type(2) { animation-delay: 400ms; }
svg line:nth-of-type(3) { animation-delay: 700ms; }
svg line:nth-of-type(4) { animation-delay: 1000ms; }
@keyframes dash {
to {
stroke-dashoffset: 0;
}
}
svg {
position: absolute;
width: 100%;
height: auto;
opacity: .95;
transform: rotateZ(0.5deg);
}
/** The grid and the strikethrough line */
#xo {
position: relative;
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-template-rows: repeat(3, minmax(0, 1fr));
gap: var(--grid-gap);
width: 100%;
height: 100%;
}
#xo.game_ended {
pointer-events: none;
}
#xo:after {
content: '';
position: absolute;
display: none;
width: 90%;
height: var(--strike-through-width);
border-radius: calc(var(--strike-through-width) / 2);
background: #fff;
left: 5%;
top: 17%;
transform: rotateZ(-1deg);
filter: url(#chalk2) drop-shadow(0 0 1px var(--bg-color));
}
#xo.game_end_0:after, #xo.game_end_1:after, #xo.game_end_2:after { display: block; }
#xo.game_end_1:after { top: 48%; }
#xo.game_end_2:after { top: 82%; }
#xo.game_end_3:after, #xo.game_end_4:after, #xo.game_end_5:after {
display: block;
left: 15%;
top: 5%;
width: var(--strike-through-width);
height: 90%;
}
#xo.game_end_4:after { left: 48%; }
#xo.game_end_5:after { left: 82%; }
#xo.game_end_6:after, #xo.game_end_7:after {
display: block;
top: 52%;
left: -10%;
width: 124%;
transform: rotateZ(45deg);
}
#xo.game_end_7:after { top: 45%; transform: rotateZ(-45deg); }
/** Grid items (x / o letters) */
#xo p {
display: flex;
justify-content: center;
align-items: center;
margin: 0;
font-family: 'Delicious Handrawn', 'Comic Sans MS', 'Comic Sans', cursive;
font-size: .9em;
color: rgba(255, 255, 255, .92);
filter: url(#chalk2);
transform: rotateZ(3deg) scaleY(0.8);
cursor: pointer;
}
#xo p:nth-child(2n) {
transform: rotateZ(-175deg) scaleY(0.8);
}
#xo p:nth-child(3n) {
transform: scaleX(-1) scaleY(0.82);
}
#xo p.empty:before {
content: 'X';
opacity: 0;
}
#xo p.empty:hover:before {
content: 'X';
opacity: .3;
}
#xo p.userMarked:before {
content: 'X';
}
#xo p.computerMarked:before {
content: 'O';
}
/** refresh button and its states */
.button_wrap {
position: relative;
display: block;
height: 106px;
}
button, button:before, button:after {
display: block;
box-sizing: border-box;
border-radius: 35px;
}
button {
width: 60px;
height: 60px;
padding: 10px;
margin: 50px auto;
background-color: transparent;
border: rgba(0,0,0,0.3) 1px solid;
border-top-color: rgba(0,0,0,0.1);
border-left-color: rgba(0,0,0,0.2);
border-right-color: rgba(0,0,0,0.2);
border-bottom-color: rgba(0,0,0,0.3);
box-shadow: inset rgba(255,255,255,0.3) -5px -30px 10px,
inset rgba(255,255,255,0.3) 0px 30px 10px,
inset rgba(255,255,255,1) 0px 2px 1px,
rgba(0,0,0,0.4) 0px 10px 15px;
transition: box-shadow 0s ease-in-out;
text-align: center;
cursor: pointer;
background-image: radial-gradient( 50% 0%, 8% 50%, hsla(0,0%,100%,.2) 0%, hsla(0,0%,100%,0) 100%),
radial-gradient( 50% 100%, 12% 50%, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 100%),
radial-gradient( 0% 50%, 50% 7%, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 100%),
radial-gradient( 100% 50%, 50% 5%, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 100%),
repeating-radial-gradient( 50% 50%, 100% 100%, hsla(0,0%, 0%,0) 0%, hsla(0,0%, 0%,0) 3%, hsla(0,0%, 0%,.1) 3.5%),
repeating-radial-gradient( 50% 50%, 100% 100%, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 6%, hsla(0,0%,100%,.1) 7.5%),
repeating-radial-gradient( 50% 50%, 100% 100%, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 1.2%, hsla(0,0%,100%,0) 2.2%);
}
button:disabled {
pointer-events: none;
}
button:before, button:after {
content: "";
position: absolute;
}
button:before {
z-index: -50;
width: 60px;
height: 64px;
margin-top: -15px;
margin-left: -11px;
background: #666666;
background: -webkit-gradient(linear, left top, right top, color-stop(0%,#666666), color-stop(50%,#bbbbbb), color-stop(100%,#666666));
background: linear-gradient(left, #666666 0%,#bbbbbb 50%,#666666 100%);
box-shadow: #000 0px 0px 2px;
transition: transform 300ms ease-out;
}
button:after {
z-index: -100;
width: 70px;
height: 70px;
margin-top: -64px;
margin-left: -16px;
background: rgba(0,0,0,0.1);
box-shadow: inset rgba(255,255,255,0.1) 0px -5px 5px,
rgba(0,0,0,0.1) 0px 3px 3px,
rgba(255,255,255,0.05) 0px -3px 3px;
}
button:disabled, button:active {
margin-top: 46px;
color: #fff;
box-shadow: inset rgba(255,255,255,0.4) 0px -30px 15px,
inset rgba(255,255,255,0.2) -25px 25px 10px,
inset rgba(255,255,255,1) 0px 2px 1px,
rgba(0,0,0,0.4) 0px 2px 3px;
}
button:disabled:before, button:active:before {
height: 60px;
margin-top: -11px;
}
button:disabled:after, button:active:after {
margin-top: -60px;
}
button:disabled {
box-shadow: inset rgba(255,255,255,0.4) 0px -30px 15px,
inset rgba(255,255,255,0.2) -25px 25px 10px,
inset rgba(255,255,255,1) 0px 2px 1px;
}
button:disabled:before {
background: rgba(0, 0, 0, .1);
}
button i {
display: inline-block;
width: 40px;
height: 40px;
background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" xml:space="preserve"><path class="st0" d="M404 108c-28-27-63-47-103-56l3-52-102 54-27 14-5 3 8 4 38 21 81 43 2-42a165 165 0 0 1 109 225l40 17a208 208 0 0 0-44-231zM296 416l-81-43-2 42A164 164 0 0 1 91 256c0-23 5-46 13-66l-40-17a209 209 0 0 0 147 287l-3 52 102-54 27-14 5-3-8-4-38-21z" fill="black"/></svg>')
no-repeat center center / 32px;
transition: transform 300ms ease-out;
filter: drop-shadow(rgba(0,0,0,0.2) 0px -2px 1px) drop-shadow(rgb(255,255,255) 0px 1px 2px);
opacity: .5;
}
button:not(:disabled):hover:before {
transform: rotateZ(-7.5deg);
}
button:not(:disabled):hover i {
transform: rotateZ(-7.5deg);
}
</style>
</head>
<body>
<div>
<div id="playground" class="visible line_animation">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<defs>
<filter id="chalk" x="0" y="0">
<feTurbulence result="turbulenceFine" seed="0" type="fractalNoise" baseFrequency="0.06" numOctaves="3"/>
<feTurbulence result="turbulenceFat" seed="0" type="fractalNoise" baseFrequency="0.9" numOctaves="4"/>
<feDisplacementMap in="SourceGraphic" in2="turbulenceFat" scale="4"/>
<feDisplacementMap in2="turbulenceFine" scale="3"/>
<feGaussianBlur stdDeviation="0 1" />
<feDisplacementMap in2="turbulenceFine" scale="-5"/>
<feGaussianBlur stdDeviation="1 0" />
<feDisplacementMap in2="turbulenceFat" scale="-8"/>
</filter>
<filter id="chalk2" x="0" y="0">
<feTurbulence result="turbulenceFine" seed="0" type="fractalNoise" baseFrequency="0.2" numOctaves="3"/>
<feTurbulence result="turbulenceFat" seed="0" type="fractalNoise" baseFrequency="0.05" numOctaves="4"/>
<feDisplacementMap in="SourceGraphic" in2="turbulenceFat" scale="10"/>
<feDisplacementMap in2="turbulenceFine" scale="5"/>
<feGaussianBlur stdDeviation="0 1" />
<feDisplacementMap in2="turbulenceFine" scale="-5"/>
<feDisplacementMap in2="turbulenceFat" scale="-10"/>
</filter>
</defs>
<line filter="url(#chalk)" x1="66" y1="7" x2="62" y2="191" stroke="#FFF" stroke-linecap="round" stroke-width="8"/>
<line filter="url(#chalk)" x1="137" y1="9" x2="133" y2="194" stroke="#FFF" stroke-linecap="round" stroke-width="8"/>
<line filter="url(#chalk)" x1="7" y1="62" x2="192" y2="59" stroke="#FFF" stroke-linecap="round" stroke-width="8"/>
<line filter="url(#chalk)" x1="9" y1="130" x2="194" y2="133" stroke="#FFF" stroke-linecap="round" stroke-width="8"/>
</svg>
<div id="xo"></div>
</div>
<div class="button_wrap">
<button type="button" id="restartButton" disabled><i></i></button>
</div>
</div>
<script>
// lines animation
const refreshDelay = 500;
for (const elem of document.getElementsByTagName("line")) {
elem.setAttribute('style', `stroke-dasharray:${elem.getTotalLength()};stroke-dashoffset:${elem.getTotalLength()};`);
}
// actual logic
(() => {
const playgroundElem = document.getElementById('playground');
const xoElem = document.getElementById('xo');
const restartButton = document.getElementById('restartButton');
let isPlaygroundBlocked = false;
let isGameOver = false;
// Initial setup - fill in the grid with empty elements;
// also fill in the state array
let playgroundState = [];
for (let i = 0; i < 9; i++) {
const child = document.createElement('p');
child.addEventListener('click', (e) => {
e.preventDefault();
makeUserMove(i);
return false;
});
xoElem.appendChild(child);
playgroundState[i] = -1;
}
// save the initial zero-state for later force restart
const emptyState = [ ...playgroundState ];
// Update the state and initiate computer response or the end
function makeUserMove(i) {
if (isPlaygroundBlocked || playgroundState[i] !== -1) {
// Playground is blocked or the grid item is already filled in - skip
return;
}
playgroundState[i] = 1;
isPlaygroundBlocked = true;
// computer makes a move in response if user hasn't won yet
// and it's not a tie
const winCombinationIndex = getWinCombinationIndex();
if (winCombinationIndex === -1 && playgroundState.some((mover) => mover === -1)) {
setTimeout(makeComputerMove, (Math.random() + 0.5) * 100);
} else {
forceGameOver(winCombinationIndex);
}
drawState();
}
// restart functionality when it's available
restartButton.addEventListener('click', (e) => {
e.preventDefault();
if (!isGameOver) {
return false;
}
forceRestart();
return false;
});
function forceRestart() {
playgroundElem.className = 'line_animation';
setTimeout(() => {
playgroundElem.className = '';
void playgroundElem.offsetWidth; // hacky way to trigger redraw & restart CSS animation
playgroundElem.className = 'visible line_animation';
isGameOver = false;
isPlaygroundBlocked = false;
playgroundState = [ ...emptyState ];
restartButton.disabled = true;
xoElem.className = '';
drawState();
}, refreshDelay);
}
// end the game and show the strikethrough line
function forceGameOver(winCombinationIndex) {
isGameOver = true;
restartButton.disabled = false;
xoElem.className = `game_ended game_end_${winCombinationIndex}`;
}
// all possible win indexes for 3x3 grid
const winCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
function getWinCombinationIndex(tempState) {
const state = tempState || playgroundState;
for (let mover = 0; mover <= 1; mover++) {
for (let c = 0; c < winCombinations.length; c++) {
if (winCombinations[c].every((boardIndex) => state[boardIndex] === mover)) {
return c;
}
}
}
return -1;
}
function makeComputerMove() {
const moveData = getComputerMoveData([ ...playgroundState ]);
if (moveData.index === -1) {
forceGameOver(-1);
alert('Bad state - computer got no moves to make');
return;
}
playgroundState[moveData.index] = 0;
drawState();
isPlaygroundBlocked = false;
// check if computer won
const winCombinationIndex = getWinCombinationIndex();
if (winCombinationIndex >= 0) {
forceGameOver(winCombinationIndex);
}
}
// the actual minmax recursive algorithm
// there's room for optimization
function getComputerMoveData(tempPlaygroundState, depth) {
const winCombinationIndex = getWinCombinationIndex(tempPlaygroundState);
if (winCombinationIndex !== -1) {
// we've got a winner in this hypothetical game!
const wonMover = tempPlaygroundState[winCombinations[winCombinationIndex][0]];
return {
index: -1,
score: wonMover === 1
? -1 // user hypothetically won (bad)
: 1 // computer hypothetically won (good)
};
} else if (!tempPlaygroundState.some((mover) => mover === -1)) {
// a tie
return { index: -1, score: 0 };
}
depth = (depth || 0) + 1;
// 1 - user's hypothetical turn; 0 - computer's hypothetical turn.
const mover = +!(depth % 2);
const availableMoves = tempPlaygroundState
.map((_, i) => i)
.filter((boardIndex) => tempPlaygroundState[boardIndex] === -1);
const scores = [];
const moves = [];
for (let i = 0; i < availableMoves.length; i++) {
const move = availableMoves[i];
const iterationPlayground = [ ...tempPlaygroundState ];
iterationPlayground[move] = mover;
const hypotheticalMoveData = getComputerMoveData(iterationPlayground, depth);
scores.push(hypotheticalMoveData.score);
moves.push(move);
// rollback the hypothetical move
tempPlaygroundState[move] = -1;
}
const func = mover === 1
? Math.min // we're looking for the min score if it's user's turn
: Math.max; // and for the max score if it's computer's score
const moveScore = func.apply(null, scores);
const moveScoreIndex = scores.indexOf(moveScore);
return { index: moves[moveScoreIndex], score: moveScore };
}
// grid items states
const stateToClassName = { '-1': 'empty', '0': 'computerMarked', '1': 'userMarked' };
// UI state init
drawState();
function drawState() {
const elems = document.querySelectorAll('#xo > p');
for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
elem.className = stateToClassName[playgroundState[i]];
}
}
// recompute the grid on first launch & window resize
window.addEventListener('resize', onWindowResize);
onWindowResize();
function onWindowResize() {
const gridSquareSize = Math.floor(Math.min(window.innerWidth, window.innerHeight) * .7);
playgroundElem.style.width = `${gridSquareSize}px`;
playgroundElem.style.height = `${gridSquareSize}px`;
// font size for x & o
const xoRelativeFontSize = Math.floor(xoElem.offsetWidth / 3 / 16 * 100);
playgroundElem.style.fontSize = `${xoRelativeFontSize}%`;
}
})();
</script>
</body>
</html>