World now building correctly, and a simple printout function allows

a world to be inspected.
This commit is contained in:
Simon Brooke 2014-06-30 10:13:04 +01:00
parent 53a09fddf6
commit 7bfefe95ab
2 changed files with 28 additions and 6 deletions

View file

@ -3,4 +3,5 @@
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]])
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/math.combinatorics "0.0.7"]])

View file

@ -1,4 +1,5 @@
(ns mw-engine.world)
(ns mw-engine.world
(:require [clojure.math.combinatorics :as combo]))
(defn make-cell
"Create a default cell at x, y"
@ -32,13 +33,33 @@
(and (>= x 0)(>= y 0)(< y (count world))(< x (count (first world)))))
(defn get-cell
"Return the cell a x, y in this world, if any"
"Return the cell a x, y in this world, if any."
[world x y]
(cond (in-bounds world x y)
(nth (nth world y) x)))
(defn get-neighbours
"Get the neighbours to distance depth of the cell at x, y in this world.
NOTE: this implementation is broken, it gets the diagonal"
"Get the neighbours to distance depth of the cell at x, y in this world."
[world x y depth]
(map #(get-cell world %1 %2) (range (- x depth) (+ x depth)) (range (- y depth) (+ y depth))))
(map #(get-cell world (first %) (first (rest %)))
(combo/cartesian-product
(range (- x depth) (+ x depth))
(range (- y depth) (+ y depth)))))
(defn truncate-state
"Truncate the print name of the state of this cell to at most limit characters."
[cell limit]
(let [s (:state cell)]
(cond (> (count (.toString s)) 10) (subs s 0 10)
true s)))
(defn format-world-row
"Format one row in the state of a world for printing"
[row]
(apply str
(map #(format "%10s" (truncate-state % 10)) row)))
(defn print-world
"Print the current state of this world."
[world]
(dorun (map #(println (format-world-row %)) world)) nil)