testing and assignment
This commit is contained in:
parent
b221562452
commit
afbc977e1d
@ -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")
|
||||
}
|
||||
|
32
cards/deck_test.go
Normal file
32
cards/deck_test.go
Normal 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")
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
3
evenodd/go.mod
Normal file
3
evenodd/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module evenodd
|
||||
|
||||
go 1.17
|
17
evenodd/main.go
Normal file
17
evenodd/main.go
Normal 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")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user