Guest User

Untitled

a guest
Oct 16th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns p-p-p-pokerface)
  2.  
  3. (def replacements {\T 10, \J 11, \Q 12, \K 13, \A 14})
  4.  
  5. (defn rank [card]
  6.   (let [[rank suit] card]
  7.     (cond
  8.       (Character/isDigit rank) (Integer/valueOf (str rank))
  9.       :else (get replacements rank))))
  10.  
  11. (defn suit [card]
  12.   (let [[rank suit] card]
  13.     (str suit)))
  14.  
  15. (defn n-same-cards-by [typ cards n]
  16.   (let [values (map typ cards)
  17.         freqs (frequencies values)
  18.         same? (fn [key] (== n (get freqs key)))]
  19.     (filter same? (keys freqs))))
  20.  
  21. (defn n-same-ranks [cards n]
  22.   (n-same-cards-by rank cards n))
  23.  
  24. (defn n-same-suits [cards n]
  25.   (n-same-cards-by suit cards n))
  26.  
  27. (defn pair? [hand]
  28.   (== 1 (count (n-same-ranks hand 2))))
  29.  
  30. (defn three-of-a-kind? [hand]
  31.   (== 1 (count (n-same-ranks hand 3))))
  32.  
  33. (defn four-of-a-kind? [hand]
  34.   (== 1 (count (n-same-ranks hand 4))))
  35.  
  36. (defn flush? [hand]
  37.   (== 1 (count (n-same-suits hand 5))))
  38.  
  39. (defn full-house? [hand]
  40.   (and
  41.     (pair? hand)
  42.     (three-of-a-kind? hand)))
  43.  
  44. (defn two-pairs? [hand]
  45.   (== 2 (count (n-same-ranks hand 2))))
  46.  
  47. (defn straight [eka]
  48.   (range eka (+ eka 5)))
  49.  
  50. (defn straight? [hand]
  51.   (let [sorted (sort (map rank hand))
  52.         eka (first sorted)
  53.         vika (last sorted)
  54.         suora (straight eka)]
  55.     (or
  56.       (= sorted suora)
  57.       (= (straight 1) (sort (replace {14 1} sorted))))))
  58.  
  59. (defn straight-flush? [hand]
  60.   (and
  61.     (flush? hand)
  62.     (straight? hand)))
  63.  
  64. ;; Hand                  Value
  65. ;; ===========================
  66. ;; High card (nothing)     0
  67. ;; Pair                    1
  68. ;; Two pairs               2
  69. ;; Three of a kind         3
  70. ;; Straight                4
  71. ;; Flush                   5
  72. ;; Full house              6
  73. ;; Four of a kind          7
  74. ;; Straight flush          8
  75.  
  76. (defn high-card? [hand]
  77.   true)
  78.  
  79. (defn value [hand]
  80.   (cond
  81.     (straight-flush? hand) 8
  82.     (four-of-a-kind? hand) 7
  83.     (full-house? hand) 6
  84.     (flush? hand) 5
  85.     (straight? hand) 4
  86.     (three-of-a-kind? hand) 3
  87.     (two-pairs? hand) 2
  88.     (pair? hand) 1
  89.     :else 0))
Advertisement
Add Comment
Please, Sign In to add comment