-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.go
62 lines (53 loc) · 1.21 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
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)
type deck []string
func newDeck() deck {
cards := deck{}
cardSuits := [4]string{"heart", "spade", "club", "tile"}
cardValues := [3]string{"Ace", "Two", "Three"}
for _, suit := range cardSuits {
for _, value := range cardValues {
cards = append(cards, value+" of "+suit)
}
}
return cards
}
func (d deck) print() {
fmt.Println("\n-----------------------")
for i, card := range d {
fmt.Println(i, card)
}
fmt.Println("-----------------------\n")
}
func deal(d deck, handSize int) (deck, deck) {
return d[:handSize], d[handSize:]
}
func (d deck) toString() string {
return strings.Join([]string(d), ",")
}
func (d deck) saveDeckToFile(filename string) error {
return ioutil.WriteFile(filename, []byte(d.toString()), 0666)
}
func readDeckFromFile(filename string) deck {
b, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Error ocurred while reading file : ", err)
os.Exit(1)
}
return deck(strings.Split(string(b), ","))
}
func (d deck) shuffle() {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
for i := range d {
newPosition := r.Intn(len(d))
d[i], d[newPosition] = d[newPosition], d[i]
}
}