summaryrefslogtreecommitdiff
path: root/scripts/gol.scm
diff options
context:
space:
mode:
authorHenrique Alves <henrique.alves@itsjungle.xyz>2024-05-02 00:37:10 +0300
committerHenrique Alves <henrique.alves@itsjungle.xyz>2024-05-02 00:37:10 +0300
commit510a5d76180f30f1dd75a05927bbcb10bfae8da8 (patch)
tree4395294fd3fe9076169b467b22f34e2fff22a8b9 /scripts/gol.scm
parentaeddacb8d88f41ba7bdf37d39de17d8cf3b6a258 (diff)
downloadgamejam-slgj-2024-510a5d76180f30f1dd75a05927bbcb10bfae8da8.tar.gz
gamejam-slgj-2024-510a5d76180f30f1dd75a05927bbcb10bfae8da8.tar.bz2
gamejam-slgj-2024-510a5d76180f30f1dd75a05927bbcb10bfae8da8.zip
basic GoL
Diffstat (limited to 'scripts/gol.scm')
-rw-r--r--scripts/gol.scm76
1 files changed, 76 insertions, 0 deletions
diff --git a/scripts/gol.scm b/scripts/gol.scm
new file mode 100644
index 0000000..f95dc91
--- /dev/null
+++ b/scripts/gol.scm
@@ -0,0 +1,76 @@
+(define current-cells (make-hash-table))
+
+(define neighbors
+ '((-1 . -1) (0 . -1) (1 . -1) (-1 . 0) (1 . 0) (-1 . 1) (0 . 1) (1 . 1)))
+
+(set! (current-cells '(1 . 1)) #t)
+(set! (current-cells '(1 . 2)) #t)
+(set! (current-cells '(1 . 3)) #t)
+
+(define sum-cells
+ (lambda (c1 c2)
+ (cons (+ (car c1) (car c2)) (+ (cdr c1) (cdr c2)))))
+
+(define diff-cells
+ (lambda (c1 c2)
+ (cons (- (car c1) (car c2)) (- (cdr c1) (cdr c2)))))
+
+(define sum-all-cells
+ (lambda (lat)
+ (cond ((null? (cdr lat)) (car lat))
+ (else (let ((c1 (car lat)) (c2 (sum-all-cells (cdr lat))))
+ (sum-cells c1 c2)
+ )))))
+
+(define is-hash-neighbor?
+ (lambda (c1 c2)
+ (let ((d (diff-cells c1 c2)))
+ (and (<= (car d) 1) (>= (car d) -1) (<= (cdr d) 1) (>= (cdr d) -1)))))
+
+(define count-elements
+ (lambda (lat s)
+ (cond ((null? lat) s)
+ (else (count-elements (cdr lat) (+ s 1))))))
+
+(define list-neighbors
+ (lambda (c)
+ (map (lambda (n)
+ (sum-cells c n))
+ neighbors)))
+
+(define sum-live-cells
+ (lambda (lat n)
+ (cond ((null? lat) n)
+ (else (cond ((current-cells (car lat)) (sum-live-cells (cdr lat) (+ n 1)))
+ (else (sum-live-cells (cdr lat) n)))))))
+
+(define list-all-neighbors
+ (lambda (lat cells)
+ (cond ((null? lat) cells)
+ (else
+ (map (lambda (new-neighbor)
+ (set! (cells new-neighbor) #t))
+ (cons (car lat) (list-neighbors (car lat))))
+ (list-all-neighbors (cdr lat) cells)))))
+
+(define (hash-table-keys ht)
+ (map (lambda (kv)
+ (kv 0)
+ )
+ ht)
+ )
+
+(define run-step
+ (lambda ()
+ (let ((temp-hash (make-hash-table)) (next-cells (make-hash-table)))
+ (map (lambda (temp-cell)
+ (let ((n (sum-live-cells (list-neighbors temp-cell) 0)))
+ (cond ((current-cells temp-cell)
+ (cond ((and (>= n 2) (<= n 3)) (set! (next-cells temp-cell) #t))))
+ (else (cond ((eq? n 3) (set! (next-cells temp-cell) #t))))))
+ )
+ (hash-table-keys (list-all-neighbors (hash-table-keys current-cells) temp-hash)))
+ (set! current-cells next-cells)
+ )
+ )
+ )