-2

I am creating a blackjack game and so far I have made a card class and a deck class so far. The card class works but my deck class isn't displaying anything when I go to test it. my deck class is supose to use a nested loop to set up the cards with values taken from the arrays and then the GetCard is suppose to allow the user to get a card from the deck (when shuffling and dealing) and the SetCard class is suppose set a card in the deck (when shuffling) but when I go to test my deck class it just says object reference not set to an instance of an object. I am not sure on how to fix this.

Any help would be appricated

Here is what I have for my deck class

class Deck
{

    private const Int32 MAXCARDS = 52;
    private Card[] _cards = new Card[MAXCARDS];

    public Deck()
    {
        Card.SUIT[] suits = { Card.SUIT.SPADES, Card.SUIT.HEARTS, Card.SUIT.DIAMONDS, Card.SUIT.CLUBS };
        String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

        Card[] orderedCards = new Card[suits.Length * values.Length];
        int newIndex = 0;

        // Generate an array with all possible combinations of suits and values
        for (int suitsIndex = 0; suitsIndex < suits.Length; suitsIndex++)
        {
            for (int valuesIndex = 0; valuesIndex < values.Length; valuesIndex++)
            {
                newIndex = values.Length * suitsIndex + valuesIndex; // Think about it :)
                orderedCards[newIndex] = new Card(suits[suitsIndex], values[valuesIndex]);
            }
        }
    }


   // allows the user to get a card from the deck (when shuffling and dealing)
    public Card GetCard(Int32 index)
    {
        if (index >= 0 && index <= 51)
        {
            return _cards[index];
        }
        else
        {
            throw (new System.ArgumentOutOfRangeException("cardNum", index,
            "Value must be between 0 and 51."));
        }
    }
    // allows the user to set a card in the deck (when shuffling)
    public Card SetCard(Int32 index, Card Card)
    {
        if (index >= 0 && index <= 51)
        {
            return _cards[index];

        }
        else
        {
            throw (new System.ArgumentOutOfRangeException("cardNum", index,
            "Value must be between 0 and 51."));
        }
    }
}

Here's the code I am using to test it

static void Main(string[] args)
    {
        Deck testDeck = new Deck();
        Card card;

        try
        {
            for(Int32 index =0; index < 52; index ++ )
            {
                card = testDeck.GetCard(index);
                Console.WriteLine(card.Suit + " " + card.Value);
            }
            testDeck.SetCard(0, new Card(Card.SUIT.HEARTS, "10"));
            card = testDeck.GetCard(0);
            Console.WriteLine(card.Suit + " " + card.Value);
            testDeck.SetCard(52, new Card(Card.SUIT.DIAMONDS, "3"));
            card = testDeck.GetCard(52);
            Console.WriteLine(card.Suit + " " + card.Value);


        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        Console.ReadLine();
    }
drew202
  • 17
  • 1
  • 5
  • 1
    Very active class:) A few hours ago someone asked how to validate the input. post this to him `bool valid = values.Any(x => x == value);` – L.B Dec 05 '14 at 23:46
  • `SetCard()` would appear to be identical to `GetCard()` but ignores the additional function argument `Card`. – Weather Vane Dec 05 '14 at 23:51

1 Answers1

0

You never assign any value to _cards in your constructor for Deck. Instead, you fill up a local variable called OrderedCards.

Drop OrderedCards, and directly set _cards in the constructor. Such as this:

public Deck()
{
    Card.SUIT[] suits = { Card.SUIT.SPADES, Card.SUIT.HEARTS, Card.SUIT.DIAMONDS, Card.SUIT.CLUBS };
    String[] values = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

    int newIndex = 0;

    // Generate an array with all possible combinations of suits and values
    for (int suitsIndex = 0; suitsIndex < suits.Length; suitsIndex++)
    {
        for (int valuesIndex = 0; valuesIndex < values.Length; valuesIndex++)
        {
            newIndex = values.Length * suitsIndex + valuesIndex; // Think about it :)
            _cards[ newIndex ] = new Card(suits[suitsIndex], values[valuesIndex]) );
        }
    }
}
StarPilot
  • 2,246
  • 1
  • 16
  • 18