Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
September 30, 2020
(:require [clojure.pprint :refer [pprint]]))
(def start-game [[nil nil nil]
[nil nil nil]
[nil nil nil]])
(def unwon-game [[nil nil nil]
[:x nil nil]
[nil nil nil]])
(def test-game [[nil :x nil]
[nil :x nil]
[nil :x nil]])
(def draw-game [[:o :x :o]
[:x :x :o]
[:x :o :x]])
(defn whose-turn? [game]
(let [moves (apply concat game)
xs (filter #{:x} moves)
os (filter #{:o} moves)]
(if (> (count os) (count xs))
(defn possible-moves [game]
(for [x (range 3)
y (range 3)
:when (nil? (get-in game [y x]))]
[x y]))
(defn make-move [game player [x y]]
(assoc-in game [y x] player))
(def lines
(for [y (range 3)]
(for [x (range 3)]
[x y]))
(for [x (range 3)]
(for [y (range 3)]
[x y]))
[(for [y (range 3)]
[y y])
(for [y (range 3)]
[y (- 2 y)])]))
(def not-empty? seq)
(defn winner [game]
(let [full-lines (filter (fn [line]
(every? boolean line))
(map (fn [line]
(map (fn [coord]
(get-in game coord)) line))
winner-lines (filter #(apply = %) full-lines)]
(not-empty? winner-lines)
(ffirst winner-lines)
(= 8 (count full-lines))
(defn run [game]
(let [w (winner game)]
(if w
[{:game game
:winner w}]
(let [current (whose-turn? game)
next ({:x :o :o :x} current)]
(->> game
(map #(make-move game next %))
(mapcat run))))))
(defn pp [game]
(run! println game))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment