World now building correctly, and a simple printout function allows
a world to be inspected.
This commit is contained in:
parent
53a09fddf6
commit
7bfefe95ab
|
@ -3,4 +3,5 @@
|
||||||
:url "http://example.com/FIXME"
|
:url "http://example.com/FIXME"
|
||||||
:license {:name "Eclipse Public License"
|
:license {:name "Eclipse Public License"
|
||||||
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
: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"]])
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
(ns mw-engine.world)
|
(ns mw-engine.world
|
||||||
|
(:require [clojure.math.combinatorics :as combo]))
|
||||||
|
|
||||||
(defn make-cell
|
(defn make-cell
|
||||||
"Create a default cell at x, y"
|
"Create a default cell at x, y"
|
||||||
|
@ -32,13 +33,33 @@
|
||||||
(and (>= x 0)(>= y 0)(< y (count world))(< x (count (first world)))))
|
(and (>= x 0)(>= y 0)(< y (count world))(< x (count (first world)))))
|
||||||
|
|
||||||
(defn get-cell
|
(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]
|
[world x y]
|
||||||
(cond (in-bounds world x y)
|
(cond (in-bounds world x y)
|
||||||
(nth (nth world y) x)))
|
(nth (nth world y) x)))
|
||||||
|
|
||||||
(defn get-neighbours
|
(defn get-neighbours
|
||||||
"Get the neighbours to distance depth of the cell at x, y in this world.
|
"Get the neighbours to distance depth of the cell at x, y in this world."
|
||||||
NOTE: this implementation is broken, it gets the diagonal"
|
|
||||||
[world x y depth]
|
[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)
|
||||||
|
|
Loading…
Reference in a new issue