-
Notifications
You must be signed in to change notification settings - Fork 3
/
Factor 2048.factor
320 lines (269 loc) · 6.74 KB
/
Factor 2048.factor
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
USE: accessors
FROM: arrays => <array> array ;
FROM: assocs => assoc-filter keys zip ;
FROM: combinators => case cleave cond ;
FROM: combinators.short-circuit => 1|| 1&& 2&& ;
FROM: continuations => cleanup ;
FROM: formatting => printf sprintf ;
FROM: fry => '[ _ ;
FROM: grouping => all-equal? clump group ;
FROM: io => bl flush nl readln write ;
FROM: kernel => = 2bi 2dup 2drop and bi bi* bi@ boa boolean clone equal? dip drop dup if if* keep loop nip not over swap throw tri unless when with xor ;
FROM: math => integer times * + > >= ;
FROM: math.functions => ^ ;
FROM: math.parser => hex> ;
FROM: math.order => +lt+ +gt+ +eq+ ;
FROM: random => random sample ;
FROM: sequences => <iota> <repetition> any? all? append concat each first flip head if-empty interleave length map pop push reduce reverse second set-nth tail ;
FROM: sorting => sort ;
FROM: vectors => <vector> ;
IN: 2048-game
ERROR: invalid-board ;
SYMBOL: left
SYMBOL: right
SYMBOL: up
SYMBOL: down
TUPLE: tile
{ level integer }
;
TUPLE: board
{ width integer }
{ height integer }
{ tiles array }
;
M: tile equal?
{
[ and ] ! test for f
[ [ level>> ] bi@ = ]
}
2&&
;
: valid-board? ( w h -- ? )
* 0 > ! board with 0 tiles does not have a meaningful representation
;
: <board> ( w h -- board )
[ valid-board? [ invalid-board throw ] unless ]
[ 2dup * f <array> board boa ] 2bi
;
: <tile> ( n -- tile )
tile boa
;
! 1 in 10 tile starts as 4
: new-tile ( -- tile )
10 random 0 = [ 2 ] [ 1 ] if
<tile>
;
<PRIVATE
: space-left? ( board -- ? )
tiles>> [ f = ] any?
;
: rows>> ( board -- seq )
dup tiles>>
[ drop { } ] [ swap width>> group ] if-empty
;
: rows<< ( seq board -- )
[ concat ] dip tiles<<
;
: columns>> ( board -- seq )
rows>> flip
;
: columns<< ( seq board -- )
[ flip concat ] dip tiles<<
;
: change-rows ( board quote -- board )
over [ rows>> swap call( seq -- seq ) ] [ rows<< ] bi
; inline
: change-columns ( board quote -- board )
over [ columns>> swap call( seq -- seq ) ] [ columns<< ] bi
; inline
: can-move-left? ( seq -- ? )
{
! one element seq cannot move
[ length 1 = not ]
! empty seq cannot move
[ [ f = ] all? not ]
[ 2 clump
[
{
! test for identical adjescent tiles
[ [ first ] [ second ] bi [ and ] [ = ] 2bi and ]
! test for empty space on the left and tile on the right
[ [ first ] [ second ] bi [ xor ] [ drop f = ] 2bi and ]
} 1||
] any?
]
} 1&&
;
: can-move-direction? ( board direction -- ? )
{
{ left [ rows>> [ can-move-left? ] any? ] }
{ right [ rows>> [ reverse can-move-left? ] any? ] }
{ up [ columns>> [ can-move-left? ] any? ] }
{ down [ columns>> [ reverse can-move-left? ] any? ] }
} case
;
: can-move-any? ( board -- ? )
{ left right up down } [ can-move-direction? ] with any?
;
: empty-indices ( seq -- seq )
[ length <iota> ] keep zip
[ nip f = ] assoc-filter keys
;
: pick-random ( seq -- elem )
1 sample first
;
! create a new tile on an empty space
: add-tile ( board -- )
[ new-tile swap [ empty-indices pick-random ] keep [ set-nth ] keep ] change-tiles drop
;
! combines equal tiles justified right or does nothing
: combine-tiles ( tile1 tile2 -- tile3 tile4 )
2dup { [ and ] [ = ] } 2&&
[ drop [ 1 + ] change-level f swap ] when
;
: justify-left ( seq -- seq )
[
{
{ [ dup f = ] [ 2drop +lt+ ] }
{ [ over f = ] [ 2drop +gt+ ] }
[ 2drop +eq+ ]
} cond
] sort
;
: collapse ( seq -- seq )
justify-left
! combine adjescent
dup length <vector>
[ over
[ swap [ push ] keep ]
[
{
[ pop combine-tiles ]
[ push ]
[ push ]
} cleave
] if-empty
] reduce
! fill in the gaps after combination
justify-left
;
! draws an object
GENERIC: draw ( obj -- )
PRIVATE>
! a single tile is higher than 2048 (level 10)
: won? ( board -- ? )
tiles>> [ dup [ level>> 11 >= ] when ] any?
;
! if there is no space left and no neightboring tiles are the same, end the board
: lost? ( board -- ? )
{
[ space-left? ]
[ won? ]
[ can-move-any? ]
} 1|| not
;
: serialize ( board -- str )
[ width>> ]
[ height>> ]
[ tiles>>
[ dup f = [ drop 0 ] [ level>> ] if "%02x" sprintf ] map concat
] tri
"%02x%02x%s" sprintf
;
: deserialize ( str -- board )
[ 2 head hex> ] [ 2 tail ] bi
[ 2 head hex> ] [ 2 tail ] bi
2 group [ hex> dup 0 = [ drop f ] [ tile boa ] if ] map
board boa
;
: move ( board direction -- )
{
{ left [ [ [ collapse ] map ] change-rows ] }
{ right [ [ [ reverse collapse reverse ] map ] change-rows ] }
{ up [ [ [ collapse ] map ] change-columns ] }
{ down [ [ [ reverse collapse reverse ] map ] change-columns ] }
} case drop
;
: get-input ( -- line )
readln
;
: parse-input ( line -- direction/f )
{
{ "a" [ left ] }
{ "d" [ right ] }
{ "w" [ up ] }
{ "s" [ down ] }
{ "q" [ f ] }
[ "Wrong input: %s\n" printf flush
get-input parse-input ]
} case
;
<PRIVATE
: init ( board -- )
'[ _ add-tile ] 2 swap times
;
M: tile draw ( tile -- )
level>> 2 swap ^ "% 4d" printf
;
M: boolean draw ( _ -- )
drop 4 [ bl ] times
;
: horizontal-line ( board -- )
width>>
" " write
"+------" <repetition> concat
write "+ " write nl
;
: separator ( -- )
" | " write
;
M: board draw ( board -- )
[ horizontal-line ] keep
[ rows>> ]
[
'[ _ horizontal-line ]
[ separator
[ separator ] [ draw ] interleave
separator nl
] interleave
]
[ horizontal-line ]
tri
flush
;
: update ( board -- f )
{
[
get-input parse-input [
{
[ can-move-direction? ]
[ over [ move ] [ add-tile ] bi* t ]
} 2&& drop t
] [ drop f ] if*
]
[ can-move-any? ]
} 1&&
;
: exit ( board -- )
{
{ [ dup lost? ] [ "You lost! Better luck next time." write nl ] }
{ [ dup won? ] [ "You won! Congratulations!" write nl ] }
[ "Bye!" write nl ]
} cond drop
;
PRIVATE>
: start-2048 ( -- )
4 4 <board>
[
! Initialization
[ init ]
[ draw ]
! Event loop
[ [ dup [ update ] [ draw ] bi ] loop ] tri
]
! Cleanup
[ exit ]
[ ]
cleanup
;
MAIN: start-2048