Skip to content

Instantly share code, notes, and snippets.

@naush
Created February 12, 2011 22:57
Show Gist options
  • Save naush/824217 to your computer and use it in GitHub Desktop.
Save naush/824217 to your computer and use it in GitHub Desktop.
(defn ally? [board point color] (= color (nth board point)))
(defn new-ally? [board point color ally]
(and (ally? board point color) (not (.contains ally point))))
(defn find-neighbors [point size]
(let [north (- point size)
east (inc point)
south (+ point size)
west (dec point)]
(filter #(not (out-of-bound % size)) '(north east south west))))
(defn find-ally-recursive [board point color ally size]
(let [ally (cons point ally) neighbors (find-neighbors point size)]
(reduce
#(if (new-ally? board %2 color %1) ; if point is a new ally
(find-ally-recursive board %2 color %1 size) ; then continue to explore the next neighbor
%1) ; else return ally group
ally neighbors))) ; %1 = ally group, %2 = neighbors
(defn find-ally-group [board point color size]
{:color color :points (find-ally-recursive board point color () size)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment