removing need to reverse by making map be [x y] vs [y x].
This commit is contained in:
@@ -17,20 +17,20 @@
|
||||
(doseq [row (take-nth skip my-map)]
|
||||
(println (take-nth skip (map {1 \space 0 "W" "X" "X" "." "."} row))))))
|
||||
|
||||
(defn random-map [] (-> (vec (take (/ 240 4) (repeatedly (fn [] (vec (take (/ 320 4) (repeatedly (fn [] (rand-nth [1 1 1 5 5 0])))))))))
|
||||
(defn random-map [] (-> (vec (take 640 (repeatedly (fn [] (vec (take 480 (repeatedly (fn [] (rand-nth [1 1 1 5 5 0])))))))))
|
||||
(update-in [1 1] (constantly 1))
|
||||
(update-in [50 50] (constantly 1))))
|
||||
(update-in [639 479] (constantly 1))))
|
||||
|
||||
|
||||
(defn neighbors [[^long x ^long y] my-map]
|
||||
(let [candidates [[^long (dec x) ^long (dec y)] [x ^long (dec y)] [^long (inc x) ^long (dec y)]
|
||||
[^long (dec x) y] [^long (inc x) y]
|
||||
[^long (dec x) ^long (inc y)] [x ^long (inc y)] [^long (inc x) ^long (inc y)]]
|
||||
width (count (first my-map))
|
||||
height (count my-map)]
|
||||
(vec (remove #(= 0 (get-in my-map (reverse %)))
|
||||
(filter (fn [[x y]] (and (< -1 x width)
|
||||
(< -1 y height))) candidates)))))
|
||||
height (count (first my-map))
|
||||
width (count my-map)]
|
||||
(remove #(= 0 (get-in my-map %))
|
||||
(filter (fn [[x y]] (and (< -1 x width)
|
||||
(< -1 y height))) candidates))))
|
||||
|
||||
|
||||
(defn resolve-path [came-from play-loc target-loc]
|
||||
@@ -62,7 +62,7 @@
|
||||
(defn visit-all [my-map play-loc target-loc]
|
||||
(let [play-loc (vec play-loc)
|
||||
target-log (vec target-loc)]
|
||||
(if (= 0 (get-in my-map (reverse target-loc)))
|
||||
(if (= 0 (get-in my-map target-loc))
|
||||
nil
|
||||
(let [cost-so-far ^java.util.HashMap (java.util.HashMap. {play-loc 0})
|
||||
came-from ^java.util.HashMap (java.util.HashMap.)
|
||||
@@ -74,7 +74,7 @@
|
||||
(resolve-path came-from play-loc target-loc)
|
||||
(do (doseq [neighbor (neighbors (:loc current-loc) my-map)]
|
||||
(let [cost-for-neighbor (.get cost-so-far neighbor)
|
||||
new-cost (+ (.get cost-so-far (:loc current-loc)) (get-in my-map (reverse neighbor)))]
|
||||
new-cost (+ (.get cost-so-far (:loc current-loc)) (get-in my-map neighbor))]
|
||||
(when (or (nil? cost-for-neighbor)
|
||||
(< new-cost cost-for-neighbor))
|
||||
(.put came-from (vec neighbor) (vec (:loc current-loc)))
|
||||
@@ -97,17 +97,17 @@
|
||||
|
||||
(defn test-pathfind []
|
||||
(let [my-map (random-map)
|
||||
path (visit-all my-map [1 1] [50 50])]
|
||||
path (visit-all my-map [1 1] [639 479 ])]
|
||||
(println "Test")
|
||||
(print-resolved path my-map)
|
||||
#_(print-resolved path my-map)
|
||||
(println path)))
|
||||
|
||||
(defn map-from-resource [filename]
|
||||
(let [pm (pixmap filename)
|
||||
black (color 0 0 0 255)
|
||||
painful (color 255 0 0 255)]
|
||||
(vec (for [y (reverse (range (pixmap! pm :get-height)))]
|
||||
(vec (for [x (range (pixmap! pm :get-width))
|
||||
(vec (for [x (range (pixmap! pm :get-width))]
|
||||
(vec (for [y (reverse (range (pixmap! pm :get-height)))
|
||||
:let [current-color (color (pixmap! pm :get-pixel x y))]]
|
||||
(cond
|
||||
(color! current-color :equals black) 0
|
||||
|
||||
Reference in New Issue
Block a user