正如我前面提到过的那样,我正在为扑克手工作,我正处于完成的最后阶段。我只是在胜利手牌中遇到问题,这应该就像从最高手牌列表中返回手牌一样,但如果有牌局,那么它应该返回所有牌局的名单。 所以,这是我的获胜手功能:
(defn hand-rank
"Return a value indicating how high the hand ranks."
[hand]
(let [ranks (card-ranks hand)]
(cond
(and (straight ranks) (u-flush hand)) (-> [] (conj 8) (conj (apply max ranks)))
(kind 4 ranks) (-> [] (conj 7) (conj (kind 4 ranks)) (conj (kind 1 ranks)))
(and (kind 3 ranks) (kind 2 ranks)) (-> [] (conj 6) (conj (kind 3 ranks)) (conj (kind 2 ranks)))
(u-flush hand) (-> [] (conj 5) (conj ranks))
(straight ranks) (conj [4] (apply max ranks))
(kind 3 ranks) (-> [3] (conj (kind 3 ranks)) (conj ranks))
(two-pair ranks) (-> [2] (conj (two-pair ranks)) (conj ranks))
(kind 2 ranks) (-> [1] (conj (kind 2 ranks)) (conj ranks))
:else (-> [0] (conj ranks))
)))
(defn winning-hand
"Return the max hand of the given poker hands."
[hands]
(let [min-count (count (apply min-key count (for [hand hands]
(hand-rank hand))))]
(reduce (fn [x y]
(if (<= 0 (compare (subvec (vec (flatten (hand-rank x))) 0 min-count)
(subvec (vec (flatten (hand-rank y))) 0 min-count)))
x
y)) hands)))
(defn allmax
"Return a list of all items equals to the max of the sequence."
[coll]
(let [maximum (winning-hand coll)]
(for [x coll :when (= maximum x)]
x)))
(defn winning-list
"Return a list of winning hands. poker([hand1, hand2, ...] => [hand, ..."
[hands]
(allmax hands))
获胜列表功能在没有平局的情况下正常工作,但是在平局的情况下,它仍然只返回一只手而不是最高的所有手的列表。例如:在这种情况下[“AC”,“2C”,“3C”,“4C”,“5C”]'[“AH”,“2H”,“3H”,“4H”,“5H”]' [“AD”,“2D”,“3D”,“4D”,“5D”]'[“AC”,“2S”,“3H”,“4D”,“5H”]),它应该返回'[ “AC”,“2C”,“3C”,“4C”,“5C”]'[“AH”,“2H”,“3H”,“4H”,“5H”]'[“AD”,“2D “,”3D“,”4D“,”5D“]。有人能告诉我我做错了吗?
答案 0 :(得分:0)
你正在测试手上的相等性,而不是它的“价值”(IE直接冲洗1到5)。你想要这样的东西。
(defn allmax
"Return a list of all items equals to the max of the sequence."
[coll]
(let [maximum (winning-hand coll)]
(for [x coll :when (= (hand-value maximum) (hand-value x)]
x)))
一个更好的解决方案可能会让你在手中实现一个比较器并在其他地方使用它。