【问题标题】:C# assigning value to arrayC#给数组赋值
【发布时间】:2015-10-10 08:24:55
【问题描述】:

我正在尝试编写一个程序,将 13 张扑克牌从王牌洗牌。发 2 张牌并将分配给每张牌的值相加。 ace = 11,king = 10,jack = 10,queen = 10,10 = 10,9 = 9,8 = 8 等等......有点像二十一点。

到目前为止,我只能洗牌并随机打印出两张牌,但我不知道如何为每张牌分配一个值,将它们相加并打印出来。例如,如果我的两张随机牌是国王和八,那么我希望程序打印出来..

国王

18

这就是我得到的......

        static void Main(string[] args)
    {
        string[] Cards = new string[] {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "jack", "Queen", "King", "Ace"};

        for (int x = 0; x < 100; x++) // looping the shuffle 100 times to maximize the randomness
        {
            for (int i = Cards.Length; i > 0; i--) //shuffle
            {
                string temp;
                Random random = new Random();
                int r = random.Next(i);
                temp = Cards[r];
                Cards[r] = Cards[i-1];
                Cards[i-1] = temp;
            }  
        }
        Console.WriteLine(Cards[0]); //first random card
        Console.WriteLine(Cards[1]); //second random card
        Console.ReadKey();
    }

【问题讨论】:

  • 创建一个存储名称和值的Card 类。或者,创建一个在名称和值之间映射的 Dictionary&lt;string, int&gt;
  • 如上所述,您应该使用Collection,例如KeyValuePairDictionary
  • 在循环中调用Random random = new Random(); 将导致许多(如果不是全部)随机数相同。您应该只使用Random 的单个实例。

标签: c# arrays random shuffle


【解决方案1】:

我会这样做:

var cards = new Dictionary<string, int>()
{
    { "Two", 2 }, { "Three", 3 }, { "Four", 4 }, { "Five", 5 }, { "Six", 6 },
    { "Seven", 7 }, { "Eight", 8 }, { "Nine", 9 }, { "Ten", 10 }, { "Jack", 10 },
    { "Queen", 10 }, { "King", 10 }, { "Ace", 11 },
};

var random = new Random();

var selected = cards.OrderBy(c => random.Next()).Take(2).ToArray();

foreach (var card in selected)
{
    Console.WriteLine(card.Key);
}

Console.WriteLine(selected.Sum(c => c.Value));

运行这个,我得到(例如):

Two
Ten
12

现在,想了解更多关于您现有代码的信息。

在循环中调用Random random = new Random(); 将导致许多(如果不是全部)随机数相同。您应该只使用Random 的单个实例。

不需要for (int x = 0; x &lt; 100; x++) 循环,因为for (int i = Cards.Length; i &gt; 0; i--) 循环的单次通过就足以随机化卡片。

【讨论】:

    【解决方案2】:

    要获得一个很好的纸牌游戏编程示例,请在 Google 搜索《Beginning Visual C# 2012》一书中的 KarliCards(您可以轻松找到完整的 PDF,但我不会发布链接,因为我不确定是否这是合法的)。它有很多东西,比如如何在类或结构上使用常规运算符 (+)。

    无论如何,您正在寻找的是一个枚举。它与 Rob 建议的 Dictionary(string)(int) 非常相似(我不确定如何编写三角括号)。以下是其工作原理的示例:

        enum CardValue
        {
            One = 1,
            Two = 2,
            Three = 3,
            Four = 4
        }
    
        static void Main(string[] args)
        {
            int myInt = (int)CardValue.One;
            Console.WriteLine("output should be 1: " + myInt);
    
            int mySum = (int)CardValue.One + (int)CardValue.Three;
            Console.WriteLine("output should be 4: " + mySum);
            Console.ReadKey();
        }
    

    我的第一语言是 Perl,所以我倾向于将所有内容都视为结构而不是类。总是有不止一种方法可以做到这一点......

        public enum CardSuits
        {
            Clubs,
            Spades,
            Hearts,
            Diamonds
        }
    
        public enum CardValues
        {
            Ace = 1,
            Two = 2,
            Three = 3,
            Four = 4
        }
    
        public struct Card
        {
            public CardValues Value; // Card.Value = CardValues.Ace
            public CardSuits Suit; // Card.Suit = CardSuits.Spades
    
            public override string ToString()
            {
                // Card.ToString() == "Ace of Spades"
                return String.Format(Value + " of " + Suit); 
            }
    
            public string ToStringAsInteger()
            {
                // Card.ToStringAsInteger() == "1 of Spades"
                return String.Format((int)Value + " of " + Suit); 
            }
        }
    
        static void Main(string[] args)
        {
            Card AceOfSpades = new Card();
            AceOfSpades.Value = CardValues.Ace;
            AceOfSpades.Suit = CardSuits.Spades;
    
            Card TwoOfClubs = new Card();
            TwoOfClubs.Value = CardValues.Two;
            TwoOfClubs.Suit = CardSuits.Clubs;
    
            int mySum = (int)AceOfSpades.Value + (int)TwoOfClubs.Value;
            Console.WriteLine("Sum of Ace (1) and Two (2) is: " + mySum); // should be 3
            Console.WriteLine("output of AceOfSpades.ToString() is: " + AceOfSpades.ToString());
            Console.WriteLine("output of AceOfSpades.ToStringAsInteger is: " + AceOfSpades.ToStringAsInteger());
    
            Console.ReadKey();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2010-11-09
      相关资源
      最近更新 更多