Skip to content

Instantly share code, notes, and snippets.

Created April 1, 2012 05:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2271656 to your computer and use it in GitHub Desktop.
Save anonymous/2271656 to your computer and use it in GitHub Desktop.
;; ariarule's solution to Win at Tic-Tac-Toe
;; https://4clojure.com/problem/119
(fn [player board]
(let [vboard (apply concat board)
s (range 0 3)
positions (mapcat #(map (fn [x] [% x]) s) s)
rows (partition 3 positions)
columns [(map first rows) (map second rows) (map #(nth % 2) rows)]
wins (map set (concat rows columns ['([0 0] [1 1] [2 2]) '([0 2] [1 1] [2 0])]))
piece-positions (zipmap positions vboard)
[player-positions empty-positions] (map set (map (fn [x] (map first (filter #(= (val %) x) piece-positions))) [player :e]))
]
(set
(filter #(-> % nil? not) (map #(if (some
(fn [w]
(clojure.set/subset? w (conj player-positions %)))
wins)
%)
empty-positions)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment