49 lines
966 B
Go
49 lines
966 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strings"
|
|
)
|
|
|
|
type deck []string
|
|
|
|
func (d deck) print() {
|
|
for i, card := range d {
|
|
fmt.Println(i, card)
|
|
}
|
|
}
|
|
|
|
func newDeck() deck {
|
|
cards := deck{}
|
|
cardSuits := []string{"Spades", "Diamonds", "Hearts", "Clubs"}
|
|
cardValues := []string{"Ace", "Two", "Three", "Four"}
|
|
for _, suite := range cardSuits {
|
|
for _, value := range cardValues {
|
|
cards = append(cards, value+" of "+suite)
|
|
}
|
|
}
|
|
return cards
|
|
}
|
|
|
|
func deal(d deck, handsize int) (deck, deck) {
|
|
return d[:handsize], d[handsize:]
|
|
}
|
|
|
|
func (d deck) toString(seperator string) string {
|
|
return strings.Join(d, "\n")
|
|
}
|
|
|
|
func fromString(input []byte, seperator string) deck {
|
|
return strings.Split(string(input), seperator)
|
|
}
|
|
|
|
func (d deck) save(filename string) error {
|
|
return ioutil.WriteFile(filename, []byte(d.toString("\n")), 0644)
|
|
}
|
|
|
|
func load(filename string) (deck, error) {
|
|
bytes, err := ioutil.ReadFile(filename)
|
|
return fromString(bytes, "\n"), err
|
|
}
|