Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/footpad/generate.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns footpad.generate)

(defstruct point :type :xloc :yloc)

;; 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)))))
26 changes: 26 additions & 0 deletions test/footpad/generate_test.clj
Original file line number Diff line number Diff line change
@@ -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 :rock 0)))
)
(testing "Padded case"
(= (list \space \space \#) (add-point () (struct point :rock 2))))
(testing "Pre-populated list"
(= (list \# \space \space \space \>) (add-point (list \#) (struct point :up 4))))
)

(deftest converting
(testing "Simple case"
(= (list (struct point :rock 2) (struct point :up 6))
(to-points [:rock 2 :up 6]))))