testing and assignment

This commit is contained in:
Johannes Rothe 2022-02-08 22:17:44 +01:00
parent b221562452
commit afbc977e1d
5 changed files with 83 additions and 15 deletions

View File

@ -3,7 +3,10 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand"
"os"
"strings" "strings"
"time"
) )
type deck []string 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 { func newDeck() deck {
cards := deck{} cards := deck{}
cardSuits := []string{"Spades", "Diamonds", "Hearts", "Clubs"} cardSuits := []string{"Spades", "Diamonds", "Hearts", "Clubs"}
@ -30,19 +50,15 @@ func deal(d deck, handsize int) (deck, deck) {
return d[:handsize], d[handsize:] return d[:handsize], d[handsize:]
} }
func (d deck) toString(seperator string) string {
return strings.Join(d, "\n")
}
func fromString(input []byte, seperator string) deck { 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 { func load(filename string) deck {
return ioutil.WriteFile(filename, []byte(d.toString("\n")), 0644)
}
func load(filename string) (deck, error) {
bytes, err := ioutil.ReadFile(filename) bytes, err := ioutil.ReadFile(filename)
return fromString(bytes, "\n"), err if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
return fromString(bytes, "\n")
} }

32
cards/deck_test.go Normal file
View File

@ -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")
}

View File

@ -3,8 +3,8 @@ package main
func main() { func main() {
//cards := newDeck() //cards := newDeck()
//cards.save("cards") //cards.save("cards")
cards, _ := load("cards") cards := load("cards")
hand, remainingCards := deal(cards, 5) cards.shuffle()
hand.print() cards.print()
remainingCards.print() //hand, remainingCards := deal(cards, 5)
} }

3
evenodd/go.mod Normal file
View File

@ -0,0 +1,3 @@
module evenodd
go 1.17

17
evenodd/main.go Normal file
View File

@ -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")
}
}
}