Question

Hy, I want to count of all combinations of poker cards that player can get in one hand and to display all that combinations. I don't care about how many pairs, full houses etc. are there. I just want to count all possible hands that player can get. So, one hand is made of 5 cards, there must be 4 colors (suits) and one suit must repeat. Maximum is that 4 numbers are same, number of 5th card must be different. Correct result of all possible hand combinations must be 2598960 (52 above 5 is 2598960). I just need to get correct result using my code, but I don't know how to write algorithm.
I have 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; }
    }

}

and in Program.cs class, I have Main and this code:

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
    }
Was it helpful?

Solution

Well, as much as I appreciate what you're doing, I find this easier to read:

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++;

Instead of looking at all the duplicates, and the if this card is different from the previous, what I do is: Put all cards in a row. Select the first, and then the second from the remaining, and then the third from the remaining ... and so on.

This way, you don't need to check for multiples, and you get your right answer :)

Edit:
As for the comment ... Just add this line after you initialize your list:

var deck_array = deck.ToArray();

You might want to do this on your class card:

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

And then, just change the count++; line with this:

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

Solved ... (now your cards know how to print themselves, and you're just printing the hand at the end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top