Skip to content

Instantly share code, notes, and snippets.

@tonyarkles
Created May 3, 2018 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyarkles/01e7adbbb3b5cfd271ddc50b78cb9a76 to your computer and use it in GitHub Desktop.
Save tonyarkles/01e7adbbb3b5cfd271ddc50b78cb9a76 to your computer and use it in GitHub Desktop.
(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