-
Notifications
You must be signed in to change notification settings - Fork 3
/
Elm 2048.elm
403 lines (310 loc) · 12.3 KB
/
Elm 2048.elm
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
module Main exposing (..)
import Html exposing (Html, div, p, text, button, span, h2)
import Html.Attributes exposing (class, style)
import Html.Events exposing (onClick)
import Keyboard exposing (KeyCode)
import Random
import Tuple
main =
Html.program
{ init = ( { initialModel | waitingForRandom = True }, generateRandomTiles 2 )
, view = view
, update = update
, subscriptions = always (Keyboard.downs KeyPress)
}
-- MODEL
-- tiles either have a value (2, 4, 8, ...) or are empty
type alias Tile =
Maybe Int
type alias Model =
{ score : Int
, tiles : List Tile
, hasLost : Bool
, winKeepPlaying : Bool
, waitingForRandom : Bool -- prevent user from giving input while waiting for Random Cmd to return
}
initialModel : Model
initialModel =
{ score = 0, tiles = List.repeat 16 Nothing, waitingForRandom = False, hasLost = False, winKeepPlaying = False}
-- UPDATE
type alias RandomTileInfo =
( Int, Int )
type Msg
= KeyPress KeyCode
| AddRandomTiles (List RandomTileInfo)
| NewGame
| KeepPlaying
-- asks the random generator to generate the information required for later adding random tiles
-- generate a random position for the and value (4 10%, 2 90%) for each tile
-- this uses Random.pair and Random.list to get a variable number of such pairs with one Cmd
generateRandomTiles : Int -> Cmd Msg
generateRandomTiles num =
let
randomPosition =
Random.int 0 15
randomValue =
Random.int 1 10
|> Random.map
(\rnd ->
if rnd == 10 then
4
else
2
)
-- 10% chance
randomPositionAndValue =
Random.pair randomPosition randomValue
in
Random.list num randomPositionAndValue |> Random.generate AddRandomTiles
-- actually add a random tile to the model
addRandomTile : RandomTileInfo -> List Tile -> List Tile
addRandomTile ( newPosition, newValue ) tiles =
let
-- newPosition is a value between 0 and 15
-- go through the list and count the amount of empty tiles we've seen.
-- if we reached the newPosition % emptyTileCount'th empty tile, set its value to newValue
emptyTileCount =
List.filter ((==) Nothing) tiles |> List.length
-- if there are less than 16 empty tiles this is the number of empty tiles we pass
targetCount =
newPosition % emptyTileCount
set_ith_empty_tile tile ( countEmpty, newList ) =
case tile of
Just value ->
( countEmpty, (Just value) :: newList )
Nothing ->
if countEmpty == targetCount then
-- replace this empty tile with the new value
( countEmpty + 1, (Just newValue) :: newList )
else
( countEmpty + 1, Nothing :: newList )
in
List.foldr set_ith_empty_tile ( 0, [] ) tiles |> Tuple.second
-- core game mechanic: move numbers (to the left,
-- moving to the right is equivalent to moving left on the reversed array)
-- this function works on single columns/rows
moveNumbers : List Tile -> ( List Tile, Int )
moveNumbers tiles =
let
last =
List.head << List.reverse
-- init is to last what tail is to head
init =
List.reverse << List.drop 1 << List.reverse
doMove tile ( newTiles, addScore ) =
case tile of
-- omit empty tiles when shifting
Nothing ->
( newTiles, addScore )
Just value ->
case last newTiles of
-- if the last already moved tile ...
Just (Just value2) ->
-- ... has the same value, add a tile with the summed value
if value == value2 then
( (init newTiles) ++ [ Just (2 * value) ]
, addScore + 2 * value )
else
-- ... else just add the tile
( newTiles ++ [ Just value ], addScore )
_ ->
-- ... else just add the tile
( newTiles ++ [ Just value ], addScore )
( movedTiles, addScore ) =
List.foldl doMove ( [], 0 ) tiles
in
( movedTiles ++ List.repeat (4 - List.length movedTiles) Nothing, addScore )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
-- new game button press
NewGame ->
if not model.waitingForRandom then
( { initialModel | waitingForRandom = True }, generateRandomTiles 2 )
else
( model, Cmd.none )
-- "keep playing" button on win screen
KeepPlaying ->
( { model | winKeepPlaying = True }, Cmd.none)
-- Random generator Cmd response
AddRandomTiles tileInfos ->
let
newTiles =
List.foldl addRandomTile model.tiles tileInfos
in
( { model | tiles = newTiles, waitingForRandom = False }, Cmd.none )
KeyPress code ->
let
-- zip list and indices, apply filter, unzip
indexedFilter func list =
List.map2 (,) (List.range 0 (List.length list - 1)) list
|> List.filter func
|> List.map Tuple.second
-- the i'th row (of 4) contains elements i*4, i*4+1, i*4+2, i*4+3
-- so all elements for which index//4 == i
i_th_row list i =
indexedFilter (((==) i) << (flip (//) 4) << Tuple.first) list
-- the i'th col (of 4) contain elements i, i+4, i+2*4, i+3*4
-- so all elements for which index%4 == i
i_th_col list i =
indexedFilter (((==) i) << (flip (%) 4) << Tuple.first) list
-- rows and columns of the grid
rows list =
List.map (i_th_row list) (List.range 0 3)
cols list =
List.map (i_th_col list) (List.range 0 3)
-- move each row or column and unzip the results from each call to moveNumbers
move =
List.unzip << List.map moveNumbers
moveReverse =
List.unzip << List.map (Tuple.mapFirst List.reverse << moveNumbers << List.reverse)
-- concat rows back into a flat array and sum all addScores
unrows =
Tuple.mapSecond List.sum << Tuple.mapFirst List.concat
-- turn columns back into a flat array and sum all addScores
uncols =
Tuple.mapSecond List.sum << Tuple.mapFirst (List.concat << cols << List.concat)
-- when shifting left or right each row can be (reverse-) shifted separately
-- when shifting up or down each column can be (reveerse-) shifted separately
( newTiles, addScore ) =
case code of
37 ->
-- left
unrows <| move <| rows model.tiles
38 ->
-- up
uncols <| move <| cols model.tiles
39 ->
-- right
unrows <| moveReverse <| rows model.tiles
40 ->
-- down
uncols <| moveReverse <| cols model.tiles
_ ->
( model.tiles, 0 )
containsEmptyTiles =
List.any ((==) Nothing)
containsAnySameNeighbours : List Tile -> Bool
containsAnySameNeighbours list =
let
tail = List.drop 1 list
init = List.reverse <| List.drop 1 <| List.reverse list
in
List.any (uncurry (==)) <| List.map2 (,) init tail
hasLost =
-- grid full
(not (containsEmptyTiles newTiles))
-- and no left/right move possible
&& (not <| List.any containsAnySameNeighbours <| rows newTiles)
-- and no up/down move possible
&& (not <| List.any containsAnySameNeighbours <| cols newTiles)
( cmd, waiting ) =
if List.all identity <| List.map2 (==) model.tiles newTiles then
( Cmd.none, False )
else
( generateRandomTiles 1, True )
score =
model.score + addScore
in
-- unsure whether this actually happens but regardless:
-- keep the program from accepting a new keyboard input when a new tile hasn't been spawned yet
if model.waitingForRandom then
( model, Cmd.none )
else
( { model | tiles = newTiles, waitingForRandom = waiting, score = score, hasLost = hasLost }, cmd )
-- VIEW
containerStyle : List ( String, String )
containerStyle =
[ ( "width", "450px" )
, ( "height", "450px" )
, ( "background-color", "#bbada0" )
, ( "float", "left" )
, ( "border-radius", "6px")
]
tileStyle : Int -> List ( String, String )
tileStyle value =
let
color =
case value of
0 ->
"#776e65"
2 ->
"#eee4da"
4 ->
"#ede0c8"
8 ->
"#f2b179"
16 ->
"#f59563"
32 ->
"#f67c5f"
64 ->
"#f65e3b"
128 ->
"#edcf72"
256 ->
"#edcc61"
512 ->
"#edc850"
1024 ->
"#edc53f"
2048 ->
"#edc22e"
_ ->
"#edc22e"
in
[ ( "width", "100px" )
, ( "height", "70px" )
, ( "background-color", color )
, ( "float", "left" )
, ( "margin-left", "10px" )
, ( "margin-top", "10px" )
, ( "padding-top", "30px" )
, ( "text-align", "center" )
, ( "font-size", "30px" )
, ( "font-weight", "bold" )
, ( "border-radius", "6px")
]
viewTile : Tile -> Html Msg
viewTile tile =
div [ style <| tileStyle <| Maybe.withDefault 0 tile ]
[ span [] [ text <| Maybe.withDefault "" <| Maybe.map toString tile ]
]
viewGrid : List Tile -> Html Msg
viewGrid tiles =
div [ style containerStyle ] <| List.map viewTile tiles
viewLost : Html Msg
viewLost =
div
[ style containerStyle ]
[ div
[ style [ ( "text-align", "center" ) ] ]
[ h2 [] [ text "You lost!" ]
]
]
viewWin : Html Msg
viewWin =
div
[ style containerStyle ]
[ div
[ style [ ( "text-align", "center" ) ] ]
[ h2 [] [ text "Congratulations, You won!" ]
, button
[ style [ ( "margin-bottom", "16px" ), ( "margin-top", "16px" ) ], onClick KeepPlaying ]
[ text "Keep playing" ]
]
]
view : Model -> Html Msg
view model =
div [ style [ ( "width", "450px" ) ] ]
[ p [ style [ ( "float", "left" ) ] ] [ text <| "Your Score: " ++ toString model.score ]
, button
[ style [ ( "margin-bottom", "16px" ), ( "margin-top", "16px" ), ( "float", "right" ) ], onClick NewGame ]
[ text "New Game" ]
, if model.hasLost then
viewLost
else if List.any ((==) (Just 2048)) model.tiles && not model.winKeepPlaying then
viewWin
else
viewGrid model.tiles
]