Created
June 27, 2012 15:04
-
-
Save mjg123/3004654 to your computer and use it in GitHub Desktop.
go-go-go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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