From afbc977e1da46a779dc0f24f985ea4b76826fe10 Mon Sep 17 00:00:00 2001 From: Johannes Rothe Date: Tue, 8 Feb 2022 22:17:44 +0100 Subject: [PATCH] testing and assignment --- cards/deck.go | 38 +++++++++++++++++++++++++++----------- cards/deck_test.go | 32 ++++++++++++++++++++++++++++++++ cards/main.go | 8 ++++---- evenodd/go.mod | 3 +++ evenodd/main.go | 17 +++++++++++++++++ 5 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 cards/deck_test.go create mode 100644 evenodd/go.mod create mode 100644 evenodd/main.go diff --git a/cards/deck.go b/cards/deck.go index bc12ddc..767126f 100644 --- a/cards/deck.go +++ b/cards/deck.go @@ -3,7 +3,10 @@ package main import ( "fmt" "io/ioutil" + "math/rand" + "os" "strings" + "time" ) type deck []string @@ -14,6 +17,23 @@ func (d deck) print() { } } +func (d deck) toString(seperator string) string { + return strings.Join(d, "\n") +} + +func (d deck) save(filename string) error { + return ioutil.WriteFile(filename, []byte(d.toString("\n")), 0644) +} + +func (d deck) shuffle() deck { + rand.Seed(time.Now().UnixNano()) + for i, _ := range d { + indexToSwap := rand.Intn(len(d) - 1) + d[i], d[indexToSwap] = d[indexToSwap], d[i] + } + return d +} + func newDeck() deck { cards := deck{} cardSuits := []string{"Spades", "Diamonds", "Hearts", "Clubs"} @@ -30,19 +50,15 @@ 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) + return deck(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) { +func load(filename string) deck { bytes, err := ioutil.ReadFile(filename) - return fromString(bytes, "\n"), err + if err != nil { + fmt.Println("Error: ", err) + os.Exit(1) + } + return fromString(bytes, "\n") } diff --git a/cards/deck_test.go b/cards/deck_test.go new file mode 100644 index 0000000..6e5fa77 --- /dev/null +++ b/cards/deck_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "os" + "testing" +) + +func TestNewDeck(t *testing.T) { + d := newDeck() + if len(d) != 16 { + t.Errorf("Expected 16 but got %d", len(d)) + } + + if d[0] != "Ace of Spades" { + t.Errorf("Expected Ace of spades but got %s", d[0]) + } + + if d[len(d)-1] != "Four of Clubs" { + t.Errorf("Expected Four of Clubs but got %s", d[len(d)-1]) + } +} + +func TestSaveAndLoad(t *testing.T) { + os.Remove("_decktesting") + d := newDeck() + d.save("_decktesting") + loadDeck := load("_decktesting") + if len(loadDeck) != len(d) { + t.Errorf("Expected loaded and saved deck to be the same length") + } + os.Remove("_decktesting") +} diff --git a/cards/main.go b/cards/main.go index 5713dad..ce1cc12 100644 --- a/cards/main.go +++ b/cards/main.go @@ -3,8 +3,8 @@ package main func main() { //cards := newDeck() //cards.save("cards") - cards, _ := load("cards") - hand, remainingCards := deal(cards, 5) - hand.print() - remainingCards.print() + cards := load("cards") + cards.shuffle() + cards.print() + //hand, remainingCards := deal(cards, 5) } diff --git a/evenodd/go.mod b/evenodd/go.mod new file mode 100644 index 0000000..f1102a5 --- /dev/null +++ b/evenodd/go.mod @@ -0,0 +1,3 @@ +module evenodd + +go 1.17 diff --git a/evenodd/main.go b/evenodd/main.go new file mode 100644 index 0000000..437da8c --- /dev/null +++ b/evenodd/main.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func main() { + var numbers []int + for i := 0; i <= 10; i++ { + numbers = append(numbers, i) + } + for _, num := range numbers { + if num%2 == 0 { + fmt.Println("even") + } else { + fmt.Println("odd") + } + } +}