Created
November 14, 2015 18:41
-
-
Save jamesnvc/f5fc6f9b1106d52ed88e to your computer and use it in GitHub Desktop.
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
(let [won-subboard (fn [board] | |
(let [all-equal (fn [v] (and (apply = v) (first v)))] | |
(or | |
; horizontal lines | |
(all-equal (subvec board 0 3)) | |
(all-equal (subvec board 3 6)) | |
(all-equal (subvec board 6 9)) | |
; vertical lines | |
(all-equal (vals (select-keys board [0 3 6]))) | |
(all-equal (vals (select-keys board [1 4 7]))) | |
(all-equal (vals (select-keys board [2 5 8]))) | |
; diagonals | |
(all-equal (vals (select-keys board [0 4 8]))) | |
(all-equal (vals (select-keys board [2 4 6])))))) | |
board-decided? (fn [board] (or (won-subboard board) (not-any? nil? board)))] | |
(fn [{:strs [history grid] :as state}] | |
(if (empty? history) | |
; I'm first player | |
(pr-str [2 2]) | |
; otherwise, we need to play in the correct sub-board | |
(let [[b sb] (get (last history) "move") | |
board-idx (if (board-decided? (grid sb)) | |
(->> (range 0 9) (remove (comp board-decided? grid)) rand-nth) | |
sb) | |
board (grid board-idx)] | |
(pr-str | |
[board-idx | |
(->> (range 0 9) | |
(filter (comp nil? (partial get board))) | |
rand-nth)]))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment