-
-
Save viebel/7d9534250b3eb8be087005ec11c4ebe5 to your computer and use it in GitHub Desktop.
the SET game in clojure.spec
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
; the SET game in clojure.spec | |
;; inspired by https://github.com/jgrodziski/set-game | |
(require '[clojure.spec :as s]) | |
(require 'clojure.test.check.generators) | |
(require '[clojure.spec.impl.gen :as gen]) | |
(s/def ::shape #{:oval :diamond :squiggle}) | |
(s/def ::color #{:red :purple :green}) | |
(s/def ::value #{1 2 3}) | |
(s/def ::shading #{:solid :striped :outline}) | |
(s/def ::card (s/keys :req [::shape ::color ::value ::shading])) | |
(s/def ::deck (s/coll-of ::card :distinct true :max-count 12 :min-count 12)) | |
(defn unique-or-distinct [feature] | |
#(and (coll? %) (not= 2 (count (into #{} (map feature) %))))) ; assumes 3-item set | |
(s/def ::set | |
(s/and | |
(s/coll-of ::card :min-count 3 :max-count 3 :distinct true :into #{}) | |
(unique-or-distinct ::shape) | |
(unique-or-distinct ::color) | |
(unique-or-distinct ::value) | |
(unique-or-distinct ::shading))) | |
(defn deal [] | |
(first (gen/sample (s/gen ::deck) 1))) | |
(defn sets [deck] | |
(into #{} (for [carda deck cardb deck cardc deck | |
:let [s (hash-set carda cardb cardc)] | |
:when (s/valid? ::set s)] | |
s))) | |
(-> (sets (deal)) | |
count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment