-2

Im tring to create all 52 pokercards in a loop.

Like this:

Card heart1 = new Card(1, "hearts", "hearts 1");
Card heart2 = new Card(2, "hearts", "hearts 2");

This is what I have but i'm stuck:

string[] icon = new string[]{ "hearts", "clubs", "diamonds", "spades" };
Card[] card = new Card[13];
foreach (string i in icon)
{
     for (int j = 1; j <= 13; j++)
     {

     }
}
    

I'm trying to create all 13 cards for every type of card. I want de varible to have the same name as the string with the nummber next to it.

I'm new to C# so let me know if i'm doing it wrong or if there is a better way to do this. I know I can do this by hand, but I want it to do it automatically.

Said
  • 3
  • 2
  • 1
    "I want de varible to have the same name as the string with the nummber next to it." - no, you don't want that and you cannot have that. Instead use the array you have already set up and put the cards in there and if you need the seven of hearts look at index 6 in the array. – luk2302 Feb 19 '22 at 22:07
  • Have you considered making a `Deck` Class? Creating a new `Deck` object will create the unique 52 `Card` objects. In addition, the `Deck` object could have some methods, like… `Shuffle` and `DealOneCard`. It is unclear what the `Card` class looks like, however it appears that each `Card` has an ID value, which appears superfluous since each `Cards` “Suit” and “Rank” properties would uniquely identify the Card. In other words, no two cards in the `Deck` will have the same “Suit” and “Rank” values. – JohnG Feb 19 '22 at 22:40
  • There are many dozens of good posts here illustrating how to create cars, decks nd shuffling decks of cards. – Ňɏssa Pøngjǣrdenlarp Feb 20 '22 at 00:06
  • @JohnG yes I already have a class named Deck with a method to add de cards in a list with de class Card. And within the Card class a method that adds the card to the deck I create. All I want to know is how to create all 52 cards without doing it by hand. If I know that I can handle the rest myself. The 1st varible is an indication of how many suits are on the card the 2dn is the kind of suit and the 3th is an combination of the two. – Said Feb 20 '22 at 00:32
  • many posts associated with decks of cards. I have previously offered similar for readability and generating randomized deck via https://stackoverflow.com/questions/57846758/when-trying-to-display-a-hand-of-cards-from-a-shuffled-deck-it-fills-every-hand/57848470#57848470 to consider. – DRapp Feb 20 '22 at 01:11

1 Answers1

0

C# does not support dynamically named variables. You can declare an array of cards and initialize the elements in a loop like so:

Card[] deck = new Card[52];
string[] suits = {"hearts", "clubs", "diamonds", "spades"};
for(int i=0; i < suits.Length; i++)
{
    for (int j = 1; j <= deck.Length/suits.Length; j++)
    {
        deck[i*j] = new Card(j, suits[i], $"{i} of {suits[i]}");
    }
}