Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. (def ranks {"T" 10 "J" 11 "Q" 12 "K" 13 "A" 14})
  2.  
  3. (def suits {"C" "Clubs" "H" "Hearts" "S" "Spades" "D" "Diamonds"})
  4.  
  5. (defn suit "Get the suit of the card" [card]
  6. (suits (str (first (rest card))))
  7. )
  8.  
  9. (defn rank "Get the rank of the card" [card]
  10. (if (Character/isDigit (first (char-array card)))
  11. (- (int (first (char-array card))) 48)
  12. (ranks (str (first (char-array card))))
  13. )
  14. )
  15.  
  16. (defn pair? [hand]
  17. (if (and (= 2 (reduce max (vals (frequencies (map rank hand))))) (not= (first (vals (frequencies (map rank hand)))) (second (vals (frequencies (map rank hand))))))
  18. true
  19. false
  20. ))
  21.  
  22. (defn three-of-a-kind? [hand]
  23. (if (and (= 3 (reduce max (vals (frequencies (map rank hand))))) (= 1 (second (vals (frequencies (map rank hand))))))
  24. true
  25. false
  26. )
  27. )
  28.  
  29. (defn four-of-a-kind? [hand]
  30. (if (= 4 (reduce max (vals (frequencies (map rank hand)))))
  31. true
  32. false
  33. )
  34. )
  35.  
  36. (defn what-flush? "returns 1 if its only a flush and 0 if its a straight flush, 2 otherwise" [hand]
  37. (let [hand-suits (map suit hand)
  38. hand-ranks (sort (map rank hand))]
  39. (if (= 5 (reduce max (vals (frequencies hand-suits))))
  40. (if (straight? hand)
  41. 0
  42. 1
  43. )
  44. 2
  45. )
  46. )
  47. )
  48.  
  49. (defn flush? [hand]
  50. (if (= 1 (what-flush? hand))
  51. true
  52. false
  53. )
  54. )
  55.  
  56. (defn straight-flush? [hand]
  57. (if (= 0 (what-flush? hand))
  58. true
  59. false
  60. )
  61. )
  62.  
  63. (defn full-house? [hand]
  64. (if (and (= 3 (reduce max (vals (frequencies (map rank hand)))))
  65. (= 2 (reduce min (vals (frequencies (map rank hand))))))
  66. true
  67. false
  68. )
  69. )
  70.  
  71. (defn two-pairs? [hand]
  72. (let [freq (vals (frequencies (map rank hand)))]
  73. (if (= (first freq) (first (rest freq)) 2)
  74. true
  75. false
  76. )
  77. ))
  78.  
  79. (defn straight? [hand]
  80. (let [hand-ranks (sort (map rank hand))]
  81. (if (or (every? #{1} (map - (rest hand-ranks) hand-ranks))
  82. (= '(2 3 4 5 14) hand-ranks))
  83. true
  84. false
  85. )
  86. ))
  87.  
  88. (defn value [hand]
  89. (cond
  90. (straight-flush? hand) 8
  91. (four-of-a-kind? hand) 7
  92. (full-house? hand) 6
  93. (flush? hand) 5
  94. (straight? hand) 4
  95. (three-of-a-kind? hand) 3
  96. (two-pairs? hand) 2
  97. (pair? hand) 1
  98. :else 0
  99. )
  100. )
  101.  
  102. (def high-seven ["2H" "3S" "4C" "5C" "7D"])
  103. (def pair-hand ["2H" "2S" "4C" "5C" "7D"])
  104. (def two-pairs-hand ["2H" "2S" "4C" "4D" "7D"])
  105. (def three-of-a-kind-hand ["6H" "6S" "6C" "4D" "7D"])
  106. (def four-of-a-kind-hand ["2H" "2S" "2C" "2D" "7D"])
  107. (def straight-hand ["2H" "3S" "6C" "5D" "4D"])
  108. (def low-ace-straight-hand ["2H" "3S" "4C" "5D" "AD"])
  109. (def high-ace-straight-hand ["TH" "AS" "QC" "KD" "JD"])
  110. (def flush-hand ["2H" "4H" "5H" "9H" "7H"])
  111. (def full-house-hand ["2H" "5D" "2D" "2C" "5S"])
  112. (def straight-flush-hand ["2H" "3H" "6H" "5H" "4H"])
  113. (def low-ace-straight-flush-hand ["2D" "3D" "4D" "5D" "AD"])
  114. (def high-ace-straight-flush-hand ["TS" "AS" "QS" "KS" "JS"])
  115.  
  116. (def hands [["2H" "3S" "4C" "5C" "7D"] ["2H" "2S" "4C" "5C" "7D"] ["2H" "2S" "4C" "4D" "7D"] ["2H" "2S" "2C" "4D" "7D"]
  117. ["2H" "2S" "2C" "2D" "7D"] ["2H" "3S" "6C" "5D" "4D"] ["2H" "3S" "4C" "5D" "AD"] ["TH" "AS" "QC" "KD" "JD"]
  118. ["2H" "4H" "5H" "9H" "7H"] ["2H" "5D" "2D" "2C" "5S"] ["2H" "3H" "6H" "5H" "4H"] ["2D" "3D" "4D" "5D" "AD"]
  119. ["TS" "AS" "QS" "KS" "JS"]] )
  120.  
  121. (defn sort-hand [hand]
  122. (reverse (sort (map (fn [x] [(second x) (first x)]) (frequencies (map rank hand)))))
  123. )
  124.  
  125. (defn kickers [hand]
  126. (map second (sort-hand hand))
  127. )
  128.  
  129. (defn higher-kicker? [kicker1 kicker2]
  130. (cond
  131. (empty? kicker1) false
  132. (> (first kicker1) (first kicker2)) true
  133. (< (first kicker1) (first kicker2)) false
  134. :else (higher-kicker? (rest kicker1) (rest kicker2))
  135. )
  136. )
  137.  
  138. (defn beats? [hand1 hand2]
  139. (cond
  140. (> (value hand1) (value hand2)) true
  141. (< (value hand1) (value hand2)) nil
  142. (= true (higher-kicker? (kickers hand1) (kickers hand2))) true
  143. :else nil
  144. )
  145. )
  146.  
  147. (defn get-higher-kicker [k1 k2]
  148. (cond
  149. (higher-kicker? k1 k2) k1
  150. (higher-kicker? k2 k1) k2
  151. :else k1
  152. ))
  153.  
  154. (get-higher-kicker (kickers high-seven) (kickers flush-hand))
  155.  
  156. (defn highest-kicker [v]
  157. (cond
  158. (empty? v) nil
  159. (= 1 (count v)) (first v)
  160. :else (reduce get-higher-kicker v)
  161. )
  162. )
  163.  
  164.  
  165.  
  166. (defn winning-hand
  167. ([] nil)
  168. ([hand] hand)
  169. ([hand & more]
  170. (let [all-hands (cons hand more)
  171. max-value (reduce max (map value all-hands))
  172. filter-list (filter (fn [x] (= max-value (value x))) all-hands)
  173. kicker (highest-kicker (map kickers filter-list))
  174. to-return (filter (fn[x] (= kicker (kickers x))) filter-list)
  175. ]
  176. (cond
  177. (= 1 (count to-return)) (first to-return)
  178. :else to-return
  179. )
  180. )
  181. )
  182. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement