Skip to content

Instantly share code, notes, and snippets.

@mjg123
Created June 27, 2012 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjg123/3004654 to your computer and use it in GitHub Desktop.
Save mjg123/3004654 to your computer and use it in GitHub Desktop.
go-go-go
(defn empty-board [s]
{:size s :stones {} :next :black :status :ok})
(def other {:black :white :white :black})
(defn play-stone [brd pos]
(-> brd
(assoc-in [:stones pos] (brd :next))
(assoc :next (other (brd :next)))))
(defn pass [brd]
(-> brd
(assoc :next (other (brd :next)))))
(defn neighbours [[x y]]
(map (fn [[dx dy]] [(+ x dx) (+ y dy)]) [[0 1][0 -1][1 0][-1 0]]))
(defn outside? [{size :size} [x y]]
(not (and (<= 0 x (dec size))
(<= 0 y (dec size)))))
(defn colour [board pos]
(get-in board [:stones pos]))
(defn same-colour-neighbours [board pos]
(filter #(= (colour board pos) (colour board %))
(remove #(outside? board %)(neighbours pos))))
(defn grp [board pos]
(let [col (colour board pos)]
(if (nil? col) #{}
(loop [up-next #{pos}
seen #{}]
(let [f (first up-next)
n (next up-next)]
(recur n (conj seen f)))))))
(grp
(-> (empty-board 5)
(play-stone [2 2])
(play-stone [2 3]))
[2 2])
(same-colour-neighbours
(-> (empty-board 5)
(play-stone [0 0])
(play-stone [0 1])
(play-stone [1 0]))
[0 0])
(neighbours [2 2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment