Advertisement
Martin_Toseski

Zadaca 2

Feb 25th, 2024
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (def card-value (hash-map \T "10" \J "11" \Q "12" \K "13" \A "14"))
  2.  
  3. (defn suit [card]
  4.     (first (rest card))  
  5. )
  6.  
  7. (defn rank [card]
  8.     (cond
  9.         (Character/isDigit (first card)) (Integer/valueOf (str (first card)))
  10.         :else (Integer/valueOf (get card-value (first card)))
  11.     )
  12. )
  13.  
  14. (defn get-ranks [hand]
  15.     (map rank hand)
  16. )
  17.  
  18. (defn pair? [hand]
  19.     (or (some #(= 2 %) (vals (frequencies (get-ranks hand)))) false)
  20. )
  21.  
  22. (defn three-of-a-kind? [hand]
  23.     (or (some #(= 3 %) (vals (frequencies (get-ranks hand)))) false)
  24. )
  25.  
  26. (defn four-of-a-kind? [hand]
  27.     (or (some #(= 4 %) (vals (frequencies (get-ranks hand)))) false)
  28. )
  29.  
  30. (defn flush? [hand]
  31.     (every? #(= (suit (first hand)) (suit %)) hand)
  32. )
  33.  
  34. (defn full-house? [hand]
  35.     (and (pair? hand)
  36.          (three-of-a-kind? hand))
  37. )
  38.  
  39. (defn two-pairs? [hand]
  40.     (= 2 (count (filter #(= 2 %) (vals (frequencies (get-ranks hand))))))
  41. )
  42.  
  43. (defn straight? [hand]
  44.     (let
  45.         [hand-ranks (get-ranks hand)
  46.          sorted-hand (sort hand-ranks)
  47.          ace-hand  (map #(if (= % 14) 1 %) hand-ranks)
  48.          sorted-hand1 (sort ace-hand)]
  49.         (or (= (range (first sorted-hand) (+ (last sorted-hand) 1)) sorted-hand) (= (range (first sorted-hand1) (+ (last sorted-hand1) 1)) sorted-hand1))
  50.     )
  51. )
  52.  
  53. (defn straight-flush? [hand]
  54.     (and (flush? hand)
  55.          (straight? hand))    
  56. )
  57.  
  58. (defn value [hand]
  59.     (cond
  60.         (straight-flush? hand) 8
  61.         (four-of-a-kind? hand) 7
  62.         (full-house? hand) 6
  63.         (flush? hand) 5
  64.         (straight? hand) 4
  65.         (three-of-a-kind? hand) 3
  66.         (two-pairs? hand) 2
  67.         (pair? hand) 1
  68.         :else 0
  69.     )
  70. )
  71.  
  72. (defn get-pairs [hand]
  73.     (let [hand-ranks (get-ranks hand)
  74.           handFreq (frequencies hand-ranks)
  75.           hand-sorted (reverse (sort-by val handFreq))]    
  76.         hand-sorted
  77.     )    
  78. )
  79.  
  80. (defn kickers1 [hand]
  81.     (cond
  82.         (empty? hand) ()
  83.         (= (second (first hand)) (second (last hand))) (reverse (sort (map first hand)))
  84.         :else (cons (first (first hand)) (kickers1 (rest hand)))
  85.     )
  86. )
  87.  
  88. (defn kickers [hand]
  89.     (kickers1 (get-pairs hand))
  90. )
  91.  
  92. (defn higher-kicker1? [kicker1 kicker2]
  93.     (cond
  94.         (empty? kicker1) false
  95.         (= (first kicker1) (first kicker2)) (higher-kicker1? (rest kicker1) (rest kicker2))
  96.         :else (> (first kicker1) (first kicker2))
  97.     )  
  98. )
  99.  
  100. (defn higher-kicker? [kicker1 kicker2]
  101.     (higher-kicker1? (kickers kicker1) (kickers kicker2))
  102. )
  103.  
  104. (defn beats? [hand1 hand2]
  105.     (cond
  106.         (> (value hand1) (value hand2)) true
  107.         (and
  108.             (= (value hand1) (value hand2))
  109.             (higher-kicker? hand1 hand2)) true
  110.         :else nil        
  111.     )    
  112. )
  113.  
  114. (defn winning-hand [& hands]
  115.     (let [hand-values (map value hands)
  116.           max-value (apply max hand-values)]
  117.         (cond
  118.             (empty? hands) nil
  119.             :else (filter #(= (value %) max-value) hands)
  120.         )
  121.     )
  122. )
  123.  
  124. (println (straight? ["TS" "JS" "QS" "KS" "AS"]))
  125. (println (straight? ["TS" "JS" "QS" "KS" "9S"]))
  126.  
  127. (println (winning-hand
  128.   ["2S" "4S" "7C" "4C" "7D"]
  129.   ["2S" "5S" "7C" "5C" "7D"]
  130.   ["3H" "4H" "5H" "6H" "7H"]
  131.   ["8D" "8H" "8C" "8S" "9D"]
  132.   ["2D" "2H" "2C" "5D" "5S"]
  133.   ["TS" "JS" "QS" "KS" "AS"]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement