-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
two_zero.cljs
447 lines (376 loc) · 15.5 KB
/
two_zero.cljs
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
(ns devdemos.two-zero
(:require
[devcards.core]
[devcards.util.utils :refer [html-env?]]
[clojure.string :as string]
[clojure.set :refer [difference union]]
[sablono.core :as sab :include-macros true]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[cljs.core.async :refer [timeout]]
[goog.labs.userAgent.device :as device]
[cljs.test :as t :include-macros true])
(:require-macros
[cljs.core.async.macros :refer [go]]
[devcards.core :as dc :refer [defcard defcard-doc deftest]]))
(defn lh [x] (prn-str x) x)
(defn lc [x] (.log js/console x) x)
(def is-mobile?
(when (html-env?)
(or (device/isMobile)
;; we could hook into a callback to set this on resize
(< (.-offsetWidth
(aget (.getElementsByTagName js/document "body") 0))
490))))
(defcard
"# 2048
Let's build 2048 interactively with devcards")
(def to-pixel-pos
(if is-mobile?
(fn [x] (+ 8 (* x (+ 60 8))))
(fn [x] (+ 15 (* x (+ 106 15))))))
(defn pixel-pos [pos]
(str (to-pixel-pos pos) "px"))
(defn board-cell [{:keys [top left id v highlight reveal]}]
(let [translate (str "translate3d("
(pixel-pos left) ","
(pixel-pos top) ", 0px)")]
(sab/html
[:div.cell-pos { :style { "-webkit-transform" translate
"-moz-transform" translate
"transform" translate }
:key (str (name id)) }
[:div { :class (str "cell cell-num-" v
(when highlight " highlight")
(when reveal " reveal")) } v]])))
;; These are the background cells of the board, they never change.
(defn background-cells []
(for [top (range 4) left (range 4)]
(sab/html
[:div.cell-pos.cell-empty
{ :style { :top (pixel-pos top)
:left (pixel-pos left)}}])))
(defn one-row-board-static
"Only used for the demo process"
[data]
(sab/html
[:div.board-area.board-area-one-row
[:div.background (take 4 (background-cells))]
[:div.cells (map board-cell data)]]))
(defn game-board [data]
(sab/html
[:div.board-area
[:div.background (background-cells)]
[:div.cells (map board-cell data)]]))
(defcard board-style
"## Board Style
Let's start by creating the style for the board.
The board is a 4x4 board. It will have one container with 16
absolutely positioned cells in it. These cells will mark where the
potential locations for the game tiles."
(game-board []))
(defcard board-with-cells
"### Cell Style
Then we'll work on the style for the cells. The hard part is
getting the colors and the font sizes correct."
(game-board [{ :top 0 :left 0 :v 2 :id :t1}
{ :top 0 :left 1 :v 4 :id :t2}
{ :top 0 :left 2 :v 8 :id :t3}
{ :top 0 :left 3 :v 16 :id :t4}
{ :top 1 :left 0 :v 32 :id :t5}
{ :top 1 :left 1 :v 64 :id :t6}
{ :top 1 :left 2 :v 128 :id :t7}
{ :top 1 :left 3 :v 256 :id :t8}
{ :top 2 :left 0 :v 512 :id :t9}
{ :top 2 :left 1 :v 1024 :id :t10}
{ :top 2 :left 2 :v 2048 :id :t11}]))
#_(defcard animation-work
"## Checking basic tile movement animation"
(dc/slider-card
identity
{ :left (range 4) }
:value-render-func
(fn [{:keys [left]}]
(one-row-board-static [{:left left :top 0 :v 2 :id :t1}]))))
(defcard-doc
"## Main data structures
#### Tile Map
The only data structure we are going to hold in our atom is a map
of the tiles on the page. The list will look like this:"
(dc/mkdn-pprint-str
{:t1 { :top 0 :left 0 :v 2 :id :t1}
:t2 { :top 1 :left 0 :v 4 :id :t2}
:t3 { :top 2 :left 0 :v 8 :id :t3}
:t4 { :top 3 :left 0 :v 4 :id :t4}})
"Each tile will have an `:id`, a value `:v` and a position on the board.
Data handling is complicated because we will have to manage the
state in phases to support animation. Tiles will be marked with the
following flags:
* `:double` - this flag marks the tiles value to be doubled
* `:remove` - this flag marks the tile to be removed
* `:reveal` - this flag marks the tile to be rendered with the **reveal** class
* `:highlight` - this flag marks the tile to be rendered with the **highlight** class
All of these flags are temporary markings that will be removed in
a couple of phases of animation right after a move is made.
#### Board view
The tile map will be converted to a board view that will look like this:"
(dc/mkdn-pprint-str
[[{:v 2, :id :t1} :_ :_ :_]
[{:v 8, :id :t2} :_ :_ :_]
[{:v 4, :id :t3} :_ :_ :_]
[{:v 4, :id :t4} :_ :_ :_]])
"The board view removes the position data as it is implicit.
The board view is used to compute the board transformation given a
`:left`, `:right`, `:up`, or `:down` move. For instance if we move
the above board to the `:right` it will end up looking like this:"
(dc/mkdn-pprint-str
[[:_ :_ :_ {:v 2, :id :t1}]
[:_ :_ :_ {:v 8, :id :t2}]
[:_ :_ :_ {:v 4, :id :t3}]
[:_ :_ :_ {:v 4, :id :t4}]])
"And then if we move it `:down` it will look like this."
(dc/mkdn-pprint-str
[[:_ :_ :_ :_]
[:_ :_ :_ {:v 2, :id :t1}]
[:_ :_ :_ {:v 8, :id :t2}]
[:_ :_ :_ {:v 4, :id :t3 :double true :replaces :t4}]])
"Once we have transformed the board we will turn it back into the tile map."
(dc/mkdn-pprint-str
{:t1 { :top 1 :left 3 :v 2 :id :t1}
:t2 { :top 0 :left 3 :v 8 :id :t2}
:t3 { :top 0 :left 3 :v 4 :id :t3 :double true }
:t4 { :top 0 :left 3 :v 4 :id :t4 :remove true }}))
(defcard-doc
"### Transforming one row
We need to get a transformation for one row. From there we can get
all the other transformations.
We will be transforming one row for a move `:left`.")
(def remove-blanks (partial filterv #(not= % :_)))
(defn pad-with [length item xs]
(vec (concat xs (take (- length (count xs)) (repeat item)))))
(def pad-blanks (partial pad-with 4 :_))
(defn combine? [tile-1 tile-2]
(cond
(nil? tile-1) false
(nil? tile-2) false
(:double tile-1) false
(:double tile-2) false
(= (:v tile-1) (:v tile-2)) true
:else false))
(defn combine [tile-1 tile-2]
(assoc tile-2 :double true :replaces (:id tile-1)))
(defn tile-reducer [accum tile]
(if (combine? (last accum) tile)
(conj (vec (butlast accum)) (combine (last accum) tile))
(conj accum tile)))
;; this function reduces one row or the board to the left
(def transform-row
(comp pad-blanks
(partial reduce tile-reducer [])
remove-blanks))
(deftest transform-row-tests
"In a move `:left` all the blanks will end up on the right.
So let's `remove-blanks` first"
(t/is (= (remove-blanks [:_ :_ :_ :_]) []))
(t/is (= (remove-blanks [:_ :_ :_ {:v 8}]) [{:v 8}]))
"We should also be able to `pad-blanks` back to finish the move."
(t/is (= (pad-blanks [{:v 8}]) [{:v 8} :_ :_ :_]))
"Next we are going to create a function to reduce the row. And to
help us we are going to create a predicate `combine?` to tell us if we
can combine two tiles."
(t/is (combine? {:v 2} {:v 2}))
(t/is (not (combine? {:v 2 :double true} {:v 2})))
(t/is (not (combine? {:v 2} {:v 2 :double true})))
(t/is (not (combine? {:v 2} {:v 4})))
"Then we need a function to help us `combine` the two tiles."
(t/is (= (combine {:id :t1 :v 2} {:id :t2 :v 2})
{:id :t2, :v 2, :double true, :replaces :t1}))
"We can then assemble the above functions into `transform-row`
which will take a row and do a 2048 move to the left."
(t/is (= (transform-row [:_ :_ :_ :_]) [:_ :_ :_ :_]))
(t/is (= (transform-row [{:v 8} :_ :_ :_]) [{:v 8} :_ :_ :_]))
(t/is (= (transform-row [ :_ {:v 8} :_ :_]) [{:v 8} :_ :_ :_]))
(t/is (= (transform-row [ :_ :_ {:v 8} :_]) [{:v 8} :_ :_ :_]))
(t/is (= (transform-row [ :_ :_ :_ {:v 8}]) [{:v 8} :_ :_ :_]))
(t/is (= (transform-row [ {:v 4 :id 1} {:v 4 :id 2} :_ :_])
[{:v 4 :id 2 :double true :replaces 1} :_ :_ :_]))
(t/is (= (transform-row [ {:v 4 :id 1} :_ :_ {:v 4 :id 2}])
[{:v 4 :id 2 :double true :replaces 1} :_ :_ :_]))
(t/is (= (transform-row [ :_ {:v 4 :id 1} :_ {:v 4 :id 2}])
[{:v 4 :id 2 :double true :replaces 1} :_ :_ :_]))
(t/is (= (transform-row [ :_ :_ {:v 4 :id 1} {:v 4 :id 2}])
[{:id 2 :v 4 :double true :replaces 1} :_ :_ :_]))
(t/is (= (transform-row [ :_ {:id 1 :v 4} {:id 2 :v 4} {:id 3 :v 4}])
[{:id 2 :v 4 :double true :replaces 1} {:id 3 :v 4} :_ :_]))
(t/is (= (transform-row [ {:id 1 :v 4} {:id 2 :v 4} {:id 3 :v 4} {:id 4 :v 4}])
[{:id 2 :v 4 :double true :replaces 1} {:id 4 :v 4 :double true :replaces 3} :_ :_]))
(t/is (= (transform-row [ {:id 1 :v 4} {:id 2 :v 8} {:id 3 :v 8} {:id 4 :v 4}])
[{:id 1 :v 4} {:id 3 :v 8 :double true :replaces 2 } {:id 4 :v 4} :_]))
(t/is (= (transform-row [ {:id :a :v 4} { :id :b :v 4}
{:id :c :v 8} { :id :d :v 8}])
[{:id :b :v 4 :double true :replaces :a }
{:id :d :v 8 :double true :replaces :c } :_ :_])))
(defn convert-to-visible-tiles [board]
(vec (keep identity
(for [row (range 4)
col (range 4)]
(let [t (get-in board [row col])]
(when (not= :_ t) (assoc t :top row :left col)))))))
(deftest convert-to-visible-tiles-card
(t/is (= (convert-to-visible-tiles
[[:_ :_ :_ {:v 2 :id "t1"}]
[:_ :_ :_ {:v 4 :id "t2"}]
[:_ :_ :_ {:v 8 :id "t3"}]
[:_ :_ :_ {:v 16 :id "t4"}]])
[{:v 2, :id "t1", :top 0, :left 3}
{:v 4, :id "t2", :top 1, :left 3}
{:v 8, :id "t3", :top 2, :left 3}
{:v 16, :id "t4", :top 3, :left 3}]))
(t/is (= (convert-to-visible-tiles
[[:_ :_ :_ :_]
[:_ :_ :_ :_]
[:_ :_ :_ :_]
[:_ :_ :_ :_]])
[])))
(defn select-row-key [row key]
(keep (fn [x] (when (not= x :_) (key x))) row))
(def reverse-rows (partial mapv (comp vec reverse)))
(defn reversed [f] (comp reverse-rows f reverse-rows))
(defn transpose [matrix]
(vec (apply map vector matrix)))
(defn transposed [f] (comp transpose f transpose))
(def transform-rows* (partial mapv transform-row))
(def transform-rows-right* (reversed transform-rows*))
(def transform-rows-up* (transposed transform-rows*))
(def transform-rows-down* (transposed transform-rows-right*))
(defmulti transform-rows identity)
(defmethod transform-rows :left [_ rows] (transform-rows* rows))
(defmethod transform-rows :right [_ rows] (transform-rows-right* rows))
(defmethod transform-rows :up [_ rows] (transform-rows-up* rows))
(defmethod transform-rows :down [_ rows] (transform-rows-down* rows))
(def base-board [[:_ :_ :_ :_]
[:_ :_ :_ :_]
[:_ :_ :_ :_]
[:_ :_ :_ :_]])
(defn tiles->board [tiles]
(reduce (fn [accum {:keys [top left] :as x}]
(assoc-in accum [top left]
(dissoc x :top :left)))
base-board (vals tiles)))
(defn get-replaced-tiles [new-tiles]
(keep (fn [x]
(when (:replaces x)
(-> x
(dissoc :replaces :double)
(assoc :id (:replaces x))
(assoc :remove true))))
new-tiles))
(defn transform-board [direction tiles]
(let [new-tiles (->> tiles
tiles->board
(transform-rows direction)
convert-to-visible-tiles)]
(merge tiles
(into {} (map (juxt :id #(dissoc % :replaces)) new-tiles))
(into {} (map (juxt :id identity) (get-replaced-tiles new-tiles))))))
(defn double-tiles [tiles]
(let [tiles' (filter (fn [[_ v]] (not (:remove v))) tiles)]
(into {}
(map (fn [[k x]]
[k (if (:double x)
(-> x
(assoc :v (* 2 (:v x))
:highlight true)
(dissoc :double))
x)])
tiles'))))
(defn remove-highlight-and-reveal [tiles]
(into {}
(map (fn [[k v]] [k (dissoc v :highlight :reveal)]) tiles)))
(defn create-tile [opts]
(merge
{ :v (if (< (rand) 0.9) 2 4)
:top (rand-int 4)
:left (rand-int 4)
:id (keyword (gensym "t"))
:reveal true}
opts))
(defn empty-slots [board]
(vec (keep
identity
(for [top (range 4)
left (range 4)]
(when (= :_ (get-in board [top left]))
{:top top :left left})))))
(defn add-random-tile [tiles]
(->> tiles
tiles->board
empty-slots
rand-nth
create-tile
((juxt :id identity))
(apply assoc tiles)))
(deftest transform-row-left-test
(t/is (= (transform-row [{:v 2 :id "t1"} :_ :_ {:v 2 :id "t2"}])
[{:v 2 :double true :id "t2" :replaces "t1"} :_ :_ :_]))
(t/is (= (transform-rows :left [[{:v 2 :id "t6"} :_ :_ {:v 2 :id "t1"}]
[:_ :_ :_ {:v 4 :id "t2"}]
[:_ :_ :_ {:v 8 :id "t3"}]
[{:v 16 :id "t7"} :_ :_ {:v 16 :id "t4"}]])
[[{:v 2 :id "t1" :double true :replaces "t6"} :_ :_ :_]
[{:v 4 :id "t2"} :_ :_ :_]
[{:v 8 :id "t3"} :_ :_ :_]
[{:v 16 :id "t4" :double true :replaces "t7"} :_ :_ :_]]))
(t/is (= (transform-board :left {:t1 {:id :t1 :top 0 :left 0 :v 2}
:t2 {:id :t2 :top 0 :left 3 :v 2}})
{:t1 {:id :t1, :v 2, :top 0, :left 0, :remove true}
:t2 {:id :t2, :v 2, :double true, :top 0, :left 0}}
))
(t/is (= (transform-board :right {:t1 {:id :t1 :top 0 :left 0 :v 2}
:t2 {:id :t2 :top 0 :left 3 :v 2}})
{:t1 {:id :t1, :v 2, :double true, :top 0, :left 3}
:t2 {:id :t2, :v 2, :top 0, :left 3, :remove true}}
))
(t/is (= (transform-board :up {:t1 {:id :t1 :top 0 :left 0 :v 2}
:t2 {:id :t2 :top 3 :left 0 :v 2}})
{:t2 {:id :t2, :v 2, :double true, :top 0, :left 0}
:t1 {:id :t1, :v 2, :top 0, :left 0, :remove true}}
))
(t/is (= (transform-board :down {:t1 {:id :t1 :top 0 :left 0 :v 2}
:t2 {:id :t2 :top 3 :left 0 :v 2}})
{:t1 {:id :t1, :v 2, :double true, :top 3, :left 0}
:t2 {:id :t2, :v 2, :top 3, :left 0, :remove true}})))
(defn move [dir data]
(let [prev @data]
(swap! data (partial transform-board dir))
(when (not= prev @data)
(go
(<! (timeout 100))
(swap! data (comp add-random-tile double-tiles))
(<! (timeout 400))
(swap! data remove-highlight-and-reveal)))))
(defn one-row-move [dir data]
(let [prev @data]
(swap! data (partial transform-board dir))
(when (not= prev @data)
(go
(<! (timeout 100))
(swap! data double-tiles)
(<! (timeout 400))
(swap! data remove-highlight-and-reveal)))))
(def start-data {:tile1 { :v 2 :top 0 :left 0 :id :tile1}
:tile2 { :v 2 :top 0 :left 3 :id :tile2}})
(defcard try-game-card
(fn [data _]
(sab/html
[:div
(game-board (vals @data))
[:div [:a {:onClick (fn [] (move :left data))} "left"]]
[:div
[:a {:onClick (fn [] (move :right data))} "right"]]
[:div [:a {:onClick (fn [] (move :up data))} "up"]]
[:div [:a {:onClick (fn [] (move :down data))} "down"]]
[:div
[:a {:onClick (fn [] (reset! data start-data))} "reset"]]
(dc/edn @data)]))
start-data)