-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCards.fs
157 lines (143 loc) · 3.22 KB
/
Cards.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
module Cards
type Rank =
| Ace
| King
| Queen
| Jack
| Ten
| Nine
| Eight
| Seven
| Six
| Five
| Four
| Three
| Two
type Suit =
| Hearts
| Spades
| Diamonds
| Clubs
type Card = Rank * Suit
type Hand = Card list
type Deck = Card list
let getValueOfCard card =
match card with
| ( Ace, _) -> 1, Some(11)
| ( King, _) -> 10, None
| ( Queen, _) -> 10, None
| ( Jack, _) -> 10, None
| ( Two, _) -> 2, None
| ( Three, _) -> 3, None
| ( Four, _) -> 4, None
| ( Five, _) -> 5, None
| ( Six, _) -> 6, None
| ( Seven, _) -> 7, None
| ( Eight, _) -> 8, None
| ( Nine, _) -> 9, None
| ( Ten, _) -> 10, None
let getValueOfHand cards =
let mutable a = 0
let mutable b = 0
let mutable numOfAces = 0
cards |> List.iter (fun c ->
let l, h = getValueOfCard c
a <- a + l
match h with
| Some n ->
b <- b + n
numOfAces <- numOfAces + 1
| None -> ())
if b = 0 then
a, None
else
a, Some(b + (a - numOfAces))
let draw deck =
match deck with
| head::tail -> (Some head, tail)
| [] -> (None, [])
let shuffle deck =
let random = System.Random()
deck |> List.sortBy(fun card -> random.Next());
let createStandardDeck =
[(Ace,Hearts);
(Ace,Spades);
(Ace,Diamonds);
(Ace, Clubs);
(King,Hearts);
(King,Spades);
(King,Diamonds);
(King, Clubs);
(Queen,Hearts);
(Queen,Spades);
(Queen,Diamonds);
(Queen, Clubs);
(Jack,Hearts);
(Jack,Spades);
(Jack,Diamonds);
(Jack, Clubs);
(Two,Hearts);
(Two,Spades);
(Two,Diamonds);
(Two, Clubs);
(Three,Hearts);
(Three,Spades);
(Three,Diamonds);
(Three, Clubs);
(Four,Hearts);
(Four,Spades);
(Four,Diamonds);
(Four, Clubs);
(Five,Hearts);
(Five,Spades);
(Five,Diamonds);
(Five, Clubs);
(Six,Hearts);
(Six,Spades);
(Six,Diamonds);
(Six, Clubs);
(Seven,Hearts);
(Seven,Spades);
(Seven,Diamonds);
(Seven, Clubs);
(Eight,Hearts);
(Eight,Spades);
(Eight,Diamonds);
(Eight, Clubs);
(Nine,Hearts);
(Nine,Spades);
(Nine,Diamonds);
(Nine, Clubs);
(Ten,Hearts);
(Ten,Spades);
(Ten,Diamonds);
(Ten, Clubs);]
let getNameOfRank rank =
match rank with
| Ace -> "Ace"
| King -> "King"
| Queen -> "Queen"
| Jack -> "Jack"
| Ten -> "Ten"
| Nine -> "Nine"
| Eight -> "Eight"
| Seven -> "Seven"
| Six -> "Six"
| Five -> "Five"
| Four -> "Four"
| Three -> "Three"
| Two -> "Two"
let getNameOfSuit suit =
match suit with
| Hearts -> "Hearts"
| Spades -> "Spades"
| Diamonds -> "Diamonds"
| Clubs -> "Clubs"
let getNameOfCard (rank,suit) =
let r = getNameOfRank rank
let s = getNameOfSuit suit
r + " of " + s
let printCards cards =
cards |> List.iter (fun card ->
let info = getNameOfCard card
printfn "%s" info)