Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve poker exercises #1034

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
49 changes: 38 additions & 11 deletions src/p_p_p_pokerface.clj
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
(ns p-p-p-pokerface)

(defn rank [card]
nil)
(let [rnk (get card 0)]
(if (Character/isDigit rnk)
(Integer/valueOf (str rnk))
(get {\T 10 \J 11 \Q 12 \K 13 \A 14} rnk))))

(defn suit [card]
nil)
(str (get card 1)))

(defn pair? [hand]
nil)
(let [freqs (vals (frequencies (map rank hand)))]
(not (empty? (filter #(= 2 %) freqs)))))

(defn three-of-a-kind? [hand]
nil)
(let [freqs (vals (frequencies (map rank hand)))]
(not (empty? (filter #(= 3 %) freqs)))))

(defn four-of-a-kind? [hand]
nil)
(let [freqs (vals (frequencies (map rank hand)))]
(not (empty? (filter #(= 4 %) freqs)))))

(defn flush? [hand]
nil)
(let [freqs (vals (frequencies (map suit hand)))]
(not (empty? (filter #(= 5 %) freqs)))))

(defn full-house? [hand]
nil)
(and (pair? hand)
(three-of-a-kind? hand)))

(defn two-pairs? [hand]
nil)
(let [freqs (vals (frequencies (map rank hand)))]
(or (= (count (filter #(= 2 %) freqs)) 2)
(= (count (filter #(= 4 %) freqs)) 1))))

(defn straight? [hand]
nil)
(let [ranks (map rank hand)
sorted-ranks (sort ranks)
rank-range (range (apply min sorted-ranks)
(inc (apply max sorted-ranks)))]
(if (and (= (take 4 sorted-ranks) (range 2 6))
(= (get (vec sorted-ranks) 4) 14))
true
(= sorted-ranks rank-range))))

(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))