Skip to content

Instantly share code, notes, and snippets.

@daveyarwood
Created April 4, 2014 02:59
Show Gist options
  • Save daveyarwood/9967243 to your computer and use it in GitHub Desktop.
Save daveyarwood/9967243 to your computer and use it in GitHub Desktop.
(fn best-hand [card-strings]
(let [card-parser (fn [[s r]]
(let [suit ({\S :spade, \H :heart,
\D :diamond, \C :club} s)
rank (if (> (Character/digit r 10) -1)
(- (Character/digit r 10) 2)
({\T 8, \J 9,
\Q 10, \K 11, \A 12} r))]
{:suit suit, :rank rank}))
cards (map card-parser card-strings)
suits (map :suit cards)
ranks (map :rank cards)
flush?
(if (= 1 (count (set suits))) :flush nil)
straight?
(let [aces-high (sort ranks)
aces-low (sort (replace {12 -1} ranks))]
(if (or
(= aces-high (take 5 (iterate inc (first aces-high))))
(= aces-low (take 5 (iterate inc (first aces-low)))))
:straight
nil))
straight-flush?
(if (and flush? straight?) :straight-flush nil)
pair?
(if (some (fn [[r num]] (>= num 2)) (frequencies ranks))
:pair
nil)
three-of-a-kind?
(if (some (fn [[r num]] (>= num 3)) (frequencies ranks))
:three-of-a-kind
nil)
four-of-a-kind?
(if (some (fn [[r num]] (= num 4)) (frequencies ranks))
:four-of-a-kind
nil)
two-pair?
(if (or
(some (fn [[r num]] (>= num 4)) (frequencies ranks))
(= 2 (count (filter (fn [[r num]] (>= num 2))
(frequencies ranks)))))
:two-pair
nil)
full-house?
(if (and
(some (fn [[r num]] (= num 3)) (frequencies ranks))
(some (fn [[r num]] (= num 2)) (frequencies ranks)))
:full-house
nil)
possible-hands
(remove nil? [straight-flush? four-of-a-kind? full-house? flush?
straight? three-of-a-kind? two-pair? pair?])]
(if-not (empty? possible-hands)
(first possible-hands)
:high-card)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment