From 0ee7a5e560b8a9b14b35db0ab7206d0ab08d6ade Mon Sep 17 00:00:00 2001 From: leaen Date: Wed, 18 Oct 2017 17:21:08 +1100 Subject: [PATCH] solved --- src/p_p_p_pokerface.clj | 54 ++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 5ea80094..8258bb35 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -1,34 +1,66 @@ (ns p-p-p-pokerface) (defn rank [card] - nil) + (let [[rank-char _] card + replacements {\T 10 \J 11 \Q 12 \K 13 \A 14}] + (cond + (Character/isDigit rank-char) (Integer/valueOf (str rank-char)) + :else (get replacements rank-char)))) (defn suit [card] - nil) + (let [[_ suit] card] + (str suit))) + +(defn rank-repeat-count [hand] + (vals (frequencies (map rank hand)))) + +(defn suit-repeat-count [hand] + (vals (frequencies (map suit hand)))) + +(defn has-n-repeats? [hand n] + (< 0 (count (filter (fn [count] (= n count)) (rank-repeat-count hand))))) (defn pair? [hand] - nil) + (has-n-repeats? hand 2)) (defn three-of-a-kind? [hand] - nil) + (has-n-repeats? hand 3)) (defn four-of-a-kind? [hand] - nil) + (has-n-repeats? hand 4)) (defn flush? [hand] - nil) + (= 5 (apply max (suit-repeat-count hand)))) (defn full-house? [hand] - nil) + (and (pair? hand) (three-of-a-kind? hand))) (defn two-pairs? [hand] - nil) + (or (four-of-a-kind? hand) + (= '(2 2) (filter (fn [repeat] (= 2 repeat)) (rank-repeat-count hand))))) + +(defn sequential-ranks [rank-seq] + (range (apply min rank-seq) (+ 1( apply max rank-seq)))) (defn straight? [hand] - nil) + (let [r (sort (map rank hand)) + r-generated (sequential-ranks r) + r-ace-low (sort (replace {14 1} r)) + r-ace-low-generated (sequential-ranks r-ace-low)] + (or (= r r-generated) + (= r-ace-low r-ace-low-generated)))) (defn straight-flush? [hand] - nil) + (and (straight? hand) (flush? hand))) (defn value [hand] - nil) + (cond + (straight-flush? hand) 8 + (four-of-a-kind? hand) 7 + (full-house? hand) 6 + (flush? hand) 5 + (straight? hand) 4 + (three-of-a-kind? hand) 3 + (two-pairs? hand) 2 + (pair? hand) 1 + :else 0))