Created
May 20, 2012 04:14
-
-
Save MichaelDrogalis/2744695 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defrecord Value [name value]) | |
(defrecord Card [value suit]) | |
(def values | |
(map #(Value. %1 %2) [:two :three :four :five :six :seven :eight :nine :ten :jack :queen :king :ace] (range 1 14))) | |
(def suits #{:clubs :diamonds :hearts :spades}) | |
(def deck (mapcat (fn [value] | |
(map (fn [suit] (Card. value suit)) suits)) | |
values)) | |
(defn straight? [cards] | |
(let [card-vals (into #{} (map :value (map :value cards))) | |
min-val (apply min card-vals) | |
max-val (apply max card-vals)] | |
(and (= (into #{} (range min-val (inc max-val))) card-vals) | |
(= (count (range min-val (inc max-val))) 5)))) | |
(defn flush? [cards] | |
(apply = (map :suit cards))) | |
(defn four-of-a-kind? [cards] | |
(let [values (map :value (map :value cards))] | |
(some #(= (count %) 4) (partition-by identity values)))) | |
(defn straight-flush? [cards] | |
(and (flush? cards) | |
(straight? cards))) | |
(defn royal-flush? [cards] | |
(and (flush? cards) | |
(= (into #{} (map :name (map :value cards))) #{:ten :jack :queen :king :ace}))) | |
(defn how-many-until | |
([pred] (how-many-until pred 0)) | |
([pred n] | |
(let [cards (take 5 (shuffle deck))] | |
(if (pred cards) | |
n | |
(recur pred (inc n)))))) | |
(how-many-until royal-flush?) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment