This commit is contained in:
Benjamyn Love 2021-09-28 23:13:24 +10:00
parent 3da69bccc6
commit 49b436d6f9
7 changed files with 89 additions and 2 deletions

View File

@ -3,7 +3,10 @@ package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)
// Create a new type of 'deck
@ -44,3 +47,23 @@ func (d deck) saveToFile(fileName string) error {
error := ioutil.WriteFile(fileName, []byte(d.toString()), 0644)
return error
}
func newDeckFromFile(filename string) deck {
bs, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Error: ", err)
os.Exit(69)
}
return deck(strings.Split(string(bs), ","))
}
func (d deck) shuffle() {
source := rand.NewSource(time.Now().UnixNano())
rdm := rand.New(source)
for i := range d {
newPos := rdm.Intn(len(d) - 1)
d[i], d[newPos] = d[newPos], d[i]
}
}

39
deck/deck_test.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"os"
"testing"
)
func TestNewDeck(t *testing.T) {
d := newDeck()
if len(d) != 52 {
t.Errorf("Expected deck length of 52, but got %d", len(d))
}
if d[0] != "Ace of Spades" {
t.Errorf("Expected the first card to be Ace of Spades, but got %s", d[0])
}
if d[len(d)-1] != "King of Clubs" {
t.Errorf("Expected the first card to be King of Clubs, but got %s", d[len(d)-1])
}
}
func TestSaveDeckToDiskAndLoadDeckFromDisk(t *testing.T) {
os.Remove("_decktesting")
d := newDeck()
r_err := d.saveToFile("_decktesting")
if r_err != nil {
t.Errorf("Failed to save file to _decktesting, got error: %s", r_err)
}
r := newDeckFromFile("_decktesting")
if r.toString() != d.toString() {
t.Errorf("Failed to load deck, contents does not match")
}
os.Remove("_decktesting")
}

3
deck/go.mod Normal file
View File

@ -0,0 +1,3 @@
module blove/deck
go 1.17

View File

@ -1,6 +1,10 @@
package main
func main() {
cards := newDeck()
cards.saveToFile("mydeck.txt")
// cards := newDeck()
// cards.saveToFile("mydeck.txt")
cards := newDeckFromFile("mydeck.txt")
cards.shuffle()
cards.print()
}

1
deck/mydeck.txt Normal file
View File

@ -0,0 +1 @@
Ace of Spades,Two of Spades,Three of Spades,Four of Spades,Five of Spades,Six of Spades,Seven of Spades,Eight of Spades,Nine of Spades,Ten of Spades,Jack of Spades,Queen of Spades,King of Spades,Ace of Hearts,Two of Hearts,Three of Hearts,Four of Hearts,Five of Hearts,Six of Hearts,Seven of Hearts,Eight of Hearts,Nine of Hearts,Ten of Hearts,Jack of Hearts,Queen of Hearts,King of Hearts,Ace of Diamonds,Two of Diamonds,Three of Diamonds,Four of Diamonds,Five of Diamonds,Six of Diamonds,Seven of Diamonds,Eight of Diamonds,Nine of Diamonds,Ten of Diamonds,Jack of Diamonds,Queen of Diamonds,King of Diamonds,Ace of Clubs,Two of Clubs,Three of Clubs,Four of Clubs,Five of Clubs,Six of Clubs,Seven of Clubs,Eight of Clubs,Nine of Clubs,Ten of Clubs,Jack of Clubs,Queen of Clubs,King of Clubs

3
evenodd/go.mod Normal file
View File

@ -0,0 +1,3 @@
module lovelynet.net/evenodd
go 1.17

14
evenodd/main.go Normal file
View File

@ -0,0 +1,14 @@
package main
import "fmt"
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
for _, n := range s {
if n%2 == 0 {
fmt.Println(n, "is even")
} else {
fmt.Println(n, "is odd")
}
}
}