Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. type Rank = | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Knight | Queen | King | Ace
  2. type Suit = Hearts | Spades | Diamonds | Clubs
  3. type Card = Card of (Rank * Suit)
  4. let ranks = [Two ; Three ; Four ; Five ; Six ; Seven ; Eight ; Nine ; Ten ; Knight ; Queen ; King ; Ace]
  5. let suits = [Hearts; Spades; Diamonds; Clubs]
  6.  
  7. let getValue r =
  8. match r with
  9. | Two -> 2
  10. | Three -> 3
  11. | Four -> 4
  12. | Five -> 5
  13. | Six -> 6
  14. | Seven -> 7
  15. | Eight -> 8
  16. | Nine -> 9
  17. | Ten | Knight | Queen | King -> 10
  18. | Ace -> 11
  19.  
  20. //Creates the deck of 52 cards. Like a customized zip function.
  21. let zipCards suits ranks =
  22. let rec go l1 l2 result =
  23. match (l1, l2) with
  24. | l1 :: l1s, l2 :: l2s -> go (l1 :: l1s) l2s (Card (l2,l1) :: result)
  25. | l1 :: l1s, [] -> go l1s ranks result
  26. | _,_ -> result
  27. go suits ranks []
  28.  
  29. //This is the full deck of cards
  30. let fullDeck = (zipCards suits ranks)
  31.  
  32. //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
  33. let countHand hand =
  34. let countCards = hand |> List.fold (fun acc (r,_) -> acc + (getValue r)) 0
  35. let containsAce = List.map (fun (r,_) -> r = Ace) hand |> List.contains true
  36. if countCards > 21 && containsAce then countCards - 10 else countCards
  37.  
  38. //Take a random card from the list. Return the card, and the list without the card.
  39. let takeRandomCard (cards: Card list) =
  40. let random = System.Random()
  41. let card = List.item (random.Next(cards.Length)) cards
  42. let newCards = List.except [card] cards
  43. (newCards, card)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement