c#扑克牌组合

时间:2013-11-13 22:37:48

标签: math combinations permutation poker variations

Hy,我想要计算玩家一方面可以获得的所有扑克牌组合并显示所有组合。我不关心有多少双,满屋等。我只想计算玩家可以得到的所有可能的牌。因此,一只手由5张牌组成,必须有4种颜色(套装),一套牌必须重复。最大值是4个数字相同,第5张卡的数量必须不同。所有可能的手组合的正确结果必须是2598960(52以上5是2598960)。我只需要使用我的代码得到正确的结果,但我不知道如何编写算法 我有card。cs class:

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
    }

1 个答案:

答案 0 :(得分:3)

好吧,尽管我很欣赏你正在做的事情,但我觉得这更容易阅读:

int count = 0;
int cards_amount = 52;
for (var first = 0; first < cards_amount-4; first++)
    for (var second = first + 1; second < cards_amount-3; second++)
        for (var third = second+1; third < cards_amount-2; third++)
            for (var fourth = third+1; fourth < cards_amount-1; fourth++)
                for (var fifth = fourth+1; fifth < cards_amount; fifth++)
                    count++;

而不是查看所有重复项和if this card is different from the previous,我所做的是:将所有卡片连成一行。选择第一个,然后选择剩下的第二个,然后选择剩下的第三个......依此类推。

这样,您无需检查倍数,并得到正确答案:)

修改
至于评论......只需在初始化列表后添加此行:

var deck_array = deck.ToArray();

您可能希望在class card上执行此操作:

public override string ToString() {
        // have this print your card number & suit
}

然后,只需更改count++;行:

{
   count ++;
   Console.WriteLine("{0},{1},{2},{3},{4}", deck_array[first], deck_array[second], 
       deck_array[third] , deck_array[fourth] , deck_array[fifth] );
}

解决了......(现在你的卡片知道如何打印自己,而你最后只是打印了手。