C # I have 50+ other statements for flashcards, is there a way to make this shorter or do it all in one go?

I have a form that loads and generates 7 different random numbers: from 1-13, 1 is Ace, and 13 is King. After generating 7 different random numbers, he puts each of these random numbers in 7 boxes with pictures. I show image windows using the if statement.

It also cycles through the Peaks, Hearts, Clubs and Diamonds array 13 times.

And My expressions if:

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 and easy way to select 7 random cards and show them in the image window?

This is a very long time how I do it.

+6
source share
5 answers

So many offers other than OOP on how to handle this. Here is my solution, which allows each object to track itself, and will provide an easy way to shuffle the deck and get the image associated with each card (I wrote several card games).

To store actual images, insert them into your project as resource files, named in a certain way:

  • card_Club_1
  • card_Club_2
  • card_Club_3
  • etc...

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

 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. } } 
+4
source

There are many ways, but overall I would say: Use an array as a search

For example, you have an array like

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

Now you just need to calculate the correct index. Since you did not provide as much information as I need to help you with this, I just guess that it will look something like this:

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

As I said, this is the idea. Now you need to find the correct calculation.

+5
source

Put all your objects in an array, then use your random number to select an index from the array.

 private Image[] _cards = new Image[] {ace_of_spades, ace_of_hearts, /* and so on for all of the cards*/ } void YourFunction(Random rnd) { var nextCardIndex = rnd.GetNext(_cards.Length); pictureBox1.Image = _cards[nextCardIndex]; } 
0
source

I would rename your images. If you can have names like Spade_1, Spade_2, etc., you can create a file name in a simple step.

 var name = cardType + "_" + cardNum; 
0
source

Why not randomize from 0-51 (total 52 cards).

Then for the suit, the suit will be based on INTEGER card number 13. Thus, the cards are 0-12 ex: Spades, 13-25 = Hearts, 26-38 = Diamonds, 39-51 = Clubs.

The module of the number 13 will be a card (from 0 to 12). Adding 1 to the module result, you should go from 0-12 in the form of an array 0 = Ace, 12 = King

0
source

All Articles