;;; ****************************************************************
;;; Conway's Game of Life ******************************************
;;; ****************************************************************
;;; Don't know where/when got this. --mk
(defstruct (world (:print-function
(lambda (s d o)
(declare (ignore d))
(format s "#<WORLD: ~D>" (world-numdots world)))))
size
current
numdots
next
(xmin 1000000) ; Initialize the region to ridiculous numbers.
(xmax -1)
(ymin 1000000)
(ymax -1))
(defun setnext (world i j)
(let* ((current (world-current world))
(next (world-next world))
(neighbors (count-neighbors current i j)))
;; set the next bit pattern
(if (zerop (bit current i j))
(cond ((not (= neighbors 3))
;; current = 0, surrounding cells != 3
(setf (bit next i j) 0))
(t (setf (bit next i j) 1)
;; current = 0, surrounding cells = 3
(incf (world-numdots world))))
(cond ((or (= neighbors 2)
(= neighbors 3))
;; current = 1, surrounding cells = 2,3
(setf (bit next i j) 1))
(t (setf (bit next i j) 0)
(decf (world-numdots world)))))
;; reset the bounds, if necessary
(unless (zerop (bit next i j))
(when (< i (world-xmin world)) (setf (world-xmin world) i))
(when (> i (world-xmax world)) (setf (world-xmax world) i))
(when (< j (world-ymin world)) (setf (world-ymin world) j))
(when (> j (world-ymax world)) (setf (world-ymax world) j)))))