0

嗨,我想计算玩家一只手可以拿到的所有扑克牌组合,并显示所有这些组合。我不在乎有多少对,满屋等。我只想计算玩家可以获得的所有可能的手牌。所以,一手牌由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
    }
4

1 回答 1

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

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

于 2013-11-13T23:07:33.980 回答