Created
May 3, 2018 00:52
-
-
Save tonyarkles/01e7adbbb3b5cfd271ddc50b78cb9a76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defparameter *width* 8) | |
(defparameter *height* 8) | |
(defparameter *board* | |
(make-array (list *width* *height*) :initial-element 0)) | |
(defun birth (x y) | |
(setf (aref *board* x y) 1)) | |
(defun death (x y) | |
(setf (aref *board* x y) 0)) | |
(defun val (x y) | |
(aref *board* (mod x *width*) (mod y *height*))) | |
(defun next-state (x y) | |
(case (val x y) | |
(0 (cond | |
((= 3 (alive-neighbours x y)) (list 'birth x y)) | |
(t nil))) | |
(1 (cond | |
((< (alive-neighbours x y) 2) (list 'death x y)) | |
((> (alive-neighbours x y) 3) (list 'death x y)) | |
(t nil))))) | |
(defun neighbour (x y dir) | |
(val (+ x (first dirs)) (+ y (second dir)))) | |
(defun alive-neighbours (x y) | |
(let ((dirs '((-1 -1) (0 -1) (1 -1) (-1 0) (1 0) (-1 1) (0 1) (1 1)))) | |
(reduce (lambda (sum dir) (+ sum (neighbour x y dir))) dirs :initial-value 0))) | |
(defun range (max &key (min 0) (step 1)) | |
(loop for n from min below max by step | |
collect n)) | |
(defun all-updates () | |
(mapcan (lambda (y) | |
(mapcar (lambda (x) | |
(next-state x y)) | |
(range *width*))) | |
(range *height*))) | |
(defun apply-updates (updates) | |
(mapcar (lambda (up) (eval up)) updates)) | |
(defun game-step () | |
(apply-updates (all-updates)) | |
(pprint *board*)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment