Advertisement
Shinmera

Cheap and Shitty Game of Life Implementation

Jun 19th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.04 KB | None | 0 0
  1. (defpackage game-of-life
  2.   (:use :cl)
  3.   (:export :do-step))
  4.  
  5. (in-package :game-of-life)
  6.  
  7. (defun do-step (grid)
  8.   (loop with dims = (array-dimensions grid)
  9.        with new = (make-array dims)
  10.      for i below (first dims)
  11.      do (loop for j below (second dims)
  12.            do (setf (aref new i j) (consider i j grid dims)))
  13.      finally (return new)))
  14.  
  15. (defun consider (x y grid dims)
  16.   (let ((neighbours (count-neighbours x y grid dims)))
  17.     (case neighbours
  18.       ((0 1) NIL)
  19.       (2 (aref grid x y))
  20.       (3 T)
  21.       (4 NIL))))
  22.  
  23. (defun count-neighbours (x y grid dims)
  24.   (let* ((e (1- (first dims)))
  25.          (f (1- (second dims)))
  26.          (x-a (if (<= x 0) 0 (1- x)))
  27.          (y-a (if (<= y 0) 0 (1- y)))
  28.          (x-b (if (>= x e) e (1+ x)))
  29.          (y-b (if (>= x f) f (1+ x))))
  30.     (loop with counter = 0
  31.        for i from x-a upto x-b
  32.        do (loop for j from y-a upto y-b
  33.              if (and (not (and (= x i) (= y j))) (aref grid i j))
  34.              do (setf counter (1+ counter)))
  35.        finally (return counter))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement