-
Notifications
You must be signed in to change notification settings - Fork 1
/
deckofcards.go
56 lines (46 loc) · 883 Bytes
/
deckofcards.go
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
package main
import (
"fmt"
"math/rand"
)
type Card struct {
Type string
Suit string
}
type Deck []Card
//create a new deck
func New() (deck Deck) {
types := []string{"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"}
suits := []string{"Heart", "Spade", "Diamond", "Club"}
for i := 0; i < len(types); i++ {
for j := 0; j < len(suits); j++ {
card := Card{
Type: types[i],
Suit: suits[j],
}
deck = append(deck, card)
}
}
return
}
//shuffle the deck
func Shuffle(deck Deck) Deck {
for i := 0; i < len(deck); i++ {
r := rand.Intn(i + 1)
if i != r {
deck[i], deck[r] = deck[r], deck[i]
}
}
return deck
}
//display deck contents
func Show(deck Deck) Deck {
for i := 0; i < len(deck); i++ {
fmt.Println(deck[i])
}
return deck
}
func main() {
deck := New()
Show(Shuffle(deck))
}