嗨,我想计算玩家一只手可以拿到的所有扑克牌组合,并显示所有这些组合。我不在乎有多少对,满屋等。我只想计算玩家可以获得的所有可能的手牌。所以,一手牌由5张牌组成,必须有4种颜色(花色),并且必须重复一个花色。最多4个号码相同,第5张牌的号码必须不同。所有可能的手牌组合的正确结果必须是 2598960(52 高于 5 是 2598960)。我只需要使用我的代码获得正确的结果,但我不知道如何编写算法。
我有card
.cs 类:
class card
{
private int number;
private char suit;
public card(int _number, char _suit)
{
this.number = _number;
this.suit = _suit;
}
public int Number
{
get{return this.number;}
set{this.number = value;}
}
public char Suit
{
get { return this.suit; }
set { this.suit = value; }
}
}
在Program
.cs 类中,我有 Main 和这段代码:
static void Main(string[] args)
{
char[] suits = { '\u2660', '\u2663', '\u2665', '\u2666' }; //Spades, Clubs, Hearts, Diamonds
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int cnt = 0;
List<card> deck = new List<card>();
//making deck
foreach (char suit in suits)
{
foreach (int number in numbers)
{
deck.Add(new card(number, suit));
}
}
foreach (card first in deck)
{
foreach (card second in deck)
{
if (second.Number != first.Number)
{
foreach (card third in deck)
{
if (third.Number != second.Number && third.Number != first.Number)
{
foreach (card fourth in deck)
{
if (fourth.Number != third.Number && fourth.Number != second.Number && fourth.Number != first.Number)
{
foreach (card fifth in deck)
{
if (fifth.Suit != first.Suit && fifth.Suit != second.Suit && fifth.Suit != third.Suit && fifth.Suit != fourth.Suit)
{
//Console.WriteLine("{0}{1} {2}{3} {4}{5} {6}{7} {8}{9}", first.Number, first.Suit, second.Number, second.Suit, third.Number, third.Suit, fourth.Number, fourth.Suit, fifth.Number, fifth.Suit);
cnt++;
}
}
}
}
}
}
}
}
}
Console.WriteLine("Combinations: {0}", cnt);//Result must be: 2598960
}