2015-07-21 473 views
0

我正试图编写一个程序,将13张扑克牌从王牌转换为王牌。处理2张卡片并合计分配给每张卡片的值。 ace = 11,king = 10,jack = 10,queen = 10,ten = 10,nine = 9,eight = 8等等......有点像二十一点。C#给数组赋值

到目前为止,我只能随机洗牌并随机打印两张牌,但我不知道如何为每张牌分配一个值,并将其添加并打印出来。例如,如果我的两个随机卡是国王和八的话,我想程序打印出来..

这里是我得到的...

 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(); 
    } 
+3

创建一个存储名称和值的'Card'类。或者,创建一个名称和值之间映射的“字典”。 – Rob

+0

如上所述,您应该使用“集合”,如“KeyValuePair”或“Dictionary”。 – Greg

+0

在循环中调用'Random random = new Random();'会导致很多(如果不是全部的话)随机数是相同的。你应该只使用一个“Random”实例。 – Enigmativity

回答

0

对于编程纸牌游戏的一个很好的例子,从本书开始Visual C#2012(你可以很容易地找到一个完整的PDF,但我不会发布的谷歌搜索KarliCards一个链接,因为我不知道它是否合法)。它有很多东西,比如如何在类或结构等东西上使用常规运算符(+)。

无论如何,你要找的是一个枚举。这与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(); 
    } 
0

这是我会怎么做:

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 < 100; x++)循环,因为for (int i = Cards.Length; i > 0; i--)循环的单个循环足以随机化卡片。