From 7bfefe95aba217deb8d719a147aa73c39519677d Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Mon, 30 Jun 2014 10:13:04 +0100 Subject: [PATCH] World now building correctly, and a simple printout function allows a world to be inspected. --- project.clj | 3 ++- src/mw_engine/world.clj | 31 ++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/project.clj b/project.clj index 7f2929f..8018d17 100644 --- a/project.clj +++ b/project.clj @@ -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"]]) diff --git a/src/mw_engine/world.clj b/src/mw_engine/world.clj index f303ea4..173037c 100644 --- a/src/mw_engine/world.clj +++ b/src/mw_engine/world.clj @@ -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)