aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md0
-rw-r--r--gol.asd11
-rw-r--r--src/gol.lisp71
-rw-r--r--src/package.lisp3
4 files changed, 85 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/README.md
diff --git a/gol.asd b/gol.asd
new file mode 100644
index 0000000..bca2031
--- /dev/null
+++ b/gol.asd
@@ -0,0 +1,11 @@
+(asdf:defsystem #:gol
+ :description ""
+ :author "Thomas Albers Raviola <thomas@thomaslabs.org>"
+ :maintainer "Thomas Albers Raviola <thomas@thomaslabs.org>"
+ :license "GPL-3"
+ :version "1.0.0"
+ :serial t
+ ;; :depends-on ()
+ :pathname "src"
+ :components ((:file "package")
+ (:file "gol")))
diff --git a/src/gol.lisp b/src/gol.lisp
new file mode 100644
index 0000000..d954583
--- /dev/null
+++ b/src/gol.lisp
@@ -0,0 +1,71 @@
+(in-package #:gol)
+
+(asdf:load-systems :alexandria
+ :sdl2)
+(use-package :alexandria)
+
+(deftype grid (width height)
+ `(array integer (,width ,height)))
+
+(defun next-generation (grid result)
+ (assert (equal (array-dimensions grid) (array-dimensions result)))
+ (destructuring-bind (m n) (array-dimensions grid)
+ (flet ((cell-value (i j)
+ (- (loop :for u :from (1- i) :repeat 3
+ :summing
+ (loop :for v :from (1- j) :repeat 3
+ :summing (aref grid (mod u m) (mod v n))))
+ (aref grid i j))))
+ (dotimes (i m)
+ (dotimes (j n)
+ (let ((x (aref grid i j))
+ (y (cell-value i j)))
+ (setf (aref result i j)
+ (if (zerop x)
+ (if (= y 3) 1 0)
+ (if (or (= y 2) (= y 3)) 1 0)))))))))
+
+(defun draw-grid (renderer grid &key (cell-w 10) (cell-h 10))
+ (destructuring-bind (m n) (array-dimensions grid)
+ (dotimes (i m)
+ (dotimes (j n)
+ (let ((rect (sdl2:make-rect (* i cell-h) (* j cell-w) cell-w cell-h)))
+ (sdl2:render-draw-rect renderer rect)
+ (unless (zerop (aref grid i j))
+ (sdl2:render-fill-rect renderer rect)))))))
+
+(defun run ()
+ (sdl2:with-init (:everything)
+ (sdl2:with-window (window)
+ (sdl2:with-renderer (renderer window)
+ (sdl2:with-event-loop ()
+ (:idle ()
+ (sdl2:set-render-draw-color renderer 255 255 255 255)
+ (sdl2:render-clear renderer)
+ (sdl2:set-render-draw-color renderer 0 0 0 255)
+ (draw-grid renderer *grid1*)
+ (sdl2:render-present renderer)
+ (sdl2:delay 200)
+ (next-generation *grid1* *grid2*)
+ (rotatef *grid1* *grid2*))
+ (:quit () t))))))
+
+
+(defparameter *grid1* (make-array '(20 20) :initial-element 0))
+(defparameter *grid2* (make-array '(20 20) :initial-element 0))
+
+(setf (aref *grid1* 0 0) 1
+ (aref *grid1* 1 0) 1
+ (aref *grid1* 1 1) 1
+ (aref *grid1* 2 1) 1
+ (aref *grid1* 0 2) 1)
+
+(run)
+
+*grid1*
+*grid2*
+
+(progn
+ (next-generation *grid1* *grid2*)
+ (rotatef *grid1* *grid2*)
+ *grid2*)
diff --git a/src/package.lisp b/src/package.lisp
new file mode 100644
index 0000000..1db28a2
--- /dev/null
+++ b/src/package.lisp
@@ -0,0 +1,3 @@
+(defpackage #:gol
+ (:use #:cl)
+ (:export))