Created
September 1, 2019 12:37
-
-
Save craftybones/ec44ed56405e282ed7be9de2d490fc74 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
(def suits (partial map :suit)) | |
(def ranks (partial map :rank)) | |
(def by-rank (partial group-by :rank)) | |
(defn largest-group [hand] | |
(->> hand | |
by-rank | |
vals | |
(apply max-key count))) | |
(defn consecutive-pairs [coll] (map vector coll (rest coll))) | |
(defn succ? [x y] (= (succ x) y)) | |
(defn successive? | |
[hand] | |
(every? (partial apply succ?) (consecutive-pairs hand))) | |
(defn straight? | |
[hand] | |
(let [hand-ranks (sort (ranks hand))] | |
(cond | |
(= [2 3 4 5 14] (sort hand-ranks)) true | |
(successive? hand-ranks) true | |
:else false))) | |
(defn same-suit? | |
[c1 c2] | |
(= (:suit c1) (:suit c2))) | |
(defn flush? [hand] | |
(->> (rest hand) | |
(map vector hand) | |
(every? (partial apply same-suit?)))) | |
(def straight-flush? (every-pred straight? flush?)) | |
(defn royal-flush? | |
[hand] | |
(and (straight-flush? hand) (= 10 (:rank (apply min-key :rank hand))))) | |
(defn classify-pairs | |
[hand] | |
(let [rank-groups (map (comp count second) (by-rank hand)) | |
largest-group (apply max rank-groups)] | |
(condp = largest-group | |
4 :four-of-a-kind | |
3 (if (= 2 (count rank-groups)) :full-house :three-of-a-kind) | |
2 (if (= 3 (count rank-groups)) :two-pair :one-pair)))) | |
(defn ranking | |
[hand] | |
(cond | |
(royal-flush? hand) :royal-flush | |
(straight-flush? hand) :straight-flush | |
(straight? hand) :straight | |
(flush? hand) :flush | |
(= 1 (largest-group hand)) :high-card | |
:else (classify-pairs hand))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment