Hindrik
Hindrik

Reputation: 155

Creating a deck of cards - Dont get a part of the deck class - Beginning Visual C# 2010

I am new to C# and new to this forum. Decided to learn c# two months ago and started with Beginning Visual C# 2010. Didnt need any help till now. In this chapter (ch10) I had to create a deck of cards. I already made two enums with ranks and suits. After this I created the card class:

public class Card
{
    public readonly Rank rank;
    public readonly Suit suit;

    private Card()
    {

    }

    public Card(Suit newSuit, Rank newRank)
    {
        suit = newSuit;
        rank = newRank;
    }

    public override String ToString()
    {
        return "The " + rank + "of " + suit + "s";
    }
}

After this I had to make the deck class:

public class Deck
{
    private Card[] cards;

    public Deck()
    {
        cards = new Card[52];
        for (int suitVal = 0; suitVal < 4; suitVal++)
        {
            for (int rankVal = 1; rankVal < 14; rankVal++)
            {
                **cards[suitVal * 13 + rankVal -1] = new Card((Suit)suitVal,(Rank)rankVal);**
            }                
        }
    }

There is more to the deck class but I just dont get the part in bold (the 13 at least makes sence, feeling whise, since there are 13 cards per suit, but I really cant place the -1). What exactly happens in the deck class and specifically in the part in bold?

Thanks in advance

Upvotes: 2

Views: 1117

Answers (2)

SWeko
SWeko

Reputation: 30912

You start with

suitVal = 0; rankVal = 1;

and you need to make the first card in the deck, The first card is at the index position 0.

suitVal * 13 + rankVal - 1 = 0 * 13 + 1 - 1 = 0; <-- exactly what you need

Then you get

suitVal = 0; rankVal = 2;  //index should be 1
suitVal * 13 + rankVal - 1 = 0 * 13 + 2 - 1 = 1; <-- exactly what you need

All the way to the highest rank. So now you have a single suit in the deck, 13 cards at positions 0 through 12. Next index position should be 13, for the ace of the second suit

suitVal = 1; rankVal = 1;  //index should be 13
suitVal * 13 + rankVal - 1 = 1 * 13 + 1 - 1 = 13; <-- exactly what you need

etc, etc... up to

suitVal = 3; rankVal = 13;  //index should be 51, last one
suitVal * 13 + rankVal - 1 = 3 * 13 + 13 - 1 = 51; <-- exactly what you need

In C# all arrays/lists are 0-based, so when you include a 1-based construct, like the rankVal in your example, you have to compensate by removing one from it's index.

Upvotes: 1

CodeCaster
CodeCaster

Reputation: 151690

It's an index ranging from 0..51:

for (int suitVal = 0; suitVal < 4; suitVal++)
{
    for (int rankVal = 1; rankVal < 14; rankVal++)
    {
        int cardIndex = suitVal * 13 + rankVal - 1;
        cards[cardIndex] = new Card((Suit)suitVal,(Rank)rankVal);
    }                
}

This way with (suitVal * 13) + (rankVal - 1) you can access a specific card in the array. Because the rankVal starts at 1, you'll have to subtract one.

Upvotes: 1

Related Questions