From 5db2f2767b70efaae4a1b0d7cdf71c9fad100fdf Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 16:58:57 +0300 Subject: [PATCH 01/11] Implement suit --- src/p_p_p_pokerface.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 5ea80094..f095d888 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -4,7 +4,8 @@ nil) (defn suit [card] - nil) + (let [[_ second-char] card] + (str second-char))) (defn pair? [hand] nil) From bd4fc28fcc5631df98ad0b3ad85b0f092b0ccfce Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 17:21:02 +0300 Subject: [PATCH 02/11] Implement rank --- src/p_p_p_pokerface.clj | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index f095d888..2588ec7a 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -1,7 +1,13 @@ (ns p-p-p-pokerface) +(def replacements {\T 10 \J 11 \Q 12 \K 13 \A 14}) + + (defn rank [card] - nil) + (let [[rank-char _] card] + (if (Character/isDigit rank-char) + (Integer/valueOf (str rank-char)) + (replacements rank-char)))) (defn suit [card] (let [[_ second-char] card] From 0e64ea3c88e5ad2401f3242adfdb533a32640885 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 18:01:11 +0300 Subject: [PATCH 03/11] Implement pair? --- src/p_p_p_pokerface.clj | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 2588ec7a..aaaa2b1f 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -13,8 +13,11 @@ (let [[_ second-char] card] (str second-char))) +(defn hand->rank-frequencies [hand] + (frequencies (map rank hand))) + (defn pair? [hand] - nil) + (= 2 (apply max (vals (hand->rank-frequencies hand))))) (defn three-of-a-kind? [hand] nil) From efbb86cbbe4851a9e1cc76d3171c8f6a59aad3be Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 18:03:17 +0300 Subject: [PATCH 04/11] Implement three-of-a-kind? and four-of-a-kind? --- src/p_p_p_pokerface.clj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index aaaa2b1f..55c89375 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -16,14 +16,15 @@ (defn hand->rank-frequencies [hand] (frequencies (map rank hand))) + (defn pair? [hand] (= 2 (apply max (vals (hand->rank-frequencies hand))))) (defn three-of-a-kind? [hand] - nil) + (= 3 (apply max (vals (hand->rank-frequencies hand))))) (defn four-of-a-kind? [hand] - nil) + (= 4 (apply max (vals (hand->rank-frequencies hand))))) (defn flush? [hand] nil) From dca58becd612e70d8aa1687be77e8c082a741ad8 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 18:05:53 +0300 Subject: [PATCH 05/11] Implement flush? --- src/p_p_p_pokerface.clj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 55c89375..550ab207 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -16,6 +16,8 @@ (defn hand->rank-frequencies [hand] (frequencies (map rank hand))) +(defn hand->suit-frequencies [hand] + (frequencies (map suit hand))) (defn pair? [hand] (= 2 (apply max (vals (hand->rank-frequencies hand))))) @@ -27,7 +29,7 @@ (= 4 (apply max (vals (hand->rank-frequencies hand))))) (defn flush? [hand] - nil) + (= 5 (apply max (vals (hand->suit-frequencies hand))))) (defn full-house? [hand] nil) From 982aa53f0749e3f8c7df2b2b44dc77da45458c40 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 18:50:59 +0300 Subject: [PATCH 06/11] Implement full-house? --- src/p_p_p_pokerface.clj | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 550ab207..3d30588c 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -20,19 +20,20 @@ (frequencies (map suit hand))) (defn pair? [hand] - (= 2 (apply max (vals (hand->rank-frequencies hand))))) + (< 1 (apply max (vals (hand->rank-frequencies hand))))) (defn three-of-a-kind? [hand] - (= 3 (apply max (vals (hand->rank-frequencies hand))))) + (< 2 (apply max (vals (hand->rank-frequencies hand))))) (defn four-of-a-kind? [hand] - (= 4 (apply max (vals (hand->rank-frequencies hand))))) + (< 3 (apply max (vals (hand->rank-frequencies hand))))) (defn flush? [hand] (= 5 (apply max (vals (hand->suit-frequencies hand))))) (defn full-house? [hand] - nil) + (let [rank-freqs (vals (hand->rank-frequencies hand))] + (or (= [2 3] rank-freqs) (= [3 2] rank-freqs)))) (defn two-pairs? [hand] nil) From ffcf619bbba58e5631f76bc107acf428aabcbbc1 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 19:28:49 +0300 Subject: [PATCH 07/11] Implement two-pairs? --- src/p_p_p_pokerface.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 3d30588c..8c4506ee 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -36,7 +36,8 @@ (or (= [2 3] rank-freqs) (= [3 2] rank-freqs)))) (defn two-pairs? [hand] - nil) + (let [descending-rank-freqs (sort > (vals (hand->rank-frequencies hand)))] + (or (= [2 2] (take 2 descending-rank-freqs)) (= [4] (take 1 descending-rank-freqs))))) (defn straight? [hand] nil) From ea1be0e118b100683ccc26af58ad3a1775fc6017 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 20:18:07 +0300 Subject: [PATCH 08/11] Implement straight? --- src/p_p_p_pokerface.clj | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index 8c4506ee..c3c9ef75 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -39,8 +39,17 @@ (let [descending-rank-freqs (sort > (vals (hand->rank-frequencies hand)))] (or (= [2 2] (take 2 descending-rank-freqs)) (= [4] (take 1 descending-rank-freqs))))) +(defn straight-impl? [ranks] + (let [sorted-ranks (sort ranks)] + (let [lowest_rank (nth sorted-ranks 0)] + (= sorted-ranks (range lowest_rank (+ lowest_rank 5)))))) + (defn straight? [hand] - nil) + (let [ranks-high-ace (map rank hand)] + (let [ranks-low-ace (replace {14 1} ranks-high-ace)] + (or (straight-impl? ranks-high-ace) (straight-impl? ranks-low-ace))))) + + (defn straight-flush? [hand] nil) From 88f8ab438e6d0bc97fa23f13a150c55124c80354 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 20:19:58 +0300 Subject: [PATCH 09/11] Implement straight-flush? --- src/p_p_p_pokerface.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index c3c9ef75..dd4a45cb 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -52,7 +52,7 @@ (defn straight-flush? [hand] - nil) + (and (flush? hand) (straight? hand))) (defn value [hand] nil) From 07050a52ac94a48be6c1c8aba1b85bd26c83b724 Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 21:11:41 +0300 Subject: [PATCH 10/11] Implement value. All done. --- src/p_p_p_pokerface.clj | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/p_p_p_pokerface.clj b/src/p_p_p_pokerface.clj index dd4a45cb..b05b029f 100644 --- a/src/p_p_p_pokerface.clj +++ b/src/p_p_p_pokerface.clj @@ -54,5 +54,19 @@ (defn straight-flush? [hand] (and (flush? hand) (straight? hand))) + +(defn high-card? [hand] + true) ; All hands have a high card. + + (defn value [hand] - nil) + (let [checkers #{[high-card? 0] [pair? 1] + [two-pairs? 2] [three-of-a-kind? 3] + [straight? 4] [flush? 5] + [full-house? 6] [four-of-a-kind? 7] + [straight-flush? 8]}] + (let [evaluate-checkers (map (fn [x] x) checkers)] ;;run anonym fn for each checker, anon fn is formed in the next row + (let [matching-checkers (filter (fn [x] ((first x) hand)) evaluate-checkers)] ;;filters matching checkers, (first x) points to the first part of the checker macro match pair i.e. 'high-card' + (let [values-from-matching-checkers (map second matching-checkers)] + (apply max values-from-matching-checkers)))))) + From 986bf233ad00ed4f2a60200e55f9ffd4a1b6706b Mon Sep 17 00:00:00 2001 From: rkelkka Date: Sun, 14 Oct 2018 21:12:30 +0300 Subject: [PATCH 11/11] Fix travis build script lein cmd --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 455f3c0e..45c29f60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: clojure -lein: lein2 -script: lein2 midje :config .midje-grading-config.clj +lein: lein +script: lein midje :config .midje-grading-config.clj jdk: - openjdk7 notifications: