Skip to content

Instantly share code, notes, and snippets.

@ponzao
Created April 4, 2014 10:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponzao/9972091 to your computer and use it in GitHub Desktop.
Save ponzao/9972091 to your computer and use it in GitHub Desktop.
(fn best-hand
[hand]
(let [ranks (map second hand)
suits (map first hand)
rank-freqs (sort (vals (frequencies ranks)))]
(letfn [(flush? [hand]
(apply = suits))
(straight? [hand]
(let [straight-hands (set (map set (partition 5 1 [\A \2 \3 \4 \5
\6 \7 \8 \9 \T
\J \Q \K \A])))]
(straight-hands (set ranks))))]
(cond (= [2 3] rank-freqs) :full-house
(= [1 2 2] rank-freqs) :two-pair
(= [1 4] rank-freqs) :four-of-a-kind
(= [1 1 3] rank-freqs) :three-of-a-kind
(= [1 1 1 2] rank-freqs) :pair
(and (straight? hand) (flush? hand)) :straight-flush
(flush? hand) :flush
(straight? hand) :straight
:else :high-card))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment