-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.go
76 lines (58 loc) · 1.32 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
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)
type deck []string
func (d deck) print() {
for i, card := range d {
fmt.Println(i, card)
}
}
func newDeck() deck {
cards := deck{}
cardValues := []string{"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}
cardSuits := []string{"Spades", "Diamonds", "Clubs", "Hearts"}
for _, cv := range cardValues {
for _, cs := range cardSuits {
cards = append(cards, cv+" of "+cs)
}
}
return cards
}
func deal(d deck, handSize int) (deck, deck) {
return d[handSize:], d[:handSize]
}
func (d deck) toString() string {
stringSlice := []string(d)
str := strings.Join(stringSlice[:], ",")
fmt.Println(str)
return str
}
func toByte(d string) []byte {
return []byte(d)
}
func (d deck) saveToFile(filename string) error {
return ioutil.WriteFile("./hanem.txt", toByte(d.toString()), 0644)
}
func (d deck) shuffle() {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
for i := range d {
newPos := r.Intn(len(d) - 1)
d[i], d[newPos] = d[newPos], d[i]
}
}
func newDeckFromFile(filename string) deck {
bs, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Error", err)
os.Exit(1)
}
stringSlice := strings.Split(string(bs), ",")
return deck(stringSlice)
}