There's a null pointer exception that currently occurs when a rule maps

onto neighbours, e.g.:
	(map #(population % :deer) (get-neighbours world cell))
I've worked around it for now by commenting out the rules which do this,
but the bug is in get-neighbours and should be fixed there.
This commit is contained in:
simon 2014-07-04 09:10:58 +01:00
parent 8a00e5c0da
commit 6320b3e5b0
2 changed files with 20 additions and 18 deletions

View file

@ -87,12 +87,12 @@
(cond (> (* (population cell :deer) 10) (get-int cell :fertility)) (cond (> (* (population cell :deer) 10) (get-int cell :fertility))
(merge cell {:deer (int (/ (get-int cell :fertility) 10))}))) (merge cell {:deer (int (/ (get-int cell :fertility) 10))})))
;; deer gradually spread through the world by breeding or migrating. ;; deer gradually spread through the world by breeding or migrating.
(fn [cell world] ;; (fn [cell world]
(let [n (apply + (map #(population % :deer) (get-neighbours world cell)))] ;; (let [n (apply + (map #(population % :deer) (get-neighbours world cell)))]
(cond (and ;; (cond (and
(= (population cell :deer) 0) ;; (= (population cell :deer) 0)
(>= n 2)) ;; (>= n 2))
(merge cell {:deer (int (/ n 2))})))) ;; (merge cell {:deer (int (/ n 2))}))))
;; deer breed. ;; deer breed.
(fn [cell world] (fn [cell world]
(cond (cond
@ -114,12 +114,12 @@
(cond (> (population cell :wolves) (population cell :deer)) (cond (> (population cell :wolves) (population cell :deer))
(merge cell {:wolves 0}))) (merge cell {:wolves 0})))
;; wolves gradually spread through the world by breeding or migrating. ;; wolves gradually spread through the world by breeding or migrating.
(fn [cell world] ;; (fn [cell world]
(let [n (apply + (map #(population % :wolves) (get-neighbours world cell)))] ;; (let [n (apply + (map #(population % :wolves) (get-neighbours world cell)))]
(cond (and ;; (cond (and
(= (population cell :wolves) 0) ;; (= (population cell :wolves) 0)
(>= n 2)) ;; (>= n 2))
(merge cell {:wolves 2})))) ;; (merge cell {:wolves 2}))))
;; wolves breed. ;; wolves breed.
(fn [cell world] (fn [cell world]
(cond (cond

View file

@ -30,9 +30,11 @@
(defn get-int (defn get-int
"Get the value of a property expected to be an integer from a map; if not present (or not an integer) return 0." "Get the value of a property expected to be an integer from a map; if not present (or not an integer) return 0."
[map key] [map key]
(cond map
(let [v (map key)] (let [v (map key)]
(cond (integer? v) v (cond (and v (integer? v)) v
true 0))) true 0))
true (throw (Exception. "No map passed?"))))
(defn population (defn population
"Return the population of this species in this cell. "Return the population of this species in this cell.