Creating a deck of cards in C#

I was having difficulty using one-dimensional arrays while creating a deck of cards in a Blackjack console game in C#. Most of the research I came up with used enum and list, but as part of an assessment I was required to use Array. I found some ways of doing this by using different aspects of other code I had seen, but they were long and repetitive and I didn’t fully understand them. After creating the first draft of code to create a deck of cards. I figured out a simpler and shorter method to do this.
How I started my deck of cards…
Below shows the code I originally started with, as you can see I am looping over the 4 different card suits and adding them to the deck array which holds 52 cards. To add cards this way I had to go through each card suit and loop over the number of card types that should be in each suit. Because of this method, I had to start from index 13 to add the new card suit and then again when adding this to the deck I had to minus the index from the card type index and card value index so that it would start from the beginning on the outside loop. Using this code required me to loop over 4 times but also to manually update the starting index and then change it again for the outside loop. Overall it was time-consuming and complicated.
for (int i = 0; i < 13; i++)
{
deck[i] = new Card(cardType[i], cardSuit[0], cardValue[i]);
Console.WriteLine($"Card = {cardType[i]}{cardSuit[0],2}, card value = {cardValue[i]}");
}
for (int i = 13; i < 26; i++)
{
deck[i] = new Card(cardType[i - 13], cardSuit[1], cardValue[i - 13]);
Console.WriteLine($"Card = {cardType[i - 13]}{cardSuit[1],2}, card value = {cardValue[i - 13]}");
}
...
}
return deck;
How I simplified the code for creating a deck…
Instead of using multiple for loops for each card suit, I used one for loop to loop over the card suits and used another for loop inside the card suit loop to loop over the card types. These 52 cards were then added to the deck. As you can see from the code snippet below this method is much clearer and more concise than the previous method shown.
private Card[] CreateDeck()
{
Card[] deck = new Card[52];
int cardIndex = 0;
for (int cardSuitIndex = 0; cardSuitIndex < cardSuit.Length; cardSuitIndex++)
{
for (int cardTypeIndex = 0; cardTypeIndex < cardType.Length; cardTypeIndex++)
{
deck[cardIndex] = new Card(cardType[cardTypeIndex], cardSuit[cardSuitIndex], cardValue[cardTypeIndex]);
cardIndex++;
}
}
return deck;
}
My learning...
- Problem-solving: My learning from this involved revising code already written and finding if there is a better way of writing the code. In my case, I wrote a long-handed method for adding objects to an
Array, but after stepping away from the problem and looking with fresher eyes I found a way to write the code to be clearer and more concise.
As a closing note, using enum and list may be a better way of creating a deck. But I will know more about this in the future, keep an eye out for a new/updated post.
Reference: Defining your cards and deck




