Skip to content

Instantly share code, notes, and snippets.

@eval
Forked from cgrand/set-game.clj
Last active May 10, 2017 07:46
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 eval/00607db45cf730ee97b9727d3767cabd to your computer and use it in GitHub Desktop.
Save eval/00607db45cf730ee97b9727d3767cabd to your computer and use it in GitHub Desktop.
the SET game in clojure.spec
;; the SET game in clojure.spec
;; inspired by https://github.com/jgrodziski/set-game
;; run via:
;; $ lein try org.clojure/spec.alpha org.clojure/test.check "0.9.0"
;; requires the following in ~/.lein/profiles.clj
;; {:repl
;; {:dependencies [
;; ^:displace [org.clojure/clojure "1.9.0-alpha15"]
;; [org.clojure/test.check "0.9.0"]
;; ]}
;;}
(require '[clojure.spec :as s])
(require '[clojure.test.check.generators :as tcg])
(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]
#(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 (tcg/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)))
;;;;;; repl
=> (sets (deal))
#{#{#:user{:shape :oval, :color :purple, :value 1, :shading :outline}
#:user{:shape :diamond, :color :red, :value 3, :shading :solid}
#:user{:shape :squiggle, :color :green, :value 2, :shading :striped}}
#{#:user{:shape :diamond, :color :green, :value 3, :shading :solid}
#:user{:shape :oval, :color :purple, :value 1, :shading :striped}
#:user{:shape :squiggle, :color :red, :value 2, :shading :outline}}
#{#:user{:shape :diamond, :color :green, :value 3, :shading :solid}
#:user{:shape :oval, :color :purple, :value 2, :shading :solid}
#:user{:shape :squiggle, :color :red, :value 1, :shading :solid}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment