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

Poker done #1042

Open
wants to merge 11 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
60 changes: 49 additions & 11 deletions src/p_p_p_pokerface.clj
Original file line number Diff line number Diff line change
@@ -1,34 +1,72 @@
(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]
nil)
(let [[_ second-char] card]
(str second-char)))

(defn hand->rank-frequencies [hand]
(frequencies (map rank hand)))

(defn hand->suit-frequencies [hand]
(frequencies (map suit hand)))

(defn pair? [hand]
nil)
(< 1 (apply max (vals (hand->rank-frequencies hand)))))

(defn three-of-a-kind? [hand]
nil)
(< 2 (apply max (vals (hand->rank-frequencies hand)))))

(defn four-of-a-kind? [hand]
nil)
(< 3 (apply max (vals (hand->rank-frequencies hand)))))

(defn flush? [hand]
nil)
(= 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)
(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)
(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))))))