补充 evaluate-hand函数
时间: 2023-10-11 19:05:38 浏览: 88
好的,这里是 `evaluate-hand` 函数的实现:
```
(defn evaluate-hand [hand community-cards]
(let [all-cards (concat hand community-cards)
sorted-cards (sort-by :rank all-cards)
flush-suit (->> all-cards
(group-by :suit)
(sort-by #(count (val %)))
last
first
:suit)
straight (some #(apply = (map :rank %))
(partition 5 1 sorted-cards))
straight-flush (and flush-suit straight)]
(cond->> {:hand all-cards}
straight-flush (assoc :rank :straight-flush)
flush-suit (assoc :rank :flush)
straight (assoc :rank :straight)
:else (let [by-rank (group-by :rank all-cards)
counts (map count by-rank)
max-count (apply max counts)]
(cond->> {:rank :high-card}
(= max-count 2) (assoc :rank :pair)
(= max-count 3) (assoc :rank :three-of-a-kind)
(= max-count 4) (assoc :rank :four-of-a-kind)
(and (= max-count 2) (= (count (distinct counts)) 2))
(assoc :rank :two-pair)
:else (let [sorted-counts (reverse (sort counts))
kickers (map first (filter #(= (second %) 1) (partition-all 2 sorted-counts)))]
(cond
(= max-count 1) (assoc :rank :high-card :kickers kickers)
(= max-count 3) (assoc :rank :full-house :kickers kickers)
:else (assoc :rank :three-of-a-kind :kickers kickers)))))))
```
该函数接收两个参数,一个是表示手牌的列表 `hand`,一个是表示公共牌的列表 `community-cards`。在该函数中,首先将手牌和公共牌合并成一副牌,并将其按照牌面大小排序。
然后,通过一系列判断,可以判断出当前牌型的大小,并将其保存在一个字典中,其中包括了牌型和可能的“踢牌”(如果有的话)。最终,这个字典会被返回。
这个函数并不是最优的实现方法,但是足以用于简单的德州扑克游戏。
阅读全文