-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from anthonypena97/develop
Develop
- Loading branch information
Showing
5 changed files
with
129 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module example.com/main | ||
|
||
go 1.17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |