Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 831
Taylor Avatar asked Nov 25 '15 22:11

Taylor


2 Answers

There are a lot of ways, but in general I would say: Use an array as a lookup

For example, you have an array like

Image[] images = new[]
{
    ace_of_spades,
    ace_of_hearts,
    ...
};

Now all you have to do is calculate the right index. Since you didn't provide as much information as I need to help you on this, I will simply guess that it would look similar to this:

pictureBox1.Image = images[cardNum * 7 + (int)cardType];

Like I said, this is the idea behind this. Now you have to find the right calculation for it.

like image 66
bytecode77 Avatar answered Jan 22 '23 07:01

bytecode77


So many non-OOP suggestions of how to handle this. Here's my solution, which lets each object track itself, and will provide a simple way to shuffle the deck and get the image associated with each card (I've written a few card games).

For storing the actual images, embed them into your project as resource files, named in a specific way:

  • card_Club_1
  • card_Club_2
  • card_Club_3
  • etc...

Then, when you want a card image, you just combine the suit and value and request that resource name from the resource manager, as shown below. This method requires a bit more setup and planning, but will give you much cleaner code. You can even do all this in a separate project and then re-use the classes/resources by just referencing the DLL in the application where you want to have a deck of cards available.

enum Suit : uint
{
    Club = 0,
    Heart,
    Spade,
    Diamond
}
class Card
{
    public int
        Value;
    public Suit
        Suit;

    public System.Drawing.Image GetImage()
    {
        return System.Drawing.Image.FromStream(
            global::cardLibraryProject.Properties.Resources.ResourceManager.GetStream(string.Format("card_{0}_{1}", this.Suit, this.Value))
        );
    }
}
class Deck
{
    System.Collections.ArrayList
        _arr;

    private Deck()
    {
        this._arr = new System.Collections.ArrayList(52);
    }

    void Add(Card crd)
    {
        if (!this._arr.Contains(crd))
            this._arr.Add(crd);
    }

    public void Shuffle()
    {
        Random rnd = new Random(DateTime.Now.Millisecond);
        System.Collections.ArrayList tmp1 = new System.Collections.ArrayList(this._arr);
        System.Collections.ArrayList tmp2 = new System.Collections.ArrayList(52);
        while (tmp1.Count > 0)
        {
            int idx = rnd.Next(tmp1.Count);
            tmp2.Add(tmp1[idx]);
            tmp1.RemoveAt(idx);
        }
        this._arr = tmp2;
        tmp1.Clear();
        tmp2.Clear();
    }

    public static Deck CreateDeck()
    {
        Deck newDeck = new Deck();
        for (int s = 0; s < 4; s++)
            for (int i = 0; i < 13; i++)
                newDeck.Add(new Card { Value = i, Suit = (Suit)s });
        return newDeck;
    }
}
class Program
{
    public void Main(string[] args)
    {
        Deck cards = Deck.CreateDeck();
        cards.Shuffle();

        pictureBox1.Image = cards[0].GetImage();
        // code to play game would go here.  Obviously, if you took
        // my suggestion about creating a "Cards" library, then you
        // wouldn't have a "void Main" at all, and this would
        // all go in the application that was the actual game.
    }
}
like image 22
Mike U Avatar answered Jan 22 '23 06:01

Mike U