Skip to content

Commit

Permalink
Add Remove() method to remove a card from a deck. Add unit tests for …
Browse files Browse the repository at this point in the history
…Remove and for Copy.
  • Loading branch information
zchenyu committed Apr 25, 2021
1 parent 5aa0bb5 commit d75c73a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewDeck() *Deck {
return deck
}

// Copy returns a copy of the deck.
func (deck *Deck) Copy() *Deck {
if deck == nil {
return nil
Expand All @@ -33,6 +34,15 @@ func (deck *Deck) Copy() *Deck {
return deck2
}

// Remove removes a given card from the deck.
func (deck *Deck) Remove(c Card) {
for i, c2 := range deck.cards {
if c == c2 {
deck.cards = append(deck.cards[:i], deck.cards[i+1:]...)
}
}
}

func (deck *Deck) Shuffle() {
deck.cards = make([]Card, len(fullDeck.cards))
copy(deck.cards, fullDeck.cards)
Expand Down
15 changes: 15 additions & 0 deletions deck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ func TestNewDeck(t *testing.T) {
assert.False(t, same)
}

func TestCopy(t *testing.T) {
deck := NewDeck()
deck2 := deck.Copy()
assert.NotSame(t, deck, deck2)
assert.EqualValues(t, deck, deck2)
}

func TestRemove(t *testing.T) {
deck := NewDeck()
c := NewCard("As")
assert.Contains(t, deck.cards, c)
deck.Remove(c)
assert.NotContains(t, deck.cards, c)
}

func TestDraw(t *testing.T) {
deck := NewDeck()

Expand Down

0 comments on commit d75c73a

Please sign in to comment.