Last active
July 15, 2017 06:19
-
-
Save jreighley/6aa4505fa2048360a64fa7cef6e593d6 to your computer and use it in GitHub Desktop.
Clojure.spec Chess 960
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
(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