Created
November 17, 2011 18:14
-
-
Save poetix/1373966 to your computer and use it in GitHub Desktop.
Checking for various poker hand combinations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 (ns poker.core | |
2 (:use clojure.contrib.string)) | |
3 | |
4 (defrecord Card [value suit]) | |
5 | |
6 (def VALUE { | |
7 "1" :one | |
8 "2" :two | |
9 "3" :three | |
10 "4" :four | |
11 "5" :five | |
12 "6" :six | |
13 "7" :seven | |
14 "8" :eight | |
15 "9" :nine | |
16 "10" :ten | |
17 "J" :jack | |
18 "Q" :queen | |
19 "K" :king | |
20 "A" :ace}) | |
21 | |
22 (def SUIT { | |
23 \d :diamonds | |
24 \s :spades | |
25 \c :clubs | |
26 \h :hearts}) | |
27 | |
28 (def all-values (vals VALUE)) | |
29 (def all-suits (vals SUIT)) | |
30 | |
31 (defn make-card [cardstr] | |
32 (let [suitchar (last cardstr) | |
33 valuestr (take (- (count cardstr) 1) cardstr) | |
34 suit (SUIT suitchar) | |
35 value (VALUE valuestr)] | |
36 (Card. value suit))) | |
37 | |
38 (defn make-hand [handstr] | |
39 (let [cardstrs (split #"\s" handstr)] | |
40 (map make-card cardstrs))) | |
41 | |
42 (defn find-cards-of-suit [suit hand] | |
43 (filter #(= (:suit %1) suit) hand)) | |
44 | |
45 (defn find-cards-of-value [value hand] | |
46 (filter #(= (:value %1) value) hand)) | |
47 | |
48 (defn has-pair? [hand] | |
49 (let [hand-size (count hand) | |
50 values (set (map #(:value %1) hand)) | |
51 values-size (count values)] | |
52 (> hand-size values-size))) | |
53 | |
54 (defn flush? [hand] | |
55 (let [suits (map #(:suit %1) hand) | |
56 number-of-suits (count (set suits))] | |
57 (= number-of-suits 1))) | |
58 | |
59 (defn trips? [hand] | |
60 (let [value-counts (map #(count (find-cards-of-value %1 hand)) all-values)] | |
61 (and (some #{3} value-counts) | |
62 (not-any? #{2} value-counts)))) | |
63 | |
64 (defn -main [] | |
65 (trips? (make-hand "10d 10h 10c 4h 4d"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment