add helper module to work with mapped areas
This commit is contained in:
parent
ff7fc15880
commit
09df1d724c
73
src/mapped_area.clj
Normal file
73
src/mapped_area.clj
Normal file
@ -0,0 +1,73 @@
|
||||
(ns mapped-area
|
||||
(:require [clojure.string :as string]))
|
||||
|
||||
(def test-map
|
||||
"abcd
|
||||
efgh
|
||||
ijkl")
|
||||
|
||||
(defn read-mapped-area
|
||||
{:test
|
||||
#(do
|
||||
(let [a (read-mapped-area test-map)]
|
||||
(assert (= \i (get-in a [0 0])))
|
||||
(assert (= \f (get-in a [1 1])))
|
||||
(assert (= \a (get-in a [0 2])))
|
||||
(assert (nil? (get-in a [10 10])))
|
||||
(assert (nil? (get-in a [1 3])))
|
||||
(assert (= \h (get-in a [3 1])))))}
|
||||
[s]
|
||||
(let [ss (string/split-lines s)
|
||||
lines (count ss)]
|
||||
(->>
|
||||
(reverse ss)
|
||||
(apply interleave)
|
||||
(partition lines)
|
||||
(mapv vec))))
|
||||
|
||||
(defn print-mapped-area [a]
|
||||
(let [lines (count (first a))]
|
||||
(doall
|
||||
(->>
|
||||
(map reverse a)
|
||||
(apply interleave)
|
||||
(partition lines)
|
||||
(map (partial apply str))
|
||||
(map println))))
|
||||
a)
|
||||
|
||||
(comment
|
||||
(print-mapped-area (read-mapped-area test-map))
|
||||
)
|
||||
|
||||
(defn- h
|
||||
{:test #(do (assert (= (h (read-mapped-area test-map)) 3)))}
|
||||
[a]
|
||||
(count (first a)))
|
||||
|
||||
(defn- w
|
||||
{:test #(do (assert (= (w (read-mapped-area test-map)) 4)))}
|
||||
[a]
|
||||
(count a))
|
||||
|
||||
(defn mapped?
|
||||
{:test
|
||||
#(do
|
||||
(let [a (read-mapped-area test-map)]
|
||||
(assert (true? (mapped? a 1 1)))
|
||||
(assert (true? (mapped? a [2 2])))
|
||||
(assert (false? (mapped? nil -1 0)))
|
||||
(assert (false? (mapped? a 4 0)))
|
||||
(assert (true? (mapped? a 3 1)))
|
||||
(assert (false? (mapped? a 1 3)))))}
|
||||
([a [x y]] (mapped? a x y))
|
||||
([a x y]
|
||||
(and (>= x 0)
|
||||
(>= y 0)
|
||||
(< x (w a))
|
||||
(< y (h a)))))
|
||||
|
||||
(test #'read-mapped-area)
|
||||
(test #'h)
|
||||
(test #'w)
|
||||
(test #'mapped?)
|
||||
Loading…
x
Reference in New Issue
Block a user