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();
}