Clojure 中的交易 x 手牌数量 - vs 分区

Bud*_*Joe 1 algorithm clojure

在 Clojure 中向一定数量的玩家发牌的最惯用和最有效的方式是什么?Clojure 的分区实际上并没有模拟真实世界的物理纸牌交易。

注意:我希望能够与任意数量的玩家交易。如果纸牌被完全不平等地处理也没关系。我想非常通用地处理低于 52 的卡......或混合的多副牌。

可用卡和空函数签名的示例数据结构:

(def sample-cards 
    [[:5 :Heart]
    [:8 :Spade]
    [:7 :Club]
    [:9 :Diamond]
    [:J :Spade]
    [:Q :Heart]
    [:5 :Spade]
    [:8 :Club]
    [:6 :Diamond]])


(defn deal-cards [players deck]
    ; some logic/looping 
    ; then return give the args 4 and sample-cards
    [[[:5 :Heart] [:J :Spade] [:6 :Diamond]]
    [[:8 :Spade] [:Q :Heart]]
    [[:7 :Club]  [:5 :Spade]]
    [[:9 :Diamond] [:8 :Club]]])
Run Code Online (Sandbox Code Playgroud)

Rul*_*lle 6

这是一个使用惰性序列的相当紧凑的实现:

(defn deal-cards [n deck]
  (map (partial take-nth n)
       (take n (iterate rest deck))))
Run Code Online (Sandbox Code Playgroud)

一个例子:

(deal-cards 4 sample-cards)
;; => (([:5 :Heart] [:J :Spade] [:6 :Diamond]) ([:8 :Spade] [:Q :Heart]) ([:7 :Club] [:5 :Spade]) ([:9 :Diamond] [:8 :Club]))
Run Code Online (Sandbox Code Playgroud)