Skip to content

Instantly share code, notes, and snippets.

@reiddraper
Created April 18, 2012 23:35
Show Gist options
  • Select an option

  • Save reiddraper/2417386 to your computer and use it in GitHub Desktop.

Select an option

Save reiddraper/2417386 to your computer and use it in GitHub Desktop.
(require '[crosscram.core :as crosscram] :reload)
(require '[clojure.core.match :as match])
(require '[clojure.set :as set])
(require 'clojure.pprint)
(defn move [game]
(let [rows (:rows game)
columns (:columns game)]
(loop [moves (match/match (:next-player game)
:horizontal (crosscram/generate-horizontal rows columns)
:vertical (crosscram/generate-vertical rows columns))]
(if (apply crosscram/location-empty? (:board game) (first moves))
(first moves)
(recur (rest moves))))))
(defn random-moves [game]
(let [rows (:rows game)
columns (:columns game)]
(loop [moves (match/match (:next-player game)
:horizontal (crosscram/generate-horizontal rows columns)
:vertical (crosscram/generate-vertical rows columns))]
(let [m (set/difference (set moves) (set (map :move (:history game))))
random-move (rand-nth (vec m))]
(if (apply crosscram/location-empty? (:board game) random-move)
random-move
(recur (remove #{random-move} m)))))))
(def g (crosscram/new-game 9 9 :horizontal))
(time (clojure.pprint/pprint (apply merge-with +
(map (fn [k] {k 1})
(map :winner (for [x (range 100)] (crosscram/play-symmetric g random-moves move 10)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment