我正在尝试编写蜘蛛纸牌播放器作为学习Clojure的练习。我想知道如何处理这些卡片。
我已经创建了(在stackoverflow的帮助下),来自两个标准套牌的104张牌的混洗序列。每张卡都表示为
(defstruct card :rank :suit :face-up)
Spider的画面将表示如下:
(defstruct tableau :stacks :complete)
其中:stacks是卡片向量的向量,其中4个面朝下,1张面朝下,其中6张面朝下,1张面朝下,1张面朝上,总共54张牌, :complete是完成的ace-king组的(最初)空向量(例如,表示为王心,用于打印目的)。 undealt deck的其余部分应保存在ref
中(def deck (ref seq))
在游戏过程中,画面可能包含,例如:
(struct-map tableau
:stacks [[AH 2C KS ...]
[6D QH JS ...]
...
]
:complete [KC KS])
其中“AH”是一张包含{:rank:ace:suit:hearts:face-up false}等的卡片。
如何编写一个函数来处理堆栈,然后将余数保存在ref?
中答案 0 :(得分:2)
这是我在研究上述答案后想出的解决方案。请注意,我仍在改进它并欢迎改进建议,特别是使用更多惯用的Clojure。另请注意,这些函数在几个单独的文件中定义,并不一定按所示顺序出现(如果这有所不同)。
(def suits [:clubs :diamonds :hearts :spades])
(def suit-names
{:clubs "C" :diamonds "D"
:hearts "H" :spades "S"})
(def ranks
(reduce into (replicate 2
[:ace :two :three :four :five :six :seven :eight :nine :ten :jack :queen :king])))
(def rank-names
{:ace "A" :two "2"
:three "3" :four "4"
:five "5" :six "6"
:seven "7" :eight "8"
:nine "9" :ten "T"
:jack "J" :queen "Q"
:king "K"})
(defn card-name
[card show-face-down]
(let
[rank (rank-names (:rank card))
suit (suit-names (:suit card))
face-down (:face-down card)]
(if
face-down
(if
show-face-down
(.toLowerCase (str rank suit))
"XX")
(str rank suit))))
(defn suit-seq
"Return 4 suits:
if number-of-suits == 1: :clubs :clubs :clubs :clubs
if number-of-suits == 2: :clubs :diamonds :clubs :diamonds
if number-of-suits == 4: :clubs :diamonds :hearts :spades."
[number-of-suits]
(take 4 (cycle (take number-of-suits suits))))
(defstruct card :rank :suit :face-down)
(defn unshuffled-deck
"Create an unshuffled deck containing all cards from the number of suits specified."
[number-of-suits]
(for
[rank ranks suit (suit-seq number-of-suits)]
(struct card rank suit true)))
(defn shuffled-deck
"Create a shuffled deck containing all cards from the number of suits specified."
[number-of-suits]
(shuffle (unshuffled-deck number-of-suits)))
(defn deal-one-stack
"Deals a stack of n cards and returns a vector containing the new stack and the rest of the deck."
[n deck]
(loop
[stack []
current n
rest-deck deck]
(if (<= current 0)
(vector
(vec
(reverse
(conj
(rest stack)
(let
[{rank :rank suit :suit} (first stack)]
(struct card rank suit false)))))
rest-deck)
(recur (conj stack (first rest-deck)) (dec current) (rest rest-deck)))))
(def current-deck (ref (shuffled-deck 4)))
(defn deal-initial-tableau
"Deals the initial tableau and returns it. Sets the @deck to the remainder of the deck after dealing."
[]
(dosync
(loop
[stacks []
current 10
rest-deck @current-deck]
(if (<= current 0)
(let [t (struct tableau (reverse stacks) [])
r rest-deck]
(ref-set current-deck r)
t)
(let
[n (if (<= current 4) 6 5)
[s r] (deal-one-stack n rest-deck)]
(recur (vec (conj stacks s)) (dec current) r))))))
(defstruct tableau :stacks :complete)
(defn pretty-print-tableau
[tableau show-face-down]
(let
[{stacks :stacks complete :complete} tableau]
(apply str
(for
[row (range 0 6)]
(str
(apply str
(for
[stack stacks]
(let
[card (nth stack row nil)]
(str
(if
(nil? card)
" "
(card-name card show-face-down)) " "))))
\newline)))))
答案 1 :(得分:0)
你可以编写一个函数,从给定的序列中取出chunks
个size
个项目的向量,然后从前面删除那些块:
;; note the built-in assumption that s contains enough items;
;; if it doesn't, one chunk less then requested will be produced
(defn take-chunks [chunks size s]
(map vec (partition size (take (* chunks size) s))))
;; as above, no effort is made to handle short sequences in some special way;
;; for a short input sequence, an empty output sequence will be returned
(defn drop-chunks [chunks size s]
(drop (* chunks size) s))
然后可以添加一个函数来执行两者(在split-at
和split-with
之后建模):
(defn split-chunks [chunks size s]
[(take-chunks chunks size s)
(drop-chunks chunks size s)])
假设每张卡最初都是{:face-up false}
,您可以使用以下功能打开堆叠中的最后一张卡片:
(defn turn-last-card [stack]
(update-in stack [(dec (count stack)) :face-up] not))
然后是一个函数来处理给定套牌中的初始堆栈/块:
(defn deal-initial-stacks [deck]
(dosync
(let [[short-stacks remaining] (split-chunks 6 5 deck)
[long-stacks remaining] (split-chunks 4 6 remaining)]
[remaining
(vec (map turn-last-card
(concat short-stacks long-stacks)))])))
返回值是一个doubleton向量,其第一个元素是deck的剩余部分,第二个元素是初始堆栈的向量。
然后在交易中使用此功能将Ref考虑在内:
(dosync (let [[new-deck stacks] (deal-initial-stacks @deck-ref)]
(ref-set deck-ref new-deck)
stacks))
更好的是,将游戏的整个状态保持在一个参考或原子中并从ref-set
切换到alter
/ swap!
(我将使用参考这个例子,省略dosync
并将alter
切换为swap!
以使用原子代替):
;; the empty vector is for the stacks
(def game-state-ref (ref [(get-initial-deck) []]))
;; deal-initial-stacks only takes a deck as an argument,
;; but the fn passed to alter will receive a vector of [deck stacks];
;; the (% 0) bit extracts the first item of the vector,
;; that is, the deck; you could instead change the arguments
;; vector of deal-initial-stacks to [[deck _]] and pass the
;; modified deal-initial-stacks to alter without wrapping in a #(...)
(dosync (alter game-state-ref #(deal-initial-stacks (% 0))))
免责声明:这一切都没有得到最轻微的测试关注(虽然我认为它应该可以正常工作,模拟我可能错过的任何愚蠢的错别字)。不过,这是你的练习,所以我认为将测试/抛光部分留给你是好的。 : - )