clojure - How to return a list in the winning-hands function of poker? -
clojure - How to return a list in the winning-hands function of poker? -
as mentioned before i'm working on poker hands in clojure, i'm on final stages of completion. i'm having problem in winning-hand function, should returns hand list of hands highest, if there ties, should homecoming list of of hands. so, here winning-hand function:
(defn hand-rank "return value indicating how high 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 max hand of 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 list of items equals max of sequence." [coll] (let [maximum (winning-hand coll)] (for [x coll :when (= maximum x)] x))) (defn winning-list "return list of winning hands. poker([hand1, hand2, ...] => [hand, ..." [hands] (allmax hands))
the winning-list function works fine when there no tie, in case of tie, still returning 1 hand , not list of hands highest. e.g.: in case ["ac", "2c", "3c", "4c", "5c"] '["ah", "2h", "3h", "4h", "5h"] '["ad", "2d", "3d", "4d", "5d"] '["ac", "2s", "3h", "4d", "5h"]), should homecoming '["ac", "2c", "3c", "4c", "5c"] '["ah", "2h", "3h", "4h", "5h"] '["ad", "2d", "3d", "4d", "5d"]. tell me i'm doing wrong?
you testing equality on hand itself, not it's "value" (ie straight flush 1 through 5). want this.
(defn allmax "return list of items equals max of sequence." [coll] (let [maximum (winning-hand coll)] (for [x coll :when (= (hand-value maximum) (hand-value x)] x)))
an improve solution have implement comparator hands , utilize here , in other places.
clojure clojurescript
Comments
Post a Comment