mercredi 25 novembre 2015

C# I have 50+ else if statements for cards, is there a way to make it shorter or do it all at one go?

I have a form that loads and generates 7 different random numbers, from 1-13, 1 being Ace, and 13 being King. After generating 7 different random numbers, it puts each of those random numbers into the 7 picture boxes. I'm displaying the picture boxes using the if statement.

It also cycles through an array of "Spades, Hearts, Clubs and Diamonds", 13 times.

And My if statements are like:

if (cardNum == 1 && cardType ==  "Spades")
{
    pictureBox1.Image = ace_of_spades;
}
else if (cardNum == 1 && cardType == "Hearts")
{
    pictureBox1.Image = ace_of_hearts;
}
else if (...)
{
    //change picture box
} //repeat it like 50 times

Is there a simple, easy way to pick 7 random cards and show them in the picture box?

It is extremely time consuming, the way I do it.

Aucun commentaire:

Enregistrer un commentaire