Skip to content

Commit

Permalink
Merge pull request #17 from heindrichpaul/feature/Bugfix
Browse files Browse the repository at this point in the history
updated all dependencies
  • Loading branch information
pencilpainter authored Apr 30, 2019
2 parents 32633b2 + 6023e24 commit cad587e
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 72 deletions.
15 changes: 13 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
language: go


env:
global:
CC_TEST_REPORTER_ID=cd0be3b4894ea03cc442fe60aee02ff88a0ac273720589a06624c2b2080d0832

go:
- "1.12.1"
- "1.11.6"
- "1.12.4"
- "1.11.9"
- "1.10.8"

before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build

before_install:
- go get -t -v ./...

Expand All @@ -13,3 +23,4 @@ script:

after_success:
- bash <(curl -s https://codecov.io/bash)
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DeckOfCards [![GoDoc](https://godoc.org/github.com/heindrichpaul/deckofcards?status.svg)](https://godoc.org/github.com/heindrichpaul/deckofcards)[![Build Status](https://www.travis-ci.com/heindrichpaul/deckofcards.svg?branch=master)](https://www.travis-ci.com/heindrichpaul/deckofcards)[![codecov](https://codecov.io/gh/heindrichpaul/DeckOfCards/branch/master/graph/badge.svg)](https://codecov.io/gh/heindrichpaul/DeckOfCards)[![Go Report Card](https://goreportcard.com/badge/github.com/heindrichpaul/deckofcards)](https://goreportcard.com/report/github.com/heindrichpaul/deckofcards)
# DeckOfCards [![GoDoc](https://godoc.org/github.com/heindrichpaul/deckofcards?status.svg)](https://godoc.org/github.com/heindrichpaul/deckofcards)[![Build Status](https://www.travis-ci.com/heindrichpaul/deckofcards.svg?branch=master)](https://www.travis-ci.com/heindrichpaul/deckofcards)[![codecov](https://codecov.io/gh/heindrichpaul/DeckOfCards/branch/master/graph/badge.svg)](https://codecov.io/gh/heindrichpaul/DeckOfCards)[![Go Report Card](https://goreportcard.com/badge/github.com/heindrichpaul/deckofcards)](https://goreportcard.com/report/github.com/heindrichpaul/deckofcards)[![Maintainability](https://api.codeclimate.com/v1/badges/544f91c2a39e76dc8c2e/maintainability)](https://codeclimate.com/github/heindrichpaul/deckofcards/maintainability)

---

Expand Down
1 change: 0 additions & 1 deletion card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestNewCard(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)
if err != nil {
t.Logf("Failed to create card for: %s%s\n", value, suit)
Expand Down
133 changes: 80 additions & 53 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,65 +61,15 @@ func newDeck(amount int, jokers bool) (deck *Deck, err error) {
}

for deckNum := 0; deckNum < amount; deckNum++ {
for _, suit := range suits {
//ACE
card, err := newCard(deck.DeckID, "A", suit)
if err != nil {
return nil, err
}
deck.cards = append(deck.cards, card)
deck.Remaining++
//NUMERICAL CARDS
for i := 2; i < 10; i++ {
card, err = newCard(deck.DeckID, strconv.Itoa(i), suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
}
//TEN
card, err = newCard(deck.DeckID, "0", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
//JACK
card, err = newCard(deck.DeckID, "J", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
//QUEEN
card, err = newCard(deck.DeckID, "Q", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
//KING
card, err = newCard(deck.DeckID, "K", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
}
if jokers {
card, err := newCard(deck.DeckID, "*", "*")
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
card, err = newCard(deck.DeckID, "*", "*")
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
if err := createDeck(deck, jokers); err != nil {
return nil, err
}
}
if deck.Remaining == len(deck.cards) && deck.Remaining > 0 {
deck.Success = true
deck.Shuffled = false
}
return deck, nil
return
}

//ShuffleDeck shuffles the deck that has been passed as a parameter.
Expand Down Expand Up @@ -187,3 +137,80 @@ func parseDeckCreation(deck *Deck, err error) *Deck {
return deck

}

func createDeck(deck *Deck, jokers bool) (err error) {
for _, suit := range suits {
if err = addAceToDeck(deck, suit); err != nil {
return err
}
//As all errors are handled in the method that adds aces
addNumericCardsToDeck(deck, suit)
addPictureCardsToDeck(deck, suit)
}
err = addJokersToDeck(deck, jokers)
return
}

func addJokersToDeck(deck *Deck, jokers bool) (err error) {
if jokers {
card, err := newCard(deck.DeckID, "*", "*")
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
card, err = newCard(deck.DeckID, "*", "*")
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
}
return
}

func addPictureCardsToDeck(deck *Deck, suit string) {
//JACK
card, err := newCard(deck.DeckID, "J", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
//QUEEN
card, err = newCard(deck.DeckID, "Q", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
//KING
card, err = newCard(deck.DeckID, "K", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
}

func addAceToDeck(deck *Deck, suit string) (err error) {
//ACE
card, err := newCard(deck.DeckID, "A", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
return
}

func addNumericCardsToDeck(deck *Deck, suit string) {
//NUMERICAL CARDS
for i := 2; i < 10; i++ {
card, err := newCard(deck.DeckID, strconv.Itoa(i), suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
}
//TEN
card, err := newCard(deck.DeckID, "0", suit)
if err == nil {
deck.cards = append(deck.cards, card)
deck.Remaining++
}
}
15 changes: 15 additions & 0 deletions draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ func (z *Draw) String() string {
return strings.Join(printString, "\n")

}

//AreAllCardsInThisDraw returns true if all the cards in the cards slice are in the draw.
func (z *Draw) AreAllCardsInThisDraw(cards Cards) bool {
for _, cardFromCards := range cards {
for d, cardFromDraw := range z.Cards {
if cardFromCards.Equals(cardFromDraw) {
break
}
if d == len(z.Cards)-1 && !cardFromCards.Equals(cardFromDraw) {
return false
}
}
}
return true
}
25 changes: 25 additions & 0 deletions draw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,28 @@ func TestDrawString(t *testing.T) {
t.FailNow()
}
}

func TestAreAllCardsInThisDraw(t *testing.T) {
deck := NewDeckWithJokers(1)
draw := deck.Draw(deck.Remaining)
if !draw.AreAllCardsInThisDraw(draw.Cards) {
t.Logf("All cards in a draw should be present in itself")
t.FailNow()
}
}

func TestAreAllCardsInThisDrawNegativeTest(t *testing.T) {
deck := NewDeck(1)
draw := deck.Draw(deck.Remaining)
card, err := newCard(deck.DeckID, "*", "*")
if err != nil {
t.Logf("Failed to create joker card")
t.FailNow()
}
var cards Cards
cards = append(cards, card)
if draw.AreAllCardsInThisDraw(cards) {
t.Logf("All cards from a deck without jockers should not contain a jocker")
t.FailNow()
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/heindrichpaul/deckofcards

require (
github.com/json-iterator/go v1.1.5
github.com/json-iterator/go v1.1.6
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/twinj/uuid v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
Expand Down
15 changes: 2 additions & 13 deletions pile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,8 @@ type Pile struct {
func (z *Pile) AddCardsToPile(draw *Draw, cards Cards) {

if draw != nil && draw.Success && len(draw.Cards) != 0 {
if len(draw.Cards) >= len(cards) {

for _, card := range cards {
found := false
for _, f := range draw.Cards {
if f.Value == card.Value && f.Suit == card.Suit {
found = true
}
}
if found {
z.cards = append(z.cards, card.cloneCard())
}
}
if len(draw.Cards) >= len(cards) && draw.AreAllCardsInThisDraw(cards) {
z.cards = append(z.cards, cards...)
}
}

Expand Down
1 change: 0 additions & 1 deletion pile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestAddCardsToPile(t *testing.T) {
t.FailNow()
}
pile.AddCardsToPile(draw, draw.Cards)

found := false
for _, pileCard := range pile.RetrieveCardsInPile() {
for _, drawCard := range draw.Cards {
Expand Down

0 comments on commit cad587e

Please sign in to comment.