Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (def card-value (hash-map \T "10" \J "11" \Q "12" \K "13" \A "14"))
- (defn suit [card]
- (first (rest card))
- )
- (defn rank [card]
- (cond
- (Character/isDigit (first card)) (Integer/valueOf (str (first card)))
- :else (Integer/valueOf (get card-value (first card)))
- )
- )
- (defn get-ranks [hand]
- (map rank hand)
- )
- (defn pair? [hand]
- (or (some #(= 2 %) (vals (frequencies (get-ranks hand)))) false)
- )
- (defn three-of-a-kind? [hand]
- (or (some #(= 3 %) (vals (frequencies (get-ranks hand)))) false)
- )
- (defn four-of-a-kind? [hand]
- (or (some #(= 4 %) (vals (frequencies (get-ranks hand)))) false)
- )
- (defn flush? [hand]
- (every? #(= (suit (first hand)) (suit %)) hand)
- )
- (defn full-house? [hand]
- (and (pair? hand)
- (three-of-a-kind? hand))
- )
- (defn two-pairs? [hand]
- (= 2 (count (filter #(= 2 %) (vals (frequencies (get-ranks hand))))))
- )
- (defn straight? [hand]
- (let
- [hand-ranks (get-ranks hand)
- sorted-hand (sort hand-ranks)
- ace-hand (map #(if (= % 14) 1 %) hand-ranks)
- sorted-hand1 (sort ace-hand)]
- (or (= (range (first sorted-hand) (+ (last sorted-hand) 1)) sorted-hand) (= (range (first sorted-hand1) (+ (last sorted-hand1) 1)) sorted-hand1))
- )
- )
- (defn straight-flush? [hand]
- (and (flush? hand)
- (straight? hand))
- )
- (defn value [hand]
- (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
- )
- )
- (defn get-pairs [hand]
- (let [hand-ranks (get-ranks hand)
- handFreq (frequencies hand-ranks)
- hand-sorted (reverse (sort-by val handFreq))]
- hand-sorted
- )
- )
- (defn kickers1 [hand]
- (cond
- (empty? hand) ()
- (= (second (first hand)) (second (last hand))) (reverse (sort (map first hand)))
- :else (cons (first (first hand)) (kickers1 (rest hand)))
- )
- )
- (defn kickers [hand]
- (kickers1 (get-pairs hand))
- )
- (defn higher-kicker1? [kicker1 kicker2]
- (cond
- (empty? kicker1) false
- (= (first kicker1) (first kicker2)) (higher-kicker1? (rest kicker1) (rest kicker2))
- :else (> (first kicker1) (first kicker2))
- )
- )
- (defn higher-kicker? [kicker1 kicker2]
- (higher-kicker1? (kickers kicker1) (kickers kicker2))
- )
- (defn beats? [hand1 hand2]
- (cond
- (> (value hand1) (value hand2)) true
- (and
- (= (value hand1) (value hand2))
- (higher-kicker? hand1 hand2)) true
- :else nil
- )
- )
- (defn winning-hand [& hands]
- (let [hand-values (map value hands)
- max-value (apply max hand-values)]
- (cond
- (empty? hands) nil
- :else (filter #(= (value %) max-value) hands)
- )
- )
- )
- (println (straight? ["TS" "JS" "QS" "KS" "AS"]))
- (println (straight? ["TS" "JS" "QS" "KS" "9S"]))
- (println (winning-hand
- ["2S" "4S" "7C" "4C" "7D"]
- ["2S" "5S" "7C" "5C" "7D"]
- ["3H" "4H" "5H" "6H" "7H"]
- ["8D" "8H" "8C" "8S" "9D"]
- ["2D" "2H" "2C" "5D" "5S"]
- ["TS" "JS" "QS" "KS" "AS"]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement