summaryrefslogtreecommitdiff
path: root/examples/2048/2048
blob: 58f261502abc0c3ced49e0a74c42ec4b8772e421 (plain)
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
#! /usr/bin/env guile
!#

;;; 2048
;;; Copyright (C) 2014 David Thompson <dthompson2@worcester.edu>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Clone of the official 2048 game at http://gabrielecirulli.github.io/2048/
;;
;;; Code:

(use-modules (srfi srfi-1)
             (srfi srfi-9)
             (srfi srfi-11)
             (srfi srfi-26)
             (srfi srfi-42)
             (ice-9 match)
             (ice-9 rdelim)
             (gl)
             (2d audio)
             (2d color)
             (2d font)
             (2d game)
             (2d keyboard)
             (2d rect)
             (2d signal)
             (2d sprite)
             (2d texture)
             (2d vector)
             (2d window)
             (2d repl))
;;;
;;; Helpers
;;;

(define* (flat-map proc lst . rest)
  (concatenate (apply map proc lst rest)))

(define (enumerate lst)
  (zip (iota (length lst)) lst))

;;;
;;; Game Board
;;;

(define board-size 4)

(define (double x)
  (* x 2))

(define (strip-zeros lst)
  (delete 0 lst))

(define (pad-zeros lst size)
  (append lst (make-list (max (- size (length lst)) 0) 0)))

(define (merge lst)
  (match lst
    ((x x . rest)
     (cons (double x) (merge rest)))
    ((x . rest)
     (cons x (merge rest)))
    (_ '())))

(define (points a b)
  (define (iter a b p)
    (cond ((or (null? a)
               (null? b))
           p)
          ((= (car a) (car b))
           (iter (cdr a) (cdr b) p))
          (else
           (iter (cddr a) (cdr b) (+ p (car b))))))
  (iter a b 0))

;; Merge list and accumulate points.
(define (merge-row row)
  (let* ((stripped (strip-zeros row))
         (merged (merge stripped)))
    (list (points stripped merged)
          (pad-zeros merged board-size))))

(define (transpose board)
  (if (null? (car board))
      '()
      (cons (map car board)
            (transpose (map cdr board)))))

(define (board-shift-left board)
  (map merge-row board))

(define (board-shift-right board)
  (map (lambda (row)
         (let ((merged (merge-row (reverse row))))
           (list (first merged) (reverse (second merged)))))
       board))

(define (board-shift-up board)
  (let-values (((points board)
                (unzip2 (board-shift-left (transpose board)))))
    (zip points (transpose board))))

(define (board-shift-down board)
  (let-values (((points board)
                (unzip2 (board-shift-right (transpose board)))))
    (zip points (transpose board))))

(define (board-shift board direction)
  (match direction
    ('up
     (board-shift-up board))
    ('down
     (board-shift-down board))
    ('left
     (board-shift-left board))
    ('right
     (board-shift-right board))
    (_ board)))

(define (board-shift-and-accum-points board direction)
  (let-values (((points board)
                (unzip2 (board-shift board direction))))
    (values (reduce + 0 points)
            board)))

(define (random-tile)
  (list-ref '(2 4) (random 2)))

;; So gross.
(define (board-insert board)
  (let ((x (random board-size))
        (y (random board-size)))
    (if (zero? (list-ref (list-ref board y) x))
        (append (take board y)
                (let ((rows (drop board y)))
                  (cons (let ((cells (car rows)))
                          (append (take cells x)
                                  (let ((rest (drop cells x)))
                                    (cons (random-tile)
                                          (cdr rest)))))
                        (cdr rows))))
        (board-insert board))))

(define (board-find board n)
  (list?
   (any (lambda (row)
          (memq n row))
        board)))

(define (board-win? board)
  (board-find (make-board) 2048))

(define (board-lose? board)
  (define (full? row)
    (define (iter row prev)
      (cond ((null? row)
             #t)
            ((or (zero? (car row))
                 (= (car row) prev))
             #f)
            (else
             (iter (cdr row) (car row)))))
    (iter row 0))
  (and (every full? board)
       (every full? (transpose board))))

(define null-board
  '((0 0 0 0)
    (0 0 0 0)
    (0 0 0 0)
    (0 0 0 0)))

(define (make-board)
  (board-insert (board-insert null-board)))

;;;
;;; Game State
;;;

(define save-file
  (string-join (list (getenv "HOME") ".guile-2048")
               file-name-separator-string))

(define-record-type <2048>
  (make-2048 board score best-score)
  2048?
  (board 2048-board)
  (score 2048-score)
  (best-score 2048-best-score))

(define* (new-game #:optional (previous #f))
  (let ((best-score (if previous
                        (choose-best-score previous)
                        (load-best-score))))
    (make-2048 (make-board) 0 best-score)))

(define (load-best-score)
  (if (file-exists? save-file)
      (with-input-from-file save-file
        (lambda ()
          (let ((score (string->number (read-string))))
            (if (number? score) score 0))))
      0))

(define (save-best-score state)
  (with-output-to-file save-file
    (lambda ()
      (format #t "~d" (choose-best-score state)))))

(define (choose-best-score state)
  (max (2048-score state) (2048-best-score state)))

(define-signal controls
  (signal-filter
   (lambda (key)
     (any (cut eq? key <>)
          '(up down left right n)))
   #f key-last-down))

(define-signal 2048-state
  (signal-fold
   (lambda (key prev)
     (if (eq? key 'n)
         (new-game prev)
         (let-values (((points new-board)
                       (board-shift-and-accum-points (2048-board prev) key)))
           (let ((score (+ (2048-score prev) points)))
             ;; Only insert a new tile if there's room and the board
             ;; was actually shifted.
             (if (and (not (equal? (2048-board prev) new-board))
                      (board-find new-board 0))
                 (make-2048 (board-insert new-board) score
                            (2048-best-score prev))
                 (make-2048 new-board score (2048-best-score prev)))))))
   (new-game)
   controls))

;; For convenience
(define-signal board
  (signal-map 2048-board 2048-state))

(define-signal score-saver
  (signal-tap (lambda (state)
                (when (board-lose? (2048-board state))
                  (save-best-score state)))
              2048-state))

;;;
;;; Rendering
;;;

(open-window)
(enable-sprites)
(enable-fonts)
(enable-audio)

(define background (rgb #xfaf8ef))

(set-gl-clear-color (color-r background)
                    (color-g background)
                    (color-b background)
                    (color-a background))

(define tile-texture (load-texture "tile.png"))

(define font (load-default-font 32))

(define-record-type <tile>
  (%make-tile background label)
  tile?
  (background tile-background)
  (label tile-label))

(define text-color-1 (rgb #x776e65))
(define text-color-2 (rgb #xf9f6f2))

(define tile-properties
  `((0    . ((bg-color . ,(rgba #xeee4daaa))
             (text-color . ,text-color-1)))
    (2    . ((bg-color . ,(rgb #xeee4da))
             (text-color . ,text-color-1)))
    (4    . ((bg-color . ,(rgb #xede0c8))
             (text-color . ,text-color-1)))
    (8    . ((bg-color . ,(rgb #xf2b179))
             (text-color . ,text-color-2)))
    (16   . ((bg-color . ,(rgb #xf59563))
             (text-color . ,text-color-2)))
    (32   . ((bg-color . ,(rgb #xf67c5f))
             (text-color . ,text-color-2)))
    (64   . ((bg-color . ,(rgb #xf65e3b))
             (text-color . ,text-color-2)))
    (128  . ((bg-color . ,(rgb #xedcf72))
             (text-color . ,text-color-2)))
    (256  . ((bg-color . ,(rgb #xedcc61))
             (text-color . ,text-color-2)))
    (512  . ((bg-color . ,(rgb #xedc850))
             (text-color . ,text-color-2)))
    (1024 . ((bg-color . ,(rgb #xedc53f))
             (text-color . ,text-color-2)))
    (2048 . ((bg-color . ,(rgb #xedc22e))
             (text-color . ,text-color-2)))))

(define (tile-bg-color n)
  (assoc-ref (assoc-ref tile-properties n) 'bg-color))

(define (tile-text-color n)
  (assoc-ref (assoc-ref tile-properties n) 'text-color))

(define (make-tile x y n)
  (let* ((w (texture-width tile-texture))
         (h (texture-height tile-texture))
         (background
          (make-sprite tile-texture
                       #:position (center (vector (* x w) (* y h)))
                       #:color (tile-bg-color n)
                       #:anchor #(0 0)))
         (label
          (make-label font
                      (if (zero? n) " " (number->string n))
                      (center
                       (vector (+ (* x w) (/ w 2))
                               (+ (* y h) (/ h 2))))
                      #:color (tile-text-color n)
                      #:anchor 'center)))
    (%make-tile background label)))

(define (draw-tile tile)
  (draw-sprite (tile-background tile))
  (draw-label (tile-label tile)))

(define window-width 640)
(define window-height 480)
(define board-width
  (* board-size (texture-width tile-texture)))
(define board-height
  (* board-size (texture-height tile-texture)))
(define center-pos
  (vector (/ (- window-width board-width) 2)
          (- window-height board-height 8)))

(define (center v)
  (v+ v center-pos))

(define (enumerate-board board)
  (enumerate (map (cut enumerate <>) board)))

;; Transform board into a list of tile objects.
(define-signal tiles
  (signal-map
   (lambda (board)
     (flat-map
      (lambda (row)
        (let ((y (first row))
              (row (second row)))
          (map (lambda (cell)
                 (let ((x (first cell))
                       (n (second cell)))
                   (make-tile x y n)))
               row)))
      (enumerate-board board)))
   board))

(define-signal status
  (signal-map
   (lambda (board)
     (let ((message (cond ((board-lose? board) "GAME OVER")
                          ((board-win? board) "YOU WIN!")
                          (else ""))))
       (make-label font message
                   (center
                    (vector (/ board-width 2)
                            (/ board-height 2)))
                   #:color black
                   #:anchor 'bottom-center)))
   board))

(define play-again-font (load-default-font 16))

(define-signal play-again-message
  (signal-map
   (lambda (board)
     (make-label play-again-font
                 (if (or (board-lose? board)
                         (board-win? board))
                     "Press N to play again"
                     "")
                 (center
                  (vector (/ board-width 2)
                          (/ board-height 2)))
                 #:color black
                 #:anchor 'top-center))
   board))

(define instruction-font (load-default-font 16))

(define instructions
  (make-label instruction-font
              "Use the arrow keys to join the numbers and get to the 2048 tile!"
              (vector (/ window-width 2) 0)
              #:color text-color-1
              #:anchor 'top-center))

(define score-header-font (load-default-font 14))
(define score-font (load-default-font 22))

(define score-header
  (make-label score-header-font
              "SCORE"
              (vector (+ (vx center-pos) (/ board-width 4)) 24)
              #:color text-color-1
              #:anchor 'top-center))

(define-signal score
  (signal-map
   (lambda (state)
     (make-label score-font
                 (format #f "~d" (2048-score state))
                 (vector (vx (label-position score-header))
                         (+ (vy (label-position score-header)) 32))
                 #:color text-color-1
                 #:anchor 'center))
   2048-state))

(define best-score-header
  (make-label score-header-font
              "BEST"
              (vector (+ (vx center-pos)
                         (- board-width (/ board-width 4)))
                      24)
              #:color text-color-1
              #:anchor 'top-center))

(define-signal best-score
  (signal-map
   (lambda (state)
     (make-label score-font
                 (format #f "~d" (2048-best-score state))
                 (vector (vx (label-position best-score-header))
                         (+ (vy (label-position best-score-header)) 32))
                 #:color text-color-1
                 #:anchor 'center))
   2048-state))

(define (render)
  (for-each draw-tile (signal-ref tiles))
  (draw-label instructions)
  (draw-label score-header)
  (draw-label best-score-header)
  (draw-label (signal-ref score))
  (draw-label (signal-ref best-score))
  (draw-label (signal-ref status))
  (draw-label (signal-ref play-again-message)))

;;;
;;; Initialization
;;;

(start-2d-repl)

(add-hook! window-close-hook stop-game-loop)
(add-hook! draw-hook (lambda (dt alpha) (render)))

(with-window (make-window #:title "2048")
  (start-game-loop))