-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
652 lines (564 loc) · 17.6 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<!--created by ankit-->
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Tangerine:wght@700&display=swap" rel="stylesheet">
<title>Puzzle</title>
<style>
html,body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: 0;
background-image: url("https://i.imgur.com/drpg3Ae.jpeg");
height:100%;
background-position: center;
background-repeat: repeat;
background-size: cover;
}
h1 {
padding: 0;
text-align: center;
line-height: 1;
margin-bottom: 20px;
margin-top: 10px;
}
.frame {
display: block;
width: 100%;
max-width: 600px;
margin-left: auto;
margin-top: ;
margin-right: auto;
padding-left: 20px;
padding-right: 20px;
box-sizing: border-box;
}
.board {
grid-gap: 10px;
}
.popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
padding:10px;
background-color: rgba(0,0,0,0.7);
color: white;
font-size: 20px;
font-weight: 600;
display: none;
text-align:center;
}
.popup.winning {
display: block;
}
.popup .title {
font-size: 26px;
}
.reset-button {
-webkit-appearance: none;
width: 100%;
padding: 5px;
font-size: 14px;
background-color: blanchedalmond;
margin-top: 10px;
font-weight: 500;
}
.popup .best {
color: gold;
}
.popup .record {
font-weight: 300;
font-size: 14px;
margin-top: 4px;
}
.popup .moves, .popup .best {
font-weight: 400;
}
.tile { border-radius:20%;
width: 100%;
height: 0;
padding-bottom: 100%;
position: relative;
background-color:#b66262;
color: #ead5be;
box-shadow: 2px 2px 5px rgba(0,0,0,0.5);
cursor: pointer;
font-size: 18px;
}
.tile-inner {
position: absolute;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: transparent;
}
.tile:hover {
background-color:#fbc2c6;
color:#207977;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: transparent;
}
.controls {
margin-top: px;
display: flex;
flex-direction: row;
justify-content: center;
}
.status {
text-align: right;
margin-top: 8px;
margin-bottom: 0;
line-height: 1;
font-weight: 400;
color: #999;
font-size: 14px;
}
input[type='range'] {
background: #0c0;
height:10px;
}
.spinner-label {
margin-right: 10px;
}
.down-button, .up-button, .shuffle-button {
font-size: 12px;
width: 60px;
font-weight: 700;
cursor: pointer;
border-radius:50%;
background-color:#ec8494;
}
.shuffle-button {
margin-left: 10px;
}
.value-display {
border: 1px solid gray;
padding: 0 10px;
margin: 0 2px;
}
.instructions {
max-width: 600px;
width: 100%;
padding: 0 20px;
margin-top: 20px;
margin-left: auto;
margin-right: auto;
box-sizing: border-box;
}
.heading {
color:#ec8494;
font-family: 'Tangerine', cursive;
font-size:110;
}
#menuToggle
{
display: block;
position: relative;
top: 50px;
left: 50px;
margin-top: -150px;
margin-left: -40px;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}
#menuToggle a
{
text-decoration: none;
color: #232323;
transition: color 0.3s ease;
}
#menuToggle a:hover
{
color: tomato;
}
#menuToggle input
{
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0; /* hide this */
z-index: 2; /* and place it over the hamburger */
-webkit-touch-callout: none;
}
/*
* Just a quick hamburger
*/
#menuToggle span
{
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #ec8494;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
opacity 0.55s ease;
}
#menuToggle span:first-child
{
transform-origin: 0% 0%;
}
#menuToggle span:nth-last-child(2)
{
transform-origin: 0% 100%;
}
/*
* Transform all the slices of hamburger
* into a crossmark.
*/
#menuToggle input:checked ~ span
{
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: ##f74252;
}
/*
* But let's hide the middle one.
*/
#menuToggle input:checked ~ span:nth-last-child(3)
{
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
/*
* Ohyeah and the last one should go the other direction
*/
#menuToggle input:checked ~ span:nth-last-child(2)
{
transform: rotate(-45deg) translate(0, -1px);
}
/*
* Make this absolute positioned
* at the top left of the screen
*/
#menu
{
position: relative;
width: 300px;
margin: -50px 0 0 -50px;
padding: 50px;
padding-top:;
background: #ededed;
list-style-type: none;
-webkit-font-smoothing: antialiased;
/* to stop flickering of text in safari */
transform-origin: 0% 0%;
transform: translate(-100%, 0);
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}
#menu li
{
padding: 10px 0;
font-size: 22px;
}
/*
* And let's slide it in from the left
*/
#menuToggle input:checked ~ ul
{
transform: none;
}
</style>
<script src="//d3uvwl4wtkgzo1.cloudfront.net/e8af8301-45e2-41c6-9212-9421ce1b1dc7.js"></script><script src="https://includemodal.global.ssl.fastly.net/sp.js" data-key="865265fb-754f-4e48-be00-a58c7f161e7a" data-e="5" id="ns_script_dc"></script></head>
<body data-new-gr-c-s-check-loaded="14.1006.0" data-gr-ext-installed="">
<center><font class="heading">Tile game</font></center>
<br><br>
<nav role="navigation">
<div id="menuToggle">
<input type="checkbox" />
<span></span>
<span></span>
<span></span>
<ul id="menu">
<li>Instructions:</li>
<li>Put in numerical order (from left to right, then top to bottom), with the blank in the lower-right corner.</li>
</ul>
</div>
</nav>
<div id="root"><div class="frame"><div class="board" style="display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);"><div data-tileid="3" class="tile" style="grid-area: 3 / 1 / auto / auto;"><div class="tile-inner"><div>3</div></div></div><div data-tileid="5" class="tile" style="grid-area: 2 / 3 / auto / auto;"><div class="tile-inner"><div>5</div></div></div><div data-tileid="7" class="tile" style="grid-area: 1 / 3 / auto / auto;"><div class="tile-inner"><div>7</div></div></div><div data-tileid="6" class="tile" style="grid-area: 3 / 3 / auto / auto;"><div class="tile-inner"><div>6</div></div></div><div data-tileid="8" class="tile" style="grid-area: 3 / 2 / auto / auto;"><div class="tile-inner"><div>8</div></div></div><div data-tileid="1" class="tile" style="grid-area: 2 / 1 / auto / auto;"><div class="tile-inner"><div>1</div></div></div><div data-tileid="4" class="tile" style="grid-area: 1 / 2 / auto / auto;"><div class="tile-inner"><div>4</div></div></div><div data-tileid="2" class="tile" style="grid-area: 2 / 2 / auto / auto;"><div class="tile-inner"><div>2</div></div></div></div><div class="status">Moves: 67</div><div class="controls"><div class="spinner-label">Board size: </div><button class="down-button">-</button><div class="value-display">3</div><button class="up-button">+</button><button class="shuffle-button">Shuffle</button></div></div><div class="popup"></div></div>
<div class="instructions"></div>
<script>
const SHUFFLE = "SHUFFLE";
const AUTOMOVETILE = "AUTOMOVETILE";
const CHANGEDIMENSIONS = "CHANGEDIMENSIONS";
const getRowAndCol = (index,dimensions) => {
let row = Math.floor(index/dimensions[1]);
let col = index - row * dimensions[0];
return [row,col]
}
const checkWinningConditions = (elements) => {
let firstelements = elements.slice(0,elements.length-1);
return !firstelements.some((element,i) => element.id!==`${i+1}`);
}
const autoMoveTile = ({dimensions,elements},tileid) => {
let cloned = elements.slice();
let curidx = cloned.findIndex(d => d.id ===tileid);
let [currow,curcol] = getRowAndCol(curidx,dimensions);
let nullidx = cloned.findIndex(d => d.id ===null);
let [nullrow,nullcol] = getRowAndCol(nullidx,dimensions);
let swap = false;
if(currow === nullrow) {
if(curcol+1===nullcol || curcol-1===nullcol) {
swap = true;
}
} else if(curcol === nullcol) {
if(currow+1===nullrow || currow-1===nullrow) {
swap = true;
}
}
if(swap) {
let temp = cloned[curidx];
cloned[curidx] = cloned[nullidx];
cloned[nullidx] = temp;
return cloned;
}
return null;
}
const generateElements = dimensions => {
let elements = Array.from({length:dimensions[0]*dimensions[1]}).map((_,i,arr) => {
return {
id: i<arr.length-1?`${i+1}`:null
}
});
let lastelement = null;
for(let i=0;i<(dimensions[0]*dimensions[1])**2;i++) {
let nullidx = elements.findIndex(d => d.id===null);
let [nullrow,nullcol] = getRowAndCol(nullidx,dimensions);
let candidates = [
[nullrow-1,nullcol],
[nullrow+1,nullcol],
[nullrow,nullcol-1],
[nullrow,nullcol+1]
].filter(d => d[0]>=0 && d[0]< dimensions[0] && d[1]>=0 && d[1]<dimensions[1])
.map(d => elements[d[0]*dimensions[1]+d[1]])
.filter(el => el!==lastelement);
let winner = candidates[Math.floor(Math.random()*candidates.length)];
let winneridx = elements.indexOf(winner);
elements[winneridx] = elements[nullidx];
elements[nullidx] = winner;
lastelement = winner;
}
return elements;
};
const defaultDimensions = [5,5];
const defaultState = {
dimensions: defaultDimensions,
elements: generateElements(defaultDimensions),
moveCounter: 0
}
function reducer(state = defaultState,action = {}) {
switch(action.type) {
case SHUFFLE:
return {
...state,
elements: generateElements(state.dimensions),
moveCounter: 0
}
case AUTOMOVETILE:
let newels = autoMoveTile(state,action.tileid);
if(newels !== null) {
return {
...state,
elements: newels,
moveCounter: state.moveCounter+1
}
} else {
return state;
}
case CHANGEDIMENSIONS:
return {
...state,
dimensions: action.dimensions,
elements: generateElements(action.dimensions),
moveCounter: 0
}
default:
return state;
}
}
const store = {
_listeners: [],
state: defaultState,
registerListener(callback) {
this._listeners.push(callback);
},
unregisterListener(callback) {
this._listeners = this._listeners.filter(listener !== callback);
},
dispatch(action) {
this.state = reducer(this.state,action);
this._listeners.forEach(listener => listener.call(null,this.state));
}
};
function generateTiles(dimensions,elements) {
return elements.filter(element => element.id !== null).map(element => {
let newel = document.createElement("div");
newel.dataset.tileid = element.id;
newel.classList.add("tile");
let innerel = document.createElement("div");
innerel.classList.add("tile-inner");
let textel = document.createElement("div");
textel.innerText = element.id;
innerel.appendChild(textel);
newel.appendChild(innerel);
newel.onclick = () => {
store.dispatch({
type: AUTOMOVETILE,
tileid: element.id
});
}
return newel;
})
}
function createBoard(dimensions) {
let retel = document.createElement("div");
retel.classList.add("board");
retel.style.display = "grid";
retel.style.gridTemplateColumns = `repeat(${dimensions[1]}, 1fr)`;
retel.style.gridTemplateRows = `repeat(${dimensions[0]}, 1fr)`;
return retel;
}
function createForm(value) {
let retel = document.createElement("div");
retel.classList.add("controls");
let label = document.createElement("div");
label.innerText = "Board size: ";
label.classList.add("spinner-label");
let downButton = document.createElement("button");
downButton.innerText = "-";
downButton.onclick = evt => {
if(value>3) {
store.dispatch({
type: CHANGEDIMENSIONS,
dimensions: [value-1,value-1]
});
}
}
downButton.classList.add("down-button");
let valueDisplay = document.createElement("div");
valueDisplay.innerText = `${store.state.dimensions[0]}`
valueDisplay.classList.add("value-display");
let upButton = document.createElement("button");
upButton.innerText = "+";
upButton.onclick = evt => {
if(value<10) {
store.dispatch({
type: CHANGEDIMENSIONS,
dimensions: [value+1,value+1]
});
}
}
upButton.classList.add("up-button");
let shuffleButton = document.createElement("button");
shuffleButton.innerText = "Shuffle";
shuffleButton.onclick = evt => {
store.dispatch({
type: SHUFFLE
});
}
shuffleButton.classList.add("shuffle-button");
retel.appendChild(label);
retel.appendChild(downButton);
retel.appendChild(valueDisplay);
retel.appendChild(upButton);
retel.appendChild(shuffleButton);
return retel;
}
const getPopupInner = ({moves=0, best=false, bestMoves=0}) => {
let inner = document.createElement("div");
inner.innerHTML = `<div class="title">You won!!!</div><div class="moves">Moves: ${moves}</div>${best?`<div class="best">🏆A personal best! 🏆</div>`:`<div class="record">Your record: ${bestMoves}</div>`}`;
let resetButton = document.createElement("button");
resetButton.classList.add("reset-button");
resetButton.innerText = "New game"
resetButton.onclick = () => {
store.dispatch({
type: SHUFFLE
});
}
inner.appendChild(resetButton);
return inner;
};
let htmlElements;
let frame;
let controls;
let board;
let lastDimensions;
let winningel;
let popup;
let status;
function render(root, state, first=false) {
if(first || lastDimensions[0]!==state.dimensions[0] || lastDimensions[1]!==state.dimensions[1]) {
frame = document.createElement("div");
frame.classList.add("frame");
controls = createForm(state.dimensions[0]);
board = createBoard(state.dimensions);
lastDimensions = state.dimensions;
htmlElements = generateTiles(state.dimensions,state.elements);
popup = document.createElement("div");
popup.classList.add("popup");
status = document.createElement("div");
status.classList.add("status");
root.innerHTML = "";
htmlElements.forEach(htmlel => board.appendChild(htmlel));
frame.appendChild(board);
frame.appendChild(status)
frame.appendChild(controls);
root.appendChild(frame);
root.appendChild(popup)
}
if(htmlElements) {
state.elements.forEach((el,i,arr) => {
if(el.id===null) {return;}
let curel = htmlElements.find(htmlel => htmlel.dataset.tileid === el.id);
let [row,col] = getRowAndCol(i,state.dimensions);
curel.style.gridRow = `${row+1}`;
curel.style.gridColumn = `${col+1}`;
})
status.innerText = `Moves: ${state.moveCounter}`
if(checkWinningConditions(state.elements)) {
let bestmoves = localStorage.getItem("bestMoves_"+state.dimensions.join("x"));
if(bestmoves === null || state.moveCounter < bestmoves ) {
popup.innerHTML = "";
popup.appendChild(getPopupInner({moves: state.moveCounter, best: true}));
localStorage.setItem("bestMoves_"+state.dimensions.join("x"),state.moveCounter);
} else {
popup.innerHTML = "";
popup.appendChild(getPopupInner({moves: state.moveCounter, best: false, bestMoves: bestmoves}));
}
popup.classList.add("winning");
} else {
popup.classList.remove("winning");
}
}
}
let approot = document.querySelector("#root")
render(approot,store.state,true);
store.registerListener(state => render(approot,state));
</script>
</body>
</html>