C#我有50多张卡片的else-if语句,有没有办法缩短或一次性完成

本文关键字:一次性 有没有 我有 张卡片 语句 else-if | 更新日期: 2023-09-27 18:24:41

我有一个表单,它加载并生成7个不同的随机数,从1-13,1是Ace,13是King。在生成7个不同的随机数后,它将这些随机数中的每一个放入7个图片框中。我正在使用if语句显示图片框。

它还循环通过一系列"黑桃、红心、梅花和钻石",共13次。

我的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

有没有一种简单、简单的方法可以随机挑选7张卡片并将其显示在图片框中?

我这样做非常耗时。

C#我有50多张卡片的else-if语句,有没有办法缩短或一次性完成

有很多方法,但通常我会说:使用数组作为查找

例如,您有一个类似的数组

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

现在你所要做的就是计算正确的指数。由于你没有提供我需要的那么多信息来帮助你,我只是猜测它看起来像这样:

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

就像我说的,这就是背后的想法。现在你必须找到正确的计算方法。

关于如何处理这一问题,有很多非OOP建议。这是我的解决方案,它可以让每个对象跟踪自己,并将提供一种简单的方法来洗牌和获得与每张卡相关的图像(我已经写了一些卡牌游戏)。

为了存储实际的图像,将它们作为资源文件嵌入到您的项目中,以特定的方式命名:

  • 卡片_俱乐部_1
  • 卡_俱乐部2
  • 卡片_俱乐部_3
  • 等等

然后,当您想要一张卡片图像时,只需将套装和价值组合起来,并向资源管理器请求资源名称,如下所示。这种方法需要更多的设置和规划,但会给您更多更干净的代码。你甚至可以在一个单独的项目中完成所有这些,然后通过引用应用程序中的DLL来重用类/资源,你想在那里有一副可用的卡片。

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.
    }
}

将所有项目放入一个数组,然后使用随机数从数组中选择一个索引。

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];
}

我会重命名您的图像。如果您可以使用Spade_1、Spade_2等名称,则可以在一个简单的步骤中建立文件名。

var name = cardType + "_" + cardNum;

为什么不从0-51随机化(总共52张卡)。

然后对于套装,套装将基于第#13张牌的整数。因此,0-12张牌,例如:黑桃,13-25=红心,26-38=钻石,39-51=梅花。

数字13的模数将是卡片(从0-12)。通过在模数结果上加1,您将从0-12变成一个数组0=Ace,12=King