1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2013 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 (define-module (gacela examples engine-conway-game)
19 #:use-module (gacela system)
20 #:use-module (gacela engine)
21 #:use-module (ice-9 receive))
24 (define* (neighborhood cell #:key (size 1))
25 (let ((min (* -1 size)))
26 (let loop-x ((delta-x size) (res '()))
27 (cond ((< delta-x min) res)
30 (let loop-y ((delta-y size) (res res))
31 (cond ((< delta-y min) res)
34 (cond ((not (and (= delta-x 0) (= delta-y 0)))
35 (cons (list (+ delta-x (car cell)) (+ delta-y (cadr cell))) res))
38 (define* (frequencies cells #:optional (res '()))
42 (let ((freq (or (assoc-ref res (car cells)) 0)))
43 (frequencies (cdr cells)
44 (assoc-set! res (car cells) (+ freq 1)))))))
46 (define* (dead-loop cells freq #:optional (deads '()))
50 (let* ((key (get-key (car cells)))
51 (coord (get-component 'coord (car cells)))
52 (f (or (assoc-ref freq coord) 0))
53 (new-freq (assoc-remove! freq coord)))
54 (cond ((not (or (= f 2) (= f 3)))
55 (dead-loop (cdr cells)
57 (cons (remove-entity key) deads)))
59 (dead-loop (cdr cells) new-freq deads)))))))
61 (define* (live-loop freq #:optional (lives '()))
65 (cond ((= (cdar freq) 3)
67 (cons (new-entity `(coord . ,(caar freq))) lives)))
69 (live-loop (cdr freq) lives))))))
71 (define-system lives-or-deads ((cells (coord)))
72 (let ((freq (frequencies (apply append (map (lambda (c) (neighborhood (get-component 'coord c))) cells)))))
73 (receive (freq2 deads) (dead-loop cells freq)
76 (live-loop freq2))))))
78 (define-system print-world ((cells (coord)))
79 (format #t "Live Cells: ~a~%" (length cells)))
81 (define-engine conway-game lives-or-deads print-world)
83 (with-engine conway-game
84 (let ((cells '((4 1) (4 2) (5 1) (5 2)
85 (11 3) (11 4) (11 5) (12 2) (12 6) (13 1) (13 7) (14 1) (14 7)
86 (15 4) (16 2) (16 6) (17 3) (17 4) (17 5) (18 4)
87 (21 5) (21 6) (21 7) (22 5) (22 6) (22 7) (23 4) (23 8)
88 (25 3) (25 4) (25 8) (25 9)
89 (35 6) (35 7) (36 6) (36 7))))
90 (for-each (lambda (c) (new-entity! `(coord . ,c))) cells)))
92 (with-engine conway-game ()
93 (let ((cells '((4 1) (4 2) (5 1) (5 2)
94 (11 3) (11 4) (11 5) (12 2) (12 6) (13 1) (13 7) (14 1) (14 7)
95 (15 4) (16 2) (16 6) (17 3) (17 4) (17 5) (18 4)
96 (21 5) (21 6) (21 7) (22 5) (22 6) (22 7) (23 4) (23 8)
97 (25 3) (25 4) (25 8) (25 9)
98 (35 6) (35 7) (36 6) (36 7))))
100 (map (lambda (c) (new-entity `(coord . ,c))) cells))))