Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (def ranks {"T" 10 "J" 11 "Q" 12 "K" 13 "A" 14})
- (def suits {"C" "Clubs" "H" "Hearts" "S" "Spades" "D" "Diamonds"})
- (defn suit "Get the suit of the card" [card]
- (suits (str (first (rest card))))
- )
- (defn rank "Get the rank of the card" [card]
- (if (Character/isDigit (first (char-array card)))
- (- (int (first (char-array card))) 48)
- (ranks (str (first (char-array card))))
- )
- )
- (defn pair? [hand]
- (if (and (= 2 (reduce max (vals (frequencies (map rank hand))))) (not= (first (vals (frequencies (map rank hand)))) (second (vals (frequencies (map rank hand))))))
- true
- false
- ))
- (defn three-of-a-kind? [hand]
- (if (and (= 3 (reduce max (vals (frequencies (map rank hand))))) (= 1 (second (vals (frequencies (map rank hand))))))
- true
- false
- )
- )
- (defn four-of-a-kind? [hand]
- (if (= 4 (reduce max (vals (frequencies (map rank hand)))))
- true
- false
- )
- )
- (defn what-flush? "returns 1 if its only a flush and 0 if its a straight flush, 2 otherwise" [hand]
- (let [hand-suits (map suit hand)
- hand-ranks (sort (map rank hand))]
- (if (= 5 (reduce max (vals (frequencies hand-suits))))
- (if (straight? hand)
- 0
- 1
- )
- 2
- )
- )
- )
- (defn flush? [hand]
- (if (= 1 (what-flush? hand))
- true
- false
- )
- )
- (defn straight-flush? [hand]
- (if (= 0 (what-flush? hand))
- true
- false
- )
- )
- (defn full-house? [hand]
- (if (and (= 3 (reduce max (vals (frequencies (map rank hand)))))
- (= 2 (reduce min (vals (frequencies (map rank hand))))))
- true
- false
- )
- )
- (defn two-pairs? [hand]
- (let [freq (vals (frequencies (map rank hand)))]
- (if (= (first freq) (first (rest freq)) 2)
- true
- false
- )
- ))
- (defn straight? [hand]
- (let [hand-ranks (sort (map rank hand))]
- (if (or (every? #{1} (map - (rest hand-ranks) hand-ranks))
- (= '(2 3 4 5 14) hand-ranks))
- true
- false
- )
- ))
- (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
- )
- )
- (def high-seven ["2H" "3S" "4C" "5C" "7D"])
- (def pair-hand ["2H" "2S" "4C" "5C" "7D"])
- (def two-pairs-hand ["2H" "2S" "4C" "4D" "7D"])
- (def three-of-a-kind-hand ["6H" "6S" "6C" "4D" "7D"])
- (def four-of-a-kind-hand ["2H" "2S" "2C" "2D" "7D"])
- (def straight-hand ["2H" "3S" "6C" "5D" "4D"])
- (def low-ace-straight-hand ["2H" "3S" "4C" "5D" "AD"])
- (def high-ace-straight-hand ["TH" "AS" "QC" "KD" "JD"])
- (def flush-hand ["2H" "4H" "5H" "9H" "7H"])
- (def full-house-hand ["2H" "5D" "2D" "2C" "5S"])
- (def straight-flush-hand ["2H" "3H" "6H" "5H" "4H"])
- (def low-ace-straight-flush-hand ["2D" "3D" "4D" "5D" "AD"])
- (def high-ace-straight-flush-hand ["TS" "AS" "QS" "KS" "JS"])
- (def hands [["2H" "3S" "4C" "5C" "7D"] ["2H" "2S" "4C" "5C" "7D"] ["2H" "2S" "4C" "4D" "7D"] ["2H" "2S" "2C" "4D" "7D"]
- ["2H" "2S" "2C" "2D" "7D"] ["2H" "3S" "6C" "5D" "4D"] ["2H" "3S" "4C" "5D" "AD"] ["TH" "AS" "QC" "KD" "JD"]
- ["2H" "4H" "5H" "9H" "7H"] ["2H" "5D" "2D" "2C" "5S"] ["2H" "3H" "6H" "5H" "4H"] ["2D" "3D" "4D" "5D" "AD"]
- ["TS" "AS" "QS" "KS" "JS"]] )
- (defn sort-hand [hand]
- (reverse (sort (map (fn [x] [(second x) (first x)]) (frequencies (map rank hand)))))
- )
- (defn kickers [hand]
- (map second (sort-hand hand))
- )
- (defn higher-kicker? [kicker1 kicker2]
- (cond
- (empty? kicker1) false
- (> (first kicker1) (first kicker2)) true
- (< (first kicker1) (first kicker2)) false
- :else (higher-kicker? (rest kicker1) (rest kicker2))
- )
- )
- (defn beats? [hand1 hand2]
- (cond
- (> (value hand1) (value hand2)) true
- (< (value hand1) (value hand2)) nil
- (= true (higher-kicker? (kickers hand1) (kickers hand2))) true
- :else nil
- )
- )
- (defn get-higher-kicker [k1 k2]
- (cond
- (higher-kicker? k1 k2) k1
- (higher-kicker? k2 k1) k2
- :else k1
- ))
- (get-higher-kicker (kickers high-seven) (kickers flush-hand))
- (defn highest-kicker [v]
- (cond
- (empty? v) nil
- (= 1 (count v)) (first v)
- :else (reduce get-higher-kicker v)
- )
- )
- (defn winning-hand
- ([] nil)
- ([hand] hand)
- ([hand & more]
- (let [all-hands (cons hand more)
- max-value (reduce max (map value all-hands))
- filter-list (filter (fn [x] (= max-value (value x))) all-hands)
- kicker (highest-kicker (map kickers filter-list))
- to-return (filter (fn[x] (= kicker (kickers x))) filter-list)
- ]
- (cond
- (= 1 (count to-return)) (first to-return)
- :else to-return
- )
- )
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement