         (push animal-nu evolution-animals)))))
 
 (defun update-world ()
+  (incf evolution-generation)
   (setf evolution-animals (remove-if (lambda (animal)
                                        (<= (animal-energy animal) 0))
                                      evolution-animals))
+  (add-plants)
   (mapc (lambda (animal)
           (turn animal)
           (move animal)
           (eat animal)
           (reproduce animal))
-        evolution-animals)
-  (add-plants))
+        evolution-animals))
 
 (defun animal-color (a)
-  (let ((color (max a 255)))
-    (format "#00%X00" color)))
+  (let ((color (min (animal-energy a) 255)))
+    (format "#%2X0000" color)))
+
+;; current generation
+(defvar evolution-generation 0)
 
 (defun draw-world ()
-  (erase-buffer)
-  (loop for y
-        below evolution-height
-        do (progn (newline)
-                  (insertc "|" evolution-fence-color nil)
-                  (loop for x
-                        below evolution-width
-                        do (cond ((some (lambda (animal)
-                                          (and (= (animal-x animal) x)
-                                               (= (animal-y animal) y)))
-                                        evolution-animals)
-                                  (insertc "m" (animal-color animal) nil))
-                                 ((gethash (cons x y) evolution-plants)
-                                  (insertc "*" "green" nil))
-                                 (t (insertc " " "darkgrey" nil))))
-                  (insertc "|" evolution-fence-color nil)))
-  (newline)
-  (insert (format "Animals: %d -- Plants: %d"
-                  (length evolution-animals)
-                  (hash-table-count evolution-plants)
-                  0))
-  (redisplay t))
+  (let ((inhibit-read-only t))
+    (erase-buffer)
+    (loop for y
+          below evolution-height
+          do (progn (newline)
+                    (insertc "|" evolution-fence-color nil)
+                    (let ((anml nil))
+                      (loop for x
+                            below evolution-width
+                            do (cond ((some (lambda (animal)
+                                              (and (= (animal-x animal) x)
+                                                   (= (animal-y animal) y)
+                                                   (setq anml animal)))
+                                            evolution-animals)
+                                      (insertc "m" (animal-color anml) nil))
+                                     ((gethash (cons x y) evolution-plants)
+                                      (insertc "*" "green" nil))
+                                     (t (insertc " " "darkgrey" nil)))))
+                    (insertc "|" evolution-fence-color nil)))
+    (newline)
+    (insert (format "Generation %d || %d animals -- %d plants"
+                    evolution-generation
+                    (length evolution-animals)
+                    (hash-table-count evolution-plants)
+                    0))
+    (redisplay t)))
+
+(defun evolution-init ()
+  (setq evolution-animals
+        (list (make-animal :x      (ash evolution-width -1)
+                           :y      (ash evolution-height -1)
+                           :energy 1000
+                           :dir    0
+                           :genes  (loop repeat 8
+                                         collecting (1+ (random 10))))))
+  (setq evolution-plants (make-hash-table :test #'equal))
+  (setq evolution-generation 0))
 
 (defun evolution-start ()
   (interactive)
+  (evolution-init)
+  (draw-world)
+  (evolution-function))
+
+(defun evolution-function ()
+  (interactive)
   (let ((inhibit-read-only t))
-    (draw-world)
-    (newline)
     (let ((str (read-string "Steps (or 'quit'): ")))
       (cond ((equal str "quit") ())
             (t (let ((x (string-to-int str)))

;; evolution.el -- the evolution demo, ported to EmacsLisp

(defvar evolution-width 80)
(defvar evolution-height 30)
(defvar evolution-jungle-rectangle '(45 10 10 10))
(defvar evolution-plant-energy 20)

(defvar evolution-plants (make-hash-table :test #'equal))

;; Create an initial animal, centered, with random genes.
;;
;; The genes determine the likelihood of the animal going in a certain
;; direction
(defvar evolution-animals
  (list (make-animal :x      (ash evolution-width -1)
                     :y      (ash evolution-height -1)

                           below x
                           do (update-world)
                           (draw-world))
                   (evolution-function))))))))

