Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Rank = | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Knight | Queen | King | Ace
- type Suit = Hearts | Spades | Diamonds | Clubs
- type Card = Card of (Rank * Suit)
- let ranks = [Two ; Three ; Four ; Five ; Six ; Seven ; Eight ; Nine ; Ten ; Knight ; Queen ; King ; Ace]
- let suits = [Hearts; Spades; Diamonds; Clubs]
- let getValue r =
- match r with
- | Two -> 2
- | Three -> 3
- | Four -> 4
- | Five -> 5
- | Six -> 6
- | Seven -> 7
- | Eight -> 8
- | Nine -> 9
- | Ten | Knight | Queen | King -> 10
- | Ace -> 11
- //Creates the deck of 52 cards. Like a customized zip function.
- let zipCards suits ranks =
- let rec go l1 l2 result =
- match (l1, l2) with
- | l1 :: l1s, l2 :: l2s -> go (l1 :: l1s) l2s (Card (l2,l1) :: result)
- | l1 :: l1s, [] -> go l1s ranks result
- | _,_ -> result
- go suits ranks []
- //This is the full deck of cards
- let fullDeck = (zipCards suits ranks)
- //Counts the given list of cards. If the value is above 21 and it contains ace, the ace turns into a 1 instead of 11 by subtracting 10 from the end result
- let countHand hand =
- let countCards = hand |> List.fold (fun acc (r,_) -> acc + (getValue r)) 0
- let containsAce = List.map (fun (r,_) -> r = Ace) hand |> List.contains true
- if countCards > 21 && containsAce then countCards - 10 else countCards
- //Take a random card from the list. Return the card, and the list without the card.
- let takeRandomCard (cards: Card list) =
- let random = System.Random()
- let card = List.item (random.Next(cards.Length)) cards
- let newCards = List.except [card] cards
- (newCards, card)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement