diff --git a/simple-card-game/card-game/assests/0-game-desc.png b/simple-card-game/card-game/assests/0-game-desc.png new file mode 100644 index 0000000..b1b8124 Binary files /dev/null and b/simple-card-game/card-game/assests/0-game-desc.png differ diff --git a/simple-card-game/card-game/assests/1-project-structure.png b/simple-card-game/card-game/assests/1-project-structure.png new file mode 100644 index 0000000..88e5e46 Binary files /dev/null and b/simple-card-game/card-game/assests/1-project-structure.png differ diff --git a/simple-card-game/card-game/assests/2-deal-func.png b/simple-card-game/card-game/assests/2-deal-func.png new file mode 100644 index 0000000..b843bd0 Binary files /dev/null and b/simple-card-game/card-game/assests/2-deal-func.png differ diff --git a/simple-card-game/card-game/assests/3-shuffle.png b/simple-card-game/card-game/assests/3-shuffle.png new file mode 100644 index 0000000..9b57c4f Binary files /dev/null and b/simple-card-game/card-game/assests/3-shuffle.png differ diff --git a/simple-card-game/card-game/deck.go b/simple-card-game/card-game/deck.go new file mode 100644 index 0000000..1bc47e1 --- /dev/null +++ b/simple-card-game/card-game/deck.go @@ -0,0 +1,102 @@ +package main + +import ( + "fmt" + "io/ioutil" + "math/rand" + "os" + "strings" + "time" +) + +// declare new type called deck and assign to it a slice type + +type deck []string + +// func with receiver to iterate over a deck and print values + +func (d deck) print() { + for i, value := range d { + fmt.Println(i, value) + } +} + +// Create and return newDeck + +func newDeck() deck { + + cards := deck{} + + cardSuits := []string{"Spades", "Diamonds", "Hearts", "Clubs"} + cardValues := []string{"Ace", "Tow", "Three", "Four"} + + for _, suit := range cardSuits { + for _, value := range cardValues { + cards = append(cards, value+" of "+suit) + } + } + + return cards + +} + +// deal + +func deal(d deck, handSize int) (deck, deck) { + + return d[:handSize], d[handSize:] +} + +// save deck to file and load a deck from file +// to do: use the ioutil library +// to work with files in the ioutil library, will need to have a []byte instead +// will also need to import String package + +// fun to convert a deck to string +func (d deck) toString() string { + return strings.Join([]string(d), ",") + // []string(d) // convert deck to slice of string + +} + +func (d deck) saveToFile(filename string) error { + + return ioutil.WriteFile(filename, []byte(d.toString()), 0666) + +} + +// read deck from file +func newDeckFromFile(filename string) deck { + + bs, err := ioutil.ReadFile(filename) + + // error handling + if err != nil { + fmt.Println(err) + //quite + os.Exit(127) + } + + // convert byte slice into string then to string slice to deck + + s := strings.Split(string(bs), ",") + + return deck(s) + +} + +// shuffle list of deck + +func (d deck) shuffle() { + + source := rand.NewSource(time.Now().UnixNano()) + + r := rand.New(source) + + for i := range d { + newPosition := r.Intn(len(d) - 1) + + d[i], d[newPosition] = d[newPosition], d[i] + + } +} diff --git a/simple-card-game/card-game/deck_test.go b/simple-card-game/card-game/deck_test.go new file mode 100644 index 0000000..5c163d0 --- /dev/null +++ b/simple-card-game/card-game/deck_test.go @@ -0,0 +1,37 @@ +package main + +import ( + "os" + "testing" +) + +func TestNewDeck(t *testing.T) { + d := newDeck() + + if len(d) != 16 { + t.Errorf("Expected deck length of 16, but got %v", len(d)) + } + + if d[0] != "Ace of Spades" { + t.Errorf("Expected first card of Ace of Spades, but got %v", d[0]) + } + + if d[len(d)-1] != "Four of Clubs" { + t.Errorf("Expected last card of Four of Clubs, but got %v", d[len(d)-1]) + } +} + +func TestSaveToDeckAndNewDeckFromFile(t *testing.T) { + os.Remove("_decktesting") + + deck := newDeck() + deck.saveToFile("_decktesting") + + loadedDeck := newDeckFromFile("_decktesting") + + if len(loadedDeck) != 16 { + t.Errorf("Expected 16 cards in deck, got %v", len(loadedDeck)) + } + + os.Remove("_decktesting") +} diff --git a/simple-card-game/card-game/go.mod b/simple-card-game/card-game/go.mod new file mode 100644 index 0000000..8b0e0e2 --- /dev/null +++ b/simple-card-game/card-game/go.mod @@ -0,0 +1,3 @@ +module github.com/AymenSegni/go-examples/card-game + +go 1.17 diff --git a/simple-card-game/card-game/main.go b/simple-card-game/card-game/main.go new file mode 100644 index 0000000..602a45e --- /dev/null +++ b/simple-card-game/card-game/main.go @@ -0,0 +1,44 @@ +package main + +import "fmt" + +func main() { + + // create new deck + fmt.Println("step 1: add new deck function") + cards := newDeck() + fmt.Println("step 1 is done") + + // print deck values + fmt.Println("step 2: print list of cards in deck") + cards.print() + fmt.Println("step 2 is done") + + // deal function + fmt.Println("step 3: deal function") + hand, leftHand := deal(cards, 3) + fmt.Println(hand) + fmt.Println(leftHand) + fmt.Println("step 3 is done") + + // save to file + fmt.Println("step 4: save deck to file func") + cards.saveToFile("deck_file") + fmt.Println("step 4 is done") + + // read deck from file + fmt.Println("step 5: read deck from file") + cards = newDeckFromFile("deck_file") + cards.print() + fmt.Println("step 5 is done") + + // shuffle cards + fmt.Println("step 6: run shuffle operation") + cards.shuffle() + cards.print() + fmt.Println("step 6 is done") + + // finish the game + fmt.Println("Game Over") + +}