-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.go
65 lines (53 loc) · 1.2 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
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)
// Create a new type of 'deck'
// which is a slice of strings
type deck []string
var cardSuits = []string{"♠", "♥", "♣", "♦"}
var cardValues = []string{
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",
}
func newDeck() deck {
cards := deck{}
for _, suit := range cardSuits {
for _, value := range cardValues {
cards = append(cards, value+suit)
}
}
return cards
}
func (d deck) print() {
for _, card := range d {
fmt.Println(card)
}
}
func deal(d deck, handSize int) (deck, deck) {
hand := d[:handSize]
remainingDeck := d[handSize:]
return hand, remainingDeck
}
func (d deck) toString() string {
return strings.Join([]string(d), ",")
}
func (d deck) saveToFile(filename string) error {
return ioutil.WriteFile("files/"+filename, []byte(d.toString()), 0666)
}
func newDeckFromFile(filename string) deck {
bs, err := ioutil.ReadFile("files/" + filename)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
return deck(strings.Split(string(bs), ","))
}
func (d deck) shuffle() {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(d), func(i, j int) { d[i], d[j] = d[j], d[i] })
}