Skip to content

Instantly share code, notes, and snippets.

@jreighley
Last active July 15, 2017 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jreighley/6aa4505fa2048360a64fa7cef6e593d6 to your computer and use it in GitHub Desktop.
Save jreighley/6aa4505fa2048360a64fa7cef6e593d6 to your computer and use it in GitHub Desktop.
Clojure.spec Chess 960
(ns sssmakespec.c960
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]))
(def correct-pieces? #(= {\R 2, \K 1, \B 2, \Q 1, \N 2} (frequencies %))) ;; The right pieces and only the right peices
(def bishops-opposite? #(odd? (+ (.indexOf (vec %) \B) (.lastIndexOf (vec %) \B)))) ;; one bishop on even squares one on odd
(def K-between-R? #(< (.indexOf (vec %) \R) (.indexOf (vec %) \K) (.lastIndexOf (vec %) \R))) ;;King between the rooks
(s/def ::validc960 (s/and string?
correct-pieces?
bishops-opposite?
K-between-R?))
(defn makeseq []
(->> ["Q" "K" "R" "R" "B" "B" "N" "N"]
(shuffle)
(apply str)))
(def possible-gamelist
(->> (repeatedly makeseq)
(filter #(s/valid? ::validc960 %))
(distinct)
(take 960)))
(defn get-game []
(nth possible-gamelist (rand-int 960)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment