Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defpackage game-of-life
- (:use :cl)
- (:export :do-step))
- (in-package :game-of-life)
- (defun do-step (grid)
- (loop with dims = (array-dimensions grid)
- with new = (make-array dims)
- for i below (first dims)
- do (loop for j below (second dims)
- do (setf (aref new i j) (consider i j grid dims)))
- finally (return new)))
- (defun consider (x y grid dims)
- (let ((neighbours (count-neighbours x y grid dims)))
- (case neighbours
- ((0 1) NIL)
- (2 (aref grid x y))
- (3 T)
- (4 NIL))))
- (defun count-neighbours (x y grid dims)
- (let* ((e (1- (first dims)))
- (f (1- (second dims)))
- (x-a (if (<= x 0) 0 (1- x)))
- (y-a (if (<= y 0) 0 (1- y)))
- (x-b (if (>= x e) e (1+ x)))
- (y-b (if (>= x f) f (1+ x))))
- (loop with counter = 0
- for i from x-a upto x-b
- do (loop for j from y-a upto y-b
- if (and (not (and (= x i) (= y j))) (aref grid i j))
- do (setf counter (1+ counter)))
- finally (return counter))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement