forked from starlightromero/go-cards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.go
102 lines (86 loc) · 1.57 KB
/
deck.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
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
package main
import (
"fmt"
"math/rand"
"time"
)
type card struct {
symbol string
suit string
value int
}
type deck []card
func newDeck() deck {
cards := deck{}
cardSuits := []string{"♠", "♥", "♦", "♣"}
cardSymbolValues := map[string]int{
"A": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"10": 10,
"J": 10,
"Q": 10,
"K": 10,
}
for _, suit := range cardSuits {
for symbol, value := range cardSymbolValues {
c := card{symbol, suit, value}
cards = append(cards, c)
}
}
return cards
}
func (d deck) print() {
for _, c := range d {
fmt.Println(c.symbol, c.suit)
}
}
func (d *deck) dealOne() deck {
*d = (*d)[1:]
return (*d)[:1]
}
func (d *deck) dealTwo() deck {
*d = (*d)[2:]
return (*d)[:2]
}
func (d *deck) deal(handSize int) deck {
*d = (*d)[handSize:]
return (*d)[:handSize]
}
func (d deck) shuffle() {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
for i := range d {
j := r.Intn(len(d) - 1)
d[i], d[j] = d[j], d[i]
}
}
func (d deck) value() int {
v := 0
for _, c := range d {
v += c.value
}
return v
}
// func (d deck) toString() string {
// c := []card(d)
// return strings.Join([]string(c), ",")
// }
// func (d deck) saveToFile(filename string) error {
// return ioutil.WriteFile(filename, []byte(d.toString()), 0666)
// }
// func newDeckFromFile(filename string) deck {
// bs, err := ioutil.ReadFile(filename)
// if err != nil {
// fmt.Println("ERROR:", err)
// os.Exit(1)
// }
// s := strings.Split(string(bs), ",")
// return deck(s)
// }