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

All excercises done #1043

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target/
/bin/
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)

(def rank-table {\2 2, \3 3, \4 4, \5 5, \6 6, \7 7, \8 8, \9 9, \T 10, \J 11, \Q 12, \K 13, \A 14})

(defn rank [card]
nil)
(let [[rank suit] card]
(get rank-table rank)))

(defn suit [card]
nil)
(let [[rank suit] card]
(str suit)))

(defn pair? [hand]
nil)
(= 2 (apply max (vals (frequencies (map rank hand))))))

(defn three-of-a-kind? [hand]
nil)
(= 3 (apply max (vals (frequencies (map rank hand))))))

(defn four-of-a-kind? [hand]
nil)
(= 4 (apply max (vals (frequencies (map rank hand))))))

(defn flush? [hand]
nil)
(= 5 (apply max (vals (frequencies (map suit hand))))))

(defn full-house? [hand]
nil)
(let [rank-count (reverse (sort (vals (frequencies (map rank hand)))))]
(and (= 3 (first rank-count))
(= 2 (second rank-count)))))

(defn two-pairs? [hand]
nil)
(let [rank-count (reverse (sort (vals (frequencies (map rank hand)))))]
(or
(and (= 2 (first rank-count))
(= 2 (second rank-count)))
(= 4 (first rank-count)))))

(defn straight? [hand]
nil)
(let [ranks (sort (map rank hand))
ranks-aces-low (sort (replace {14 1} ranks))
start-rank (apply min ranks)
comp-straight (range start-rank (+ start-rank 5))
comp-straight-low (range 1 6)]
(or (= ranks comp-straight)
(= ranks-aces-low comp-straight-low))))

(defn straight-flush? [hand]
nil)
(and (straight? hand)
(flush? hand)))

(defn high-card? [hand]
true)

(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]}
hands (map (fn [x] (if ((first x) hand) (second x) nil)) checkers)
values (filter (fn [x] (not (nil? x))) hands)]
(apply max values)))