-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.go
77 lines (66 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
package main
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
)
type deck []string
func newDeck() deck {
cardSuits := []string{"Spades", "Diamonds", "Heart", "Clubs"}
cardValues := []string{"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}
cards := deck{}
for _, suit := range cardSuits {
for _, value := range cardValues {
cards = append(cards, fmt.Sprintf("%s of %s", value, suit))
}
}
return cards
}
func deal(d deck, handSize int) (deck, deck) {
return d[:handSize], d[handSize:]
}
func (d deck) shuffle() {
rand.Seed(time.Now().UnixNano())
for i := range d {
nextPos := rand.Intn(len(d) - 1)
d[i], d[nextPos] = d[nextPos], d[i]
}
}
func (d deck) print() {
for i, card := range d {
fmt.Println(i, card)
}
}
func (d deck) toString() string {
return strings.Join([]string(d), ",")
}
func (d deck) saveToFile(fileName string) error {
/*
0 - place hodler
6 - users can rw
6 - groups can rw
6 - other can rw
*/
fileNameWithExt := fmt.Sprintf("%v.csv", fileName)
return os.WriteFile(fileNameWithExt, []byte(d.toString()), 0666)
}
func loadDeckFromFile(fileName string) deck {
fileExt := filepath.Ext(fileName)
if fileExt == "" {
var stringBuilder strings.Builder
stringBuilder.WriteString(fileName)
stringBuilder.WriteString(".csv")
fileName = stringBuilder.String()
}
byteSlice, error := os.ReadFile(fileName)
if error != nil {
fmt.Println("Error:", error)
os.Exit(1)
}
csvCards := string(byteSlice)
cards := strings.Split(csvCards, ",")
return deck(cards)
}