[英]F# creating a hand with playing cards
I have this code that generates a deck of cards and a function that can randomly choose one of the 52 cards, however.但是,我有这段代码可以生成一副牌和一个 function,它可以随机选择 52 张牌中的一张。 I now need a way to give a play five unique cards that are from the deck and I am not sure how to do that.
我现在需要一种方法来让牌组中的五张独特的牌发挥作用,但我不确定该怎么做。
As I pointed out in the comments, one way to create this hand is to shuffle a deck then take 5 cards from the top.正如我在评论中指出的那样,创建这手牌的一种方法是洗牌,然后从顶部取出 5 张牌。
type Suit = Diamonds | Hearts | Clubs | Spades
type Face =
| Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten
| Jack | Queen | King | Ace
type Card = { Face: Face; Suit : Suit }
let allFaces = [
Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten;
Jack; Queen; King; Ace
]
let allSuits = [ Diamonds; Hearts; Clubs; Spades ]
let fullDeck = [
for suit in allSuits do
for face in allFaces do
yield { Face = face; Suit = suit }
]
// returns a random card and the remaining deck
let GetCard (deck: Card list) =
let rand = new System.Random()
let idx = rand.Next(deck.Length)
let card = deck.[idx]
(card, List.except [card] deck)
// shuffle a deck
let rec shuffle = function
| card :: [] -> [card]
| deck -> let (card, rest) = GetCard deck
card :: shuffle rest
let hand deck = deck |> shuffle |> List.take 5
fullDeck |> hand |> printfn "%A"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.