Skip to content

Commit

Permalink
Merge pull request #9 from anthonypena97/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
anthonypena97 authored May 5, 2022
2 parents c8bc643 + f9c1db6 commit 98771f6
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 15 deletions.
78 changes: 78 additions & 0 deletions deck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
)

//lint:file-ignore U1000 Ignore all unused code, it's generated

// Create a new type of 'deck'
// which is a slice of string
type deck []string

func newDeck() deck {
cards := deck{}
cardSuits := []string{"Spades", "Diamonds", "Hearts", "Clubs"}
cardValues := []string{"Ace", "Two", "Three", "Four"}

// 'i' and 'j' are replaced with underscores, '_' , to let go know we don't need these variables
for _, suit := range cardSuits {
for _, value := range cardValues {
cards = append(cards, value+" of "+suit)
}
}

return cards

}

// function with a receiver declared as 'd'
// by convention - the receiver is commonly decalred as one or two letters that match to the type
func (d deck) print() {

for i, card := range d {
fmt.Println(i, card)
}

}

// the second paranthesis are the two types we will be retrning
func deal(d deck, handSize int) (deck, deck) {
return d[:handSize], d[handSize:]
}

func (d deck) toString() string {
return strings.Join([]string(d), ", ")
}

func (d deck) saveToFile(filename string) error {
return ioutil.WriteFile(filename, []byte(d.toString()), 0666)
}

func newDeckFromFile(filename string) deck {
bs, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}

s := strings.Split(string(bs), ", ")
return deck(s)
}

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]
}

}
44 changes: 44 additions & 0 deletions deck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"os"
"testing"
)

func TestNewDeck(t *testing.T) {

d := newDeck()

// test #1
if len(d) != 16 {
//%v takes the len(d) value and injects it
t.Errorf("Expected deck length of 16, but got %v", len(d))
}

// test #2
if d[0] != "Ace of Spades" {
t.Errorf("Expected first card of Ace of Spades, but got %v", d[0])
}

// test #3
if d[len(d)-1] != "Four of Clubs" {
t.Errorf("Expectd 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 go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/main

go 1.17
18 changes: 3 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
package main

import "fmt"

func main() {

// var card string = "Ace of Spades"
// card := "Ace of Spades" // variable with type String
// card = "Five of Diamonds"

card := newCard()

fmt.Println(card)

}

func newCard() string {

return "Five of Diamonds"
cards := newDeck()
cards.shuffle()
cards.print()

}
1 change: 1 addition & 0 deletions my_cards
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ace of Spades, Two of Spades, Three of Spades, Four of Spades, Ace of Diamonds, Two of Diamonds, Three of Diamonds, Four of Diamonds, Ace of Hearts, Two of Hearts, Three of Hearts, Four of Hearts, Ace of Clubs, Two of Clubs, Three of Clubs, Four of Clubs

0 comments on commit 98771f6

Please sign in to comment.