I do not want to spend a lot of time talking about our
implementation which makes the tests pass, because it is not the focus of the
article. For now I will just include a quick solution which causes the tests to
pass. Keep in mind that at each step we should only write just enough to make
the tests pass. We will write more tests in a moment which help us create more
functionality. If we flesh out the Deck class with code that actually works, we
end up with something similar to the following code.
Listing 2: The Simple Implementation
public class Deck
{
private readonly List<Card> _cards;
public Deck(List<Card> cards)
{
_cards = cards;
}
public int Count
{
get { return _cards.Count; }
}
public Card DrawCard()
{
Card drawnCard = _cards[0];
_cards.Remove(drawnCard);
return drawnCard;
}
}
Notice here that there are still plenty of bugs which can
occur with this code. For example if the deck is empty, I am going to get an
ArgumentOutOfRangeException. We need to get some more tests in place and make
them pass.