From d75c73a1c6db5e677592af83657782d3e9a4ba25 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Sun, 25 Apr 2021 01:59:41 -0600 Subject: [PATCH] Add Remove() method to remove a card from a deck. Add unit tests for Remove and for Copy. --- deck.go | 10 ++++++++++ deck_test.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/deck.go b/deck.go index 54e9f9a..bb49c7f 100644 --- a/deck.go +++ b/deck.go @@ -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 @@ -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) diff --git a/deck_test.go b/deck_test.go index 9177f44..a5310b8 100644 --- a/deck_test.go +++ b/deck_test.go @@ -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()