Skip to content

Commit

Permalink
Merge pull request #5 from heindrichpaul/creating_pile_logic
Browse files Browse the repository at this point in the history
Creating pile logic
  • Loading branch information
heindrichpaul authored Dec 5, 2018
2 parents 462929b + f404b6f commit f64623a
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 67 deletions.
71 changes: 35 additions & 36 deletions card.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package deckOfCards
package deckofcards

import (
"fmt"
Expand All @@ -21,15 +21,14 @@ type cardError struct {
suit string
}

func newCard(deckId, value, suit string) (card *Card, err error) {

func newCard(deckID, value, suit string) (card *Card, err error) {
values := regexp.MustCompile(`[2-9]|0|A|K|Q|J|\*`)
suites := regexp.MustCompile(`S|D|C|H|\*`)

if !strings.EqualFold(deckId, "") {
if !strings.EqualFold(deckID, "") {

card = &Card{
DeckID: deckId,
DeckID: deckID,
Code: "",
Image: "",
Value: "",
Expand All @@ -39,41 +38,41 @@ func newCard(deckId, value, suit string) (card *Card, err error) {

if !suites.MatchString(suit) {
return nil, &cardError{"invalid suit.", value, suit}
} else {
switch suit {
case "S":
card.Suit = "SPADES"
case "D":
card.Suit = "DIAMONDS"
case "C":
card.Suit = "CLUBS"
case "H":
card.Suit = "HEARTS"
default:
card.Suit = "NONE"
}
}

switch suit {
case "S":
card.Suit = "SPADES"
case "D":
card.Suit = "DIAMONDS"
case "C":
card.Suit = "CLUBS"
case "H":
card.Suit = "HEARTS"
default:
card.Suit = "NONE"
}

if !values.MatchString(value) {
return nil, &cardError{"invalid value.", value, suit}
} else {
switch value {
case "A":
card.Value = "ACE"
case "K":
card.Value = "KING"
case "Q":
card.Value = "QUEEN"
case "J":
card.Value = "JACK"
case "0":
card.Value = "10"
case "*":
card.Value = "JOCKER"
default:
card.Value = value
}
}
switch value {
case "A":
card.Value = "ACE"
case "K":
card.Value = "KING"
case "Q":
card.Value = "QUEEN"
case "J":
card.Value = "JACK"
case "0":
card.Value = "10"
case "*":
card.Value = "JOCKER"
default:
card.Value = value
}

card.Code = fmt.Sprintf("%s%s", value, suit)
if !strings.EqualFold("*", value) && !strings.EqualFold("*", suit) {
card.Image = fmt.Sprintf("https://deckofcardsapi.com/static/img/%s.png", card.Code)
Expand All @@ -86,7 +85,7 @@ func newCard(deckId, value, suit string) (card *Card, err error) {
}

func (z *Card) String() string {
return fmt.Sprintf("%s - %s", z.Suit, z.Value)
return fmt.Sprintf("DeckID: %s\n%s - %s", z.DeckID, z.Suit, z.Value)
}

func (z *Card) draw() *Card {
Expand Down
12 changes: 9 additions & 3 deletions card_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package deckOfCards
package deckofcards

import (
"fmt"
Expand Down Expand Up @@ -29,9 +29,9 @@ func TestNewCard(t *testing.T) {
cardCreatorHelper(TestDECKID, "*", "*", t)
}

func cardCreatorHelper(deckId, suit, value string, t *testing.T) {
func cardCreatorHelper(deckID, suit, value string, t *testing.T) {
t.Log("Now running " + fmt.Sprintf("%s%s", value, suit) + ": " + time.Now().String())
card, err := newCard(deckId, value, suit)
card, err := newCard(deckID, value, suit)
if err != nil {
t.Logf("Failed to create card for: %s%s\n", value, suit)
t.FailNow()
Expand Down Expand Up @@ -121,6 +121,12 @@ func cardCreatorHelper(deckId, suit, value string, t *testing.T) {
}
}

if !strings.EqualFold(card.DeckID, TestDECKID) {
t.Logf("The DeckID is not correctly stored on the creation of a new card.\n")
t.FailNow()

}

t.Log("Finished running " + fmt.Sprintf("%s%s", value, suit) + ": " + time.Now().String())
}

Expand Down
20 changes: 10 additions & 10 deletions deck.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package deckOfCards
package deckofcards

import (
"fmt"
Expand All @@ -20,14 +20,14 @@ func UnmarshalDeck(data []byte) (*Deck, error) {
return &r, err
}

func (r *Deck) Marshal() ([]byte, error) {
func (z *Deck) Marshal() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(r)
return json.Marshal(z)
}

type Deck struct {
Remaining int `json:"remaining"`
DeckID string `json:"deck_id"`
DeckID string `json:"deckId"`
Success bool `json:"success"`
Shuffled bool `json:"shuffled"`
cards []*Card
Expand Down Expand Up @@ -96,37 +96,37 @@ func newDeck(amount int, jockers bool) (deck *Deck, err error) {
card, err = newCard(deck.DeckID, "0", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining += 1
deck.Remaining++
}
//JACK
card, err = newCard(deck.DeckID, "J", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining += 1
deck.Remaining++
}
//QUEEN
card, err = newCard(deck.DeckID, "Q", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining += 1
deck.Remaining++
}
//KING
card, err = newCard(deck.DeckID, "K", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining += 1
deck.Remaining++
}
}
if jockers {
card, err := newCard(deck.DeckID, "*", "*")
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining += 1
deck.Remaining++
}
card, err = newCard(deck.DeckID, "*", "*")
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining += 1
deck.Remaining++
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion deck_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package deckOfCards
package deckofcards

import (
"fmt"
Expand Down Expand Up @@ -28,6 +28,12 @@ func TestNewDeck(t *testing.T) {
t.Logf("Deck not properly initialized. Expected an unshuffled deck\n")
t.FailNow()
}
for _, card := range deck.cards {
if !strings.EqualFold(deck.DeckID, card.DeckID) {
t.Logf("The card's DeckID differs from the deck it belongs to.\n")
t.FailNow()
}
}
}

func TestNewDeckWithNegativeNumber(t *testing.T) {
Expand Down
30 changes: 26 additions & 4 deletions draw.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package deckOfCards
package deckofcards

import jsoniter "github.com/json-iterator/go"
import (
"fmt"
"strings"

jsoniter "github.com/json-iterator/go"
)

func UnmarshalDraw(data []byte) (*Draw, error) {
var r *Draw
Expand All @@ -9,13 +14,30 @@ func UnmarshalDraw(data []byte) (*Draw, error) {
return r, err
}

func (r *Draw) Marshal() ([]byte, error) {
func (z *Draw) Marshal() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(r)
return json.Marshal(z)
}

type Draw struct {
Success bool `json:"success"`
Cards []*Card `json:"cards"`
Remaining int `json:"remaining"`
}

func (z *Draw) String() string {
var printString []string
/*printString = append(printString, fmt.Sprintf("DeckID: %s", z.DeckID))
printString = append(printString, fmt.Sprintf("Success: %t", z.Success))
printString = append(printString, fmt.Sprintf("Shuffled: %t", z.Shuffled))
printString = append(printString, fmt.Sprintf("Remaining: %d", z.Remaining))
*/
for _, card := range z.Cards {
if !card.drawn {
printString = append(printString, fmt.Sprintf("%s", card))
}
}

return strings.Join(printString, "\n")

}
2 changes: 1 addition & 1 deletion draw_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package deckOfCards
package deckofcards

import (
"testing"
Expand Down
Loading

0 comments on commit f64623a

Please sign in to comment.