Skip to content
Browse files

change in direction

  • Loading branch information...
1 parent c91395a commit 0a868b1ab63c84a4722ce9169bb047ec9e667f09 @adammartin committed
Showing with 81 additions and 0 deletions.
  1. +52 −0 src/water/core.clj
  2. +29 −0 test/water/core_test.clj
View
52 src/water/core.clj
@@ -2,6 +2,10 @@
(defrecord Position [x y])
+(defrecord Point [value position])
+
+(defrecord RelativePoint [value point direction])
+
(defrecord Node [value position north south east west drainsTo])
(defn make-position [x y] (Position. x y) )
@@ -15,6 +19,54 @@
nil)
)
+(defn make-point [x y relation maxX maxY grid]
+ (let [position (make-neighbor x y relation maxX maxY)]
+ (if (not (nil? position))
+ (Point. (nth (nth grid (:y position)) (:x position))
+ position)
+ )
+ )
+)
+
+
+(defn make-relative [x y relation maxX maxY grid]
+ (let [relative (make-point x y relation maxX maxY grid)]
+ (if (not (nil? relative))
+ (RelativePoint. (:value relative) relative relation)
+ )
+ )
+)
+
+(defn to-map [relatives] (into {} (for [relative relatives] [(:direction relative) relative])))
+
+; Need help to figure out a better way to do this
+(defn filter-relatives [relatives]
+ (if(contains? relatives :self)
+ (relatives :self)
+ (if(contains? relatives :north)
+ (relatives :north)
+ (if(contains? relatives :west)
+ (relatives :west)
+ (if(contains? relatives :east)
+ (relatives :east)
+ (relatives :south)
+ )
+ )
+ )
+ )
+)
+
+(defn drains-to? [x y grid maxX maxY] (let [relatives (sort-by :value (remove nil? (list
+ (make-relative x y :north maxX maxY grid)
+ (make-relative x y :south maxX maxY grid)
+ (make-relative x y :east maxX maxY grid)
+ (make-relative x y :west maxX maxY grid)
+ (RelativePoint. (nth (nth grid y) x)
+ (Point. (nth (nth grid y) x) (make-position x y)) :self ))))
+ minimum (:value (first relatives))
+ filtered-relatives (to-map (filter #(= (:value %) minimum) relatives))]
+ (:point (filter-relatives filtered-relatives))))
+
(defn make-node [value x y maxX maxY] (Node. value (make-position x y)
(make-neighbor x y :north maxX maxY)
(make-neighbor x y :south maxX maxY)
View
29 test/water/core_test.clj
@@ -41,6 +41,35 @@
(fact (make-neighbor 2 0 :west 2 0) => (water.core.Position. 1 0))
+(fact (make-point 0 0 :north 0 0 [[0]]) => nil)
+
+(fact (make-point 1 1 :north 1 1 [[0 1] [2 3]]) => (water.core.Point. 1 (water.core.Position. 1 0) ))
+
+(fact (make-relative 0 0 :north 0 0 [[0]]) => nil)
+
+(fact (make-relative 1 1 :north 1 1 [[0 1] [2 3]]) => (water.core.RelativePoint. 1
+ (water.core.Point. 1 (water.core.Position. 1 0)) :north))
+
+(fact (drains-to? 1 1 [[0 1] [2 3]] 1 1) => (water.core.Point. 1 (water.core.Position. 1 0)) )
+
+(fact (drains-to? 0 1 [[0 1] [2 3]] 1 1) => (water.core.Point. 0 (water.core.Position. 0 0)) )
+
+(fact (drains-to? 0 0 [[0 1] [2 3]] 1 1) => (water.core.Point. 0 (water.core.Position. 0 0)) )
+
+(fact (drains-to? 0 1 [[0 1] [2 3]] 1 1) => (water.core.Point. 0 (water.core.Position. 0 0)) )
+
+(fact (drains-to? 0 1 [[2 3] [1 0]] 1 1) => (water.core.Point. 0 (water.core.Position. 1 1)) )
+
+(fact (drains-to? 1 1 [[0 0 0] [0 0 0] [0 0 0]] 2 2) => (water.core.Point. 0 (water.core.Position. 1 1)) )
+
+(fact (drains-to? 1 1 [[0 0 0] [0 1 0] [0 0 0]] 2 2) => (water.core.Point. 0 (water.core.Position. 1 0)) )
+
+(fact (drains-to? 1 1 [[0 1 0] [0 1 0] [0 0 0]] 2 2) => (water.core.Point. 0 (water.core.Position. 0 1)) )
+
+(fact (drains-to? 1 1 [[0 1 0] [1 1 0] [0 0 0]] 2 2) => (water.core.Point. 0 (water.core.Position. 2 1)) )
+
+(fact (drains-to? 1 1 [[0 1 0] [1 1 1] [0 0 0]] 2 2) => (water.core.Point. 0 (water.core.Position. 1 2)) )
+
(fact (make-node 1 0 0 0 0) => firstNode)
(fact (make-node 5 1 1 2 2) => anode)

0 comments on commit 0a868b1

Please sign in to comment.
Something went wrong with that request. Please try again.