这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added simple-card-game/card-game/assests/3-shuffle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 102 additions & 0 deletions simple-card-game/card-game/deck.go
Original file line number Diff line number Diff line change
@@ -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]

}
}
37 changes: 37 additions & 0 deletions simple-card-game/card-game/deck_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
3 changes: 3 additions & 0 deletions simple-card-game/card-game/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/AymenSegni/go-examples/card-game

go 1.17
44 changes: 44 additions & 0 deletions simple-card-game/card-game/main.go
Original file line number Diff line number Diff line change
@@ -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")

}