【问题标题】:C# I have 50+ else if statements for cards, is there a way to make it shorter or do it all at one go?C# 我有 50 多个 else if 卡片语句,有没有办法让它更短或一次性完成?
【发布时间】:2015-11-25 22:19:42
【问题描述】:

我有一个表单,它加载并生成 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 张卡片并在图片框中显示它们?

按照我的方式,这非常耗时。

【问题讨论】:

  • 如果您将图像资源命名为“Spades1、Hearts4”等,您可以通过cardType + cardNum...按名称加载它们。

标签: c# if-statement


【解决方案1】:

有很多方法,但总的来说我会说:使用数组作为查找

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

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

现在您所要做的就是计算正确的索引。由于您没有提供我需要帮助您的尽可能多的信息,我只是猜测它看起来类似于:

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

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

【讨论】:

  • 你的索引器比我的好很多。
  • 谢谢!好吧,你的图像随机化,但我认为 OP 想要随机化两个数字并通过这两个数字找到正确的卡片。
【解决方案2】:

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

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

  • card_Club_1
  • card_Club_2
  • card_Club_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.
    }
}

【讨论】:

  • 我应该在 Program.cs 文件或 Form1.cs 中键入此代码吗?
  • 类(“程序”除外)可以去任何地方。所有这些都是可以进入命名空间的类型。如何使用“Deck”类实现取决于您正在制作的应用程序类型。这只是一系列类的高级设计,以实现构建需要一副纸牌的东西。引导您从头到尾创建整个应用程序会涉及更多内容。
  • 设置一个库来处理这类事情。它还包括(开始)一个处理游戏逻辑的基类。还有一个专门针对“Uno”类型游戏的单独项目。 github.com/tenshino/RainstormStudios/tree/…
  • 仅供参考:我搞砸了我的 Git 存储库,所以这里有一个更新的链接:github.com/tenshino/RainstormStudios/tree/master/…
  • 我知道我接受答案很晚,但我回到这个项目并最终在谷歌上搜索了同样的问题。谢谢!
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    我会重命名您的图像。如果您可以使用 Spade_1、Spade_2 等名称,您可以通过简单的步骤构建文件名。

    var name = cardType + "_" + cardNum;
    

    【讨论】:

      【解决方案5】:

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

      那么对于花色,花色将基于卡片 # / 13 的整数。因此卡片 0-12 例如:黑桃,13-25=红心,26-38 = 方块,39-51 = 梅花。

      数字 13 的模数将是卡片(从 0 到 12)。通过将 1 添加到模数结果,您将从 0-12 作为数组 0 = Ace, 12 = King

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多