5e2ad425aa5a

Wrap up day 1.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 12 Dec 2015 19:01:22 +0000
parents bf284ae6d81f
children 1e658fe8fe4b
branches/tags (none)
files project.clj src/silt/core.clj

Changes

--- a/project.clj	Sat Dec 12 18:35:54 2015 +0000
+++ b/project.clj	Sat Dec 12 19:01:22 2015 +0000
@@ -6,7 +6,7 @@
   :dependencies [[org.clojure/clojure "1.7.0"]
                  [clojure-lanterna "0.9.5"]
                  [roul "0.2.0"]
-                 ]
+                 [org.clojure/core.match "0.3.0-alpha4"]]
   :main ^:skip-aot silt.core
   :target-path "target/%s"
   :profiles {:uberjar {:aot :all}})
--- a/src/silt/core.clj	Sat Dec 12 18:35:54 2015 +0000
+++ b/src/silt/core.clj	Sat Dec 12 19:01:22 2015 +0000
@@ -1,6 +1,7 @@
 (ns silt.core
   (:require [lanterna.screen :as s]
             [roul.random :as rr]
+            [clojure.core.match :refer [match]]
             [clojure.stacktrace :refer [print-stack-trace]])
   (:gen-class))
 
@@ -36,6 +37,7 @@
 
 (def world-temp (ref 0))
 (def mutation-chance 10)
+(def reproduction-rate 5)
 
 (def animals (ref []))
 (def initial-animals 400)
@@ -112,8 +114,20 @@
                (normalize-world-coords [(+ x dx) (+ y dy)]))
              (rr/rand-nth directions)))
 
-(defn age [animal] animal)
-(defn try-reproduce [animal] animal)
+(defn age [animal]
+  (let [{:keys [age] :as animal} (update-in animal [:age] inc)]
+    (if (and (> age 50)
+             (rr/rand-bool (inc (/ (:age animal) 100))))
+      []
+      [animal])))
+
+(defn try-reproduce [animals]
+  (match animals
+    [] []
+    [animal] (if (and (can-reproduce animal)
+                      (rr/rand-bool reproduction-rate))
+               (reproduce animal)
+               animals)))
 
 (defn tick-animal [animal]
   (-> animal
@@ -122,8 +136,7 @@
     find-resources
     wander
     age
-    try-reproduce
-    vector))
+    try-reproduce))
 
 (defn tick-animals [animals]
   (vec (mapcat tick-animal animals)))
@@ -165,10 +178,14 @@
   (conj (for [_ (range initial-animals)]
           (-> eve
             clone
+            (assoc :energy 100)
             (assoc :loc (random-coord))))
         eve))
 
 
+(defn reset-window! []
+  (dosync (ref-set window-loc [0 0])))
+
 (defn reset-terrain! []
   (let [new-terrain (-> (generate-terrain)
                       (into (generate-water))
@@ -225,6 +242,7 @@
     (draw-landmarks! screen)
     (put-right " SILT  " 0)
     (put-right (str @world-temp "° ") 1)
+    (put-right (str (count @animals) "  ") 2)
     (s/move-cursor screen (from-right screen 1) 0)
     (s/redraw screen)))
 
@@ -259,14 +277,32 @@
                    \9 10000))))
   nil)
 
+(defn reset-world! []
+  (reset-window!)
+  (reset-terrain!)
+  (reset-animals!))
+
 (defn handle-input! [screen]
   (while-let [key (s/get-key screen)]
     (case key
-      :escape (reset! running false)
-      (:up :down :left :right) (move-window! key 10)
-      (\h \j \k \l) (move-window! key)
-      \space (update-animals! \1)
-      (\1 \2 \3 \4 \5 \6 \7 \8 \9) (update-animals! key)
+      :escape
+      (reset! running false)
+
+      (:up :down :left :right)
+      (move-window! key 10)
+
+      (\h \j \k \l)
+      (move-window! key)
+
+      \space
+      (update-animals! \1)
+
+      (\1 \2 \3 \4 \5 \6 \7 \8 \9)
+      (update-animals! key)
+
+      \r
+      (reset-world!)
+
       nil)))
 
 
@@ -301,6 +337,7 @@
   (reset! running true)
   (dosync (ref-set world-temp 0))
 
+  (reset-window!)
   (reset-terrain!)
   (reset-animals!)