-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048.html
More file actions
664 lines (609 loc) · 30 KB
/
Copy path2048.html
File metadata and controls
664 lines (609 loc) · 30 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
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
653
654
655
656
657
658
659
660
661
662
663
664
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>2048 In Javascript, Crummy Text Edition</title>
<!-- 2014-06-23 Mon 18:02 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="generator" content="Org-mode" />
<meta name="author" content="Josh" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center; }
.todo { font-family: monospace; color: red; }
.done { color: green; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right { margin-left: auto; margin-right: 0px; text-align: right; }
.left { margin-left: 0px; margin-right: auto; text-align: left; }
.center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.right { text-align: center; }
th.left { text-align: center; }
th.center { text-align: center; }
td.right { text-align: right; }
td.left { text-align: left; }
td.center { text-align: center; }
dt { font-weight: bold; }
.footpara:nth-child(2) { display: inline; }
.footpara { display: block; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/javascript" src="http://orgmode.org/mathjax/MathJax.js"></script>
<script type="text/javascript">
<!--/*--><![CDATA[/*><!--*/
MathJax.Hub.Config({
// Only one of the two following lines, depending on user settings
// First allows browser-native MathML display, second forces HTML/CSS
// config: ["MMLorHTML.js"], jax: ["input/TeX"],
jax: ["input/TeX", "output/HTML-CSS"],
extensions: ["tex2jax.js","TeX/AMSmath.js","TeX/AMSsymbols.js",
"TeX/noUndefined.js"],
tex2jax: {
inlineMath: [ ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"], ["\\begin{displaymath}","\\end{displaymath}"] ],
skipTags: ["script","noscript","style","textarea","pre","code"],
ignoreClass: "tex2jax_ignore",
processEscapes: false,
processEnvironments: true,
preview: "TeX"
},
showProcessingMessages: true,
displayAlign: "center",
displayIndent: "2em",
"HTML-CSS": {
scale: 100,
availableFonts: ["STIX","TeX"],
preferredFont: "TeX",
webFont: "TeX",
imageFont: "TeX",
showMathMenu: true,
},
MMLorHTML: {
prefer: {
MSIE: "MML",
Firefox: "MML",
Opera: "HTML",
other: "HTML"
}
}
});
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">2048 In Javascript, Crummy Text Edition</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1. Our Algorithms</a>
<ul>
<li><a href="#sec-1-1">1.1. Sequence Generation</a></li>
<li><a href="#sec-1-2">1.2. Collapsing the Board</a></li>
<li><a href="#sec-1-3">1.3. Checking for moves</a></li>
<li><a href="#sec-1-4">1.4. Placing Random Values</a></li>
</ul>
</li>
<li><a href="#sec-2">2. Putting it together (in a console)</a></li>
<li><a href="#sec-3">3. Conclusions</a></li>
</ul>
</div>
</div>
<p>
I've seen a few posts going over how much more concisely you can
implement 2048 in Haskel than you can in the likes of Javascript or
Python. I'm not at all sure that is an accurate statement. Rather
than just think that doesn't sound right, I thought I would give a
stab at implementing 2048.
</p>
<p>
So, here we go.
</p>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> Our Algorithms</h2>
<div class="outline-text-2" id="text-1">
<p>
2048 is a game where you have a "grid" of 16 cells. A cell either
has a value or is blank. Cells that have values can be made to move
in any of the four main directions until they hit an occupied cell
or a "wall." When a collision happens between cells of equal value,
they are "merged" into a cell with their combined values.
</p>
<p>
Looking at all of the cells of the grid and number them 0 to 15, we
can see there are very obvious sequences to define how we look at
moving cells. In particular, to move up means to look at the
sequences \(\{12, 8, 4, 0\}, \{13, 9, 5, 1\}, \{14, 10, 6, 2\}, \{15,
11, 7, 3\}\), whereas down is the reverse of these. Similarly, left
and right have clear sequences, as well.
</p>
\begin{equation}
\begin{bmatrix}
0 & 1 & 2 & 3 \\
4 & 5 & 6 & 7 \\
8 & 9 & 10 & 11 \\
12 & 13 & 14 & 15
\end{bmatrix}
\end{equation}
<p>
Our task, then, is to run along these sequences looking for a
merge/slide candidate at a position, then to move to the next
position. If we are up for the task of calculating these positions,
then the algorithm is complicated a little in order to have to do
these calculations at each step. (That is, I'm sure you could mix
the logic for looking for a slide/merge and looking for the next
block to test.)
</p>
<p>
Instead, we'll break this up into a few main algorithms. In fact,
to keep things very simple, I'll be making three main algorithms.
One to make a set of horizontal sequences, one to make vertical
sequences, and the last to run through each index checking for a
"merge."
</p>
</div>
<div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> Sequence Generation</h3>
<div class="outline-text-3" id="text-1-1">
<p>
We begin with the algorithm to generate the sequences. We start
having been given the width (\(w\)), height (\(h\)), whether or not we
are doing a "left/right" sequence (\(lr\)), an operation to initialize
a collection (\(INIT\)), an operation which is responsible for adding
to a collection (\(ADD\)), and a an operation that will calculate the
current value we are adding (\(CALC\_CURRENT\)). We will use
variables to maintain what row we are on (\(r\)), what column we are
on (\(c\)), the collection of all sequences (\(a\)), finally the current
sequence we are making (\(s\)).
</p>
<p>
The values that we will have for \(CALC\_CURRENT\) will be \(r \times
w + c\) for horizontal sequences, and \(r + c \times h\) for vertical
ones.
</p>
<ol class="org-ol">
<li>[initialize] \(a \gets INIT()\), \(r \gets 0\)
</li>
<li>[swap w&h?] if \(!lr\), swap \(w\) and \(h\)
</li>
<li>[begin current list] \(s \gets INIT()\), \(c \gets 0\)
</li>
<li>[add list to total] \(ADD(a, s)\)
</li>
<li>[add next value to s] \(ADD(s, CALC\_CURRENT(w, h, r, c))\)
</li>
<li>[increment c] \(c \gets c + 1\), if \(c < w\) goto 4.
</li>
<li>[increment r] \(r \gets r + 1\), if \(r < h\) goto 2.
</li>
<li>[terminate] \(a\) contains all subsequences
</li>
</ol>
<p>
Not going to lie, that actually looks and sounds more complicated
than the code. I'm new and/or not good at this. Apologies for the
far from idiomatic JavaScript. Keeping it closer to how I wrote the
algorithm above. And yes, even that could probably be cleaned up.
</p>
<div class="org-src-container">
<pre class="src src-js" id="make_sequences"><span style="color: #F0DFAF; font-weight: bold;">function</span> <span style="color: #93E0E3;">makeSequences</span>(<span style="color: #DFAF8F;">w</span>, <span style="color: #DFAF8F;">h</span>, <span style="color: #DFAF8F;">lr</span>, <span style="color: #DFAF8F;">INIT</span>, <span style="color: #DFAF8F;">ADD</span>, <span style="color: #DFAF8F;">CALC_CURRENT</span>) {
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">c</span> = 0, <span style="color: #DFAF8F;">r</span> = 0, <span style="color: #DFAF8F;">s</span> = <span style="color: #BFEBBF;">null</span>, <span style="color: #DFAF8F;">a</span> = INIT();
<span style="color: #F0DFAF; font-weight: bold;">if</span> (!lr) {
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">x</span> = w;
w = h;
h = x;
}
<span style="color: #F0DFAF; font-weight: bold;">for</span> (r; r < h; r++) {
s = INIT();
ADD(a, s);
<span style="color: #F0DFAF; font-weight: bold;">for</span> (c = 0; c < w; c++) {
ADD(s, CALC_CURRENT(w, h, r, c));
}
}
<span style="color: #F0DFAF; font-weight: bold;">return</span> a;
};
</pre>
</div>
<p>
Again, not going to pretend that calling this function is the
cleanest thing in the world. Especially if I was willing to code
golf, I could just have it such that we did not have to pass the
width and height to all functions. No matter, it isn't that
bad. (For that matter, if I was just wanting to implement a 4x4 grid
for this, I would just code in the sequence and be done with it.
Looking forward to possibly playing with different size/shape
boards.)
</p>
<p>
At startup, we will initialize all four sets of our sequences such
that they can be looked up in a map by "up", "down", "left", and
"right." As so:
</p>
<div class="org-src-container">
<pre class="src src-js" id="init_sequences"><span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">sequences</span> = {
<span style="color: #CC9393;">'left'</span>: makeSequences(width, height, <span style="color: #BFEBBF;">true</span>,
<span style="color: #F0DFAF; font-weight: bold;">function</span>() { <span style="color: #F0DFAF; font-weight: bold;">return</span> []; },
<span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">c</span>, <span style="color: #DFAF8F;">v</span>) {c.push(v);},
<span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">w</span>, <span style="color: #DFAF8F;">h</span>, <span style="color: #DFAF8F;">r</span>, <span style="color: #DFAF8F;">c</span>) {<span style="color: #F0DFAF; font-weight: bold;">return</span> r * w + c;}),
<span style="color: #CC9393;">'right'</span>: makeSequences(width, height, <span style="color: #BFEBBF;">true</span>,
<span style="color: #F0DFAF; font-weight: bold;">function</span>() { <span style="color: #F0DFAF; font-weight: bold;">return</span> []; },
<span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">c</span>, <span style="color: #DFAF8F;">v</span>) {c.unshift(v);},
<span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">w</span>, <span style="color: #DFAF8F;">h</span>, <span style="color: #DFAF8F;">r</span>, <span style="color: #DFAF8F;">c</span>) {<span style="color: #F0DFAF; font-weight: bold;">return</span> r * w + c;}),
<span style="color: #CC9393;">'up'</span>: makeSequences(width, height, <span style="color: #BFEBBF;">false</span>,
<span style="color: #F0DFAF; font-weight: bold;">function</span>() { <span style="color: #F0DFAF; font-weight: bold;">return</span> []; },
<span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">c</span>, <span style="color: #DFAF8F;">v</span>) {c.push(v);},
<span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">w</span>, <span style="color: #DFAF8F;">h</span>, <span style="color: #DFAF8F;">r</span>, <span style="color: #DFAF8F;">c</span>) {<span style="color: #F0DFAF; font-weight: bold;">return</span> r + c * h;}),
<span style="color: #CC9393;">'down'</span>: makeSequences(width, height, <span style="color: #BFEBBF;">false</span>,
<span style="color: #F0DFAF; font-weight: bold;">function</span>() { <span style="color: #F0DFAF; font-weight: bold;">return</span> []; },
<span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">c</span>, <span style="color: #DFAF8F;">v</span>) {c.unshift(v);},
<span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">w</span>, <span style="color: #DFAF8F;">h</span>, <span style="color: #DFAF8F;">r</span>, <span style="color: #DFAF8F;">c</span>) {<span style="color: #F0DFAF; font-weight: bold;">return</span> r + c * h;})
};
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-1-2" class="outline-3">
<h3 id="sec-1-2"><span class="section-number-3">1.2</span> Collapsing the Board</h3>
<div class="outline-text-3" id="text-1-2">
<p>
Now that we have our sequences, we need our main algorithm. At a
high level, for each sequence, we basically slide two pointers
across it looking for either a slide, a merge, or simple advance.
Things are a little interesting when we do a slide, because we could
still merge the tile that was slid. If we do a merge, we won't
consider the tile again. Also, there is no need to look at all
pairs, as soon as the right focus reaches the end, we are done.
</p>
<p>
So, our overall algorithm is:
</p>
<ol class="org-ol">
<li>[initialize] \(l \gets 0\)
</li>
<li>[reset r] \(r \gets l\)
</li>
<li>[advance r] \(r \gets r + 1\), if \(r > w\) terminate
</li>
<li>[test r] if \(b[r]\) is empty goto 3
</li>
<li>[slide?] if \(b[l] is\) empty, b[l] <- b[r], b[r] <- empty, goto 3
</li>
<li>[merge?] if b[l] = b[r], b[l] <- b[l] + b[r]
</li>
<li>[advance l] l = l + 1, goto 2.
</li>
</ol>
<p>
In javascript, this is the rather interesting looking code below.
The only modification we really need is a flag to indicate that we
are checking for moves and not actually performing any. That is, if
the game is unable to place random pieces, we need to know if the
user is able to make any more moves.
</p>
<div class="org-src-container">
<pre class="src src-js" id="collapse_by_sequence"><span style="color: #F0DFAF; font-weight: bold;">function</span> <span style="color: #93E0E3;">collapseBySequence</span>(<span style="color: #DFAF8F;">b</span>, <span style="color: #DFAF8F;">s</span>) {
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">l</span> = 0,
r = 0;
<span style="color: #F0DFAF; font-weight: bold;">for</span> (r = 1; r < s.length; r++) {
<span style="color: #F0DFAF; font-weight: bold;">if</span> (b[s[r]] === 0) {
<span style="color: #F0DFAF; font-weight: bold;">continue</span>;
}
<span style="color: #F0DFAF; font-weight: bold;">if</span> (b[s[l]] === 0) {
b[s[l]] = b[s[r]];
b[s[r]] = 0;
<span style="color: #F0DFAF; font-weight: bold;">continue</span>;
}
<span style="color: #F0DFAF; font-weight: bold;">if</span> (b[s[l]] === b[s[r]]) {
b[s[l]] = b[s[l]] + b[s[r]];
b[s[r]] = 0;
}
l = l + 1;
r = l;
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-1-3" class="outline-3">
<h3 id="sec-1-3"><span class="section-number-3">1.3</span> Checking for moves</h3>
<div class="outline-text-3" id="text-1-3">
<p>
We do not always want to perform a collapse when inspecting the
game board. In particular, when we are checking for "game over."
Originally, I just tacked on some extra lines in the collapse
method, however, that is getting rather heavy and I'd rather keep
things simpler.
</p>
<p>
So, we have this modification of that method to simply return
whether or not there are possible moves for a sequence. As can be
seen, it is the same code, just without the modifications to the
underlying board state.
</p>
<div class="org-src-container">
<pre class="src src-js" id="check_sequence_for_moves"><span style="color: #F0DFAF; font-weight: bold;">function</span> <span style="color: #93E0E3;">checkForMoves</span>(<span style="color: #DFAF8F;">b</span>, <span style="color: #DFAF8F;">s</span>) {
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">l</span> = 0,
r = 0;
<span style="color: #F0DFAF; font-weight: bold;">for</span> (r = 1; r < s.length; r++) {
<span style="color: #F0DFAF; font-weight: bold;">if</span> (b[s[l]] === 0) {
<span style="color: #F0DFAF; font-weight: bold;">return</span> <span style="color: #BFEBBF;">true</span>
}
<span style="color: #F0DFAF; font-weight: bold;">if</span> (b[s[l]] === b[s[r]]) {
<span style="color: #F0DFAF; font-weight: bold;">return</span> <span style="color: #BFEBBF;">true</span>;
}
l = l + 1;
r = l;
}
<span style="color: #F0DFAF; font-weight: bold;">return</span> <span style="color: #BFEBBF;">false</span>;
}
</pre>
</div>
<p>
We will also go ahead and make our method that checks for any
moves. This is simply chaining the above method with all of the
sequences.
</p>
<div class="org-src-container">
<pre class="src src-js" id="check_all_sequences_for_moves"><span style="color: #F0DFAF; font-weight: bold;">function</span> <span style="color: #93E0E3;">checkSequencesForMoves</span>() {
<span style="color: #F0DFAF; font-weight: bold;">for</span> (<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">d</span> <span style="color: #F0DFAF; font-weight: bold;">in</span> sequences) {
<span style="color: #F0DFAF; font-weight: bold;">for</span> (<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">s</span> <span style="color: #F0DFAF; font-weight: bold;">in</span> sequences[d])
<span style="color: #F0DFAF; font-weight: bold;">if</span> (checkForMoves(board, sequences[d][s]))
<span style="color: #F0DFAF; font-weight: bold;">return</span> <span style="color: #BFEBBF;">true</span>;
}
<span style="color: #F0DFAF; font-weight: bold;">return</span> <span style="color: #BFEBBF;">false</span>;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-1-4" class="outline-3">
<h3 id="sec-1-4"><span class="section-number-3">1.4</span> Placing Random Values</h3>
<div class="outline-text-3" id="text-1-4">
<p>
After that, the only real "algorithmic" part I need is a way to
place random values. I'm not going to claim that random values are
a strong point, so I'm tacking "ish" to this. Basic idea is find
all indexes that have a zero and then randomly pick two.
</p>
<div class="org-src-container">
<pre class="src src-js" id="place_random_ish"><span style="color: #F0DFAF; font-weight: bold;">function</span> <span style="color: #93E0E3;">placeRandomIsh</span>(<span style="color: #DFAF8F;">board</span>, <span style="color: #DFAF8F;">allowedValues</span>, <span style="color: #DFAF8F;">sequences</span>) {
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">i</span>, <span style="color: #DFAF8F;">c</span> = [];
<span style="color: #F0DFAF; font-weight: bold;">for</span> (<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">i</span> = 0; i < board.length; i++) {
<span style="color: #F0DFAF; font-weight: bold;">if</span> (board[i] === 0) {
c.push(i);
}
}
<span style="color: #F0DFAF; font-weight: bold;">if</span> (c.length === 0) {
<span style="color: #F0DFAF; font-weight: bold;">return</span> checkSequencesForMoves();
} <span style="color: #F0DFAF; font-weight: bold;">else</span> {
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">x</span> = Math.floor(Math.random() * c.length);
board[c[x]] = allowedValues[Math.floor(Math.random() * allowedValues.len<span style="color: #DC8CC3; background-color: #3F3F3F;">gth)];</span>
}
<span style="color: #F0DFAF; font-weight: bold;">return</span> <span style="color: #BFEBBF;">true</span>;
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2"><span class="section-number-2">2</span> Putting it together (in a console)</h2>
<div class="outline-text-2" id="text-2">
<p>
With all of that, we are ready to put the code together into
something we can run from the console. I'm not exactly a node
expert. Or well, node programmer for that matter. Some quick
searching shows I really just need to set a couple of switches and
to import one library to get what I want.
</p>
<p>
Ultimately, this is much easier than a GUI would be, since I just
have to display the state of the board after each move. If I still
have the energy in a couple of days, I'll modify the collapse code
such that I can animate merges and slides appropriately. (This is
something I feel will be much easier, since at each merge/slide, I
know exactly which blocks are changing. The traditional
"functional" styles for this make that a bit more difficult to
figure out.)
</p>
<p>
I did keep the "make move" and a few other pieces of code in this
section, since that will ultimately be different when/if I get
around to doing things in a browser. At least, I suspect they will
be.
</p>
<div class="org-src-container">
<pre class="src src-js" id="crummy_console"><span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">sprintf</span>=require(<span style="color: #CC9393;">"sprintf-js"</span>).sprintf;
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">stdin</span> = process.stdin;
stdin.setRawMode(<span style="color: #BFEBBF;">true</span>);
stdin.resume();
<span style="color: #F0DFAF; font-weight: bold;">function</span> <span style="color: #93E0E3;">Game</span>(<span style="color: #DFAF8F;">width</span>, <span style="color: #DFAF8F;">height</span>) {
<<make_sequences>>
<<init_sequences>>
<<collapse_by_sequence>>
<<check_sequence_for_moves>>
<<check_all_sequences_for_moves>>
<<place_random_ish>>
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">board</span> = [];
<span style="color: #F0DFAF; font-weight: bold;">for</span> (<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">i</span> = 0; i < (width * height); i++) {
board.push(0);
}
<span style="color: #F0DFAF; font-weight: bold;">function</span> <span style="color: #93E0E3;">showBoard</span>() {
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">line</span> = <span style="color: #CC9393;">""</span>;
<span style="color: #F0DFAF; font-weight: bold;">for</span> (<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">i</span> = 0; i < board.length; i++) {
<span style="color: #F0DFAF; font-weight: bold;">if</span> (i % width === 0) {
console.log(line);
line = <span style="color: #CC9393;">""</span>;
}
line += sprintf(<span style="color: #CC9393;">"%5d"</span>, board[i]);
}
console.log(line);
}
<span style="color: #BFEBBF;">this</span>.makeMove = <span style="color: #F0DFAF; font-weight: bold;">function</span>(<span style="color: #DFAF8F;">d</span>) {
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">updated</span> = <span style="color: #BFEBBF;">false</span>;
<span style="color: #F0DFAF; font-weight: bold;">for</span> (<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">s</span> <span style="color: #F0DFAF; font-weight: bold;">in</span> sequences[d]) {
updated = updated || checkForMoves(board, sequences[d][s]);
collapseBySequence(board, sequences[d][s]);
}
<span style="color: #F0DFAF; font-weight: bold;">if</span> (updated)
placeRandomIsh(board, [2,2,2,2,4], sequences);
<span style="color: #F0DFAF; font-weight: bold;">if</span> (!checkSequencesForMoves())
console.log(<span style="color: #CC9393;">"Game Over"</span>)
showBoard();
}
placeRandomIsh(board, [2], sequences);
placeRandomIsh(board, [2], sequences);
showBoard();
}
<span style="color: #F0DFAF; font-weight: bold;">var</span> <span style="color: #DFAF8F;">game</span> = <span style="color: #F0DFAF; font-weight: bold;">new</span> <span style="color: #7CB8BB;">Game</span>(4,4);
stdin.on(<span style="color: #CC9393;">'data'</span>, <span style="color: #F0DFAF; font-weight: bold;">function</span> (<span style="color: #DFAF8F;">key</span>) {
<span style="color: #F0DFAF; font-weight: bold;">if</span> (key == <span style="color: #CC9393;">'\u0003'</span>) process.exit();
<span style="color: #F0DFAF; font-weight: bold;">if</span> (key == <span style="color: #CC9393;">'w'</span>) game.makeMove(<span style="color: #CC9393;">"up"</span>);
<span style="color: #F0DFAF; font-weight: bold;">if</span> (key == <span style="color: #CC9393;">'a'</span>) game.makeMove(<span style="color: #CC9393;">"left"</span>);
<span style="color: #F0DFAF; font-weight: bold;">if</span> (key == <span style="color: #CC9393;">'s'</span>) game.makeMove(<span style="color: #CC9393;">"down"</span>);
<span style="color: #F0DFAF; font-weight: bold;">if</span> (key == <span style="color: #CC9393;">'d'</span>) game.makeMove(<span style="color: #CC9393;">"right"</span>);
});
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3"><span class="section-number-2">3</span> Conclusions</h2>
<div class="outline-text-2" id="text-3">
<p>
I hasten to add a note saying I don't think you can really draw any
conclusions on the verbosity of javascript here. This is upwards of
130 lines of code. Though, about 30 of that is because I did not
just hard code the sequences.
</p>
<p>
It isn't like that would be a necessarily fair comparison, either.
For one, I'm not a good programmer. :) For two, without the
document that the code is tangled from (this one), I can make no
pretense that the code is obvious. I do not feel it is completely
unapproachable, but not obvious.
</p>
<p>
I do believe this code is a lot easier to reason about when it comes
to performance characteristics. Though, I can't imagine that is a
concern for this game.
</p>
<p>
I would like to extend this code into an actual GUI someday. That
will likely require reworking parts of this to support. So, I'll
treat this part as a "done deal" for now. Hopefully someone has
some enjoyment reading it.
</p>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Josh</p>
<p class="date">Created: 2014-06-23 Mon 18:02</p>
<p class="creator"><a href="http://www.gnu.org/software/emacs/">Emacs</a> 24.4.50.3 (<a href="http://orgmode.org">Org</a> mode beta_8.3)</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>