From ea73010dd2163a475645bfbe65dd0e3760caef8c Mon Sep 17 00:00:00 2001 From: David James Humphreys Date: Mon, 7 Mar 2011 06:54:58 +0000 Subject: [PATCH 1/2] Added a new ns 'generate' that creates the map in a list-based way --- src/footpad/generate.clj | 32 ++++++++++++++++++++++++++++++++ test/footpad/generate_test.clj | 26 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/footpad/generate.clj create mode 100644 test/footpad/generate_test.clj diff --git a/src/footpad/generate.clj b/src/footpad/generate.clj new file mode 100644 index 0000000..6e66aa2 --- /dev/null +++ b/src/footpad/generate.clj @@ -0,0 +1,32 @@ +(ns footpad.generate) + +(defstruct point :xloc :type) + +;; Make it easier to refer to the types of characters +;; that we deal with. +(def game-chars {:rock \# + :up \> + :down \< + :space \space}) + +(defn print-out [ p ] + ((:type p) game-chars)) + +(defn pad-spaces [amount] + (take amount (repeat (:space game-chars)))) + +(defn add-point [coll p] + (concat + coll + (pad-spaces (- (:xloc p) (count coll))) + (list ((:type p) game-chars)))) + +(defn to-points [ coll ] + (if (zero? (mod (count coll) 2)) + (map #(apply struct point %1 ) (partition 2 coll)))) + +;; (defn full-wall [ s ] +;; (map #(struct point % :rock) (range 0 s))) + +(defn print-game-line [ coll ] + (String. (char-array (reduce add-point () (to-points coll))))) diff --git a/test/footpad/generate_test.clj b/test/footpad/generate_test.clj new file mode 100644 index 0000000..f9b16cc --- /dev/null +++ b/test/footpad/generate_test.clj @@ -0,0 +1,26 @@ +(ns footpad.generate-test + (:use footpad.generate + clojure.test :reload)) + + +(deftest padding + (testing "Zero padding" + (= () (pad-spaces 0))) + (testing "Other cases" + (= (list \space) (pad-spaces 1)) + (= (list \space \space) (pad-spaces 2)))) + +(deftest adding + (testing "Simple case" + (= (list \#) (add-point () (struct point 0 :rock))) + ) + (testing "Padded case" + (= (list \space \space \#) (add-point () (struct point 2 :rock)))) + (testing "Pre-populated list" + (= (list \# \space \space \space \>) (add-point (list \#) (struct point 4 :up)))) + ) + +(deftest converting + (testing "Simple case" + (= (list (struct point 2 :rock) (struct point 6 :up)) + (to-points [2 :rock 6 :up])))) From b465556708a573eb2e5bbb3c573515d93e7e25b9 Mon Sep 17 00:00:00 2001 From: David James Humphreys Date: Mon, 7 Mar 2011 06:57:22 +0000 Subject: [PATCH 2/2] Changed the order of the point struct to allow for a default yloc --- src/footpad/generate.clj | 2 +- test/footpad/generate_test.clj | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/footpad/generate.clj b/src/footpad/generate.clj index 6e66aa2..bcaba8a 100644 --- a/src/footpad/generate.clj +++ b/src/footpad/generate.clj @@ -1,6 +1,6 @@ (ns footpad.generate) -(defstruct point :xloc :type) +(defstruct point :type :xloc :yloc) ;; Make it easier to refer to the types of characters ;; that we deal with. diff --git a/test/footpad/generate_test.clj b/test/footpad/generate_test.clj index f9b16cc..dfb6bbb 100644 --- a/test/footpad/generate_test.clj +++ b/test/footpad/generate_test.clj @@ -12,15 +12,15 @@ (deftest adding (testing "Simple case" - (= (list \#) (add-point () (struct point 0 :rock))) + (= (list \#) (add-point () (struct point :rock 0))) ) (testing "Padded case" - (= (list \space \space \#) (add-point () (struct point 2 :rock)))) + (= (list \space \space \#) (add-point () (struct point :rock 2)))) (testing "Pre-populated list" - (= (list \# \space \space \space \>) (add-point (list \#) (struct point 4 :up)))) + (= (list \# \space \space \space \>) (add-point (list \#) (struct point :up 4)))) ) (deftest converting (testing "Simple case" - (= (list (struct point 2 :rock) (struct point 6 :up)) - (to-points [2 :rock 6 :up])))) + (= (list (struct point :rock 2) (struct point :up 6)) + (to-points [:rock 2 :up 6]))))