Generate all distinct 7 card combinations of a pok

2019-07-21 11:04发布

问题:

I'm trying to generate all the distinct combinations of a poker hand, as described here:

Generating all 5 card poker hands

But I keep getting stuck. And when trying NickLarsen's C# answer at above URL I get an unhandled exception error at line 49. (https://stackoverflow.com/a/3832781/689881)

What I want is very simple: to generate all the combinations of cards and print them one line at a time in a simple .txt file

Also, I actually want all 7 card combinations (instead of 5). For example the first two lines might look like this:

2c2d2h2s3c3d3h
2c2d2h2s3c3d3s

How do I achieve this? Speed is NOT that important.

Below is the code from NickLarsen (with my modifications) that fails:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication20
{
struct Card
{
    public int Suit { get; set; }
    public int Rank { get; set; }
}

class Program
{
    static int ranks = 13;
    static int suits = 4;
    static int cardsInHand = 7;

    static void Main(string[] args)
    {
        List<Card> cards = new List<Card>();
        //cards.Add(new Card() { Rank = 0, Suit = 0 });
        int numHands = GenerateAllHands(cards);

        Console.WriteLine(numHands);
        Console.ReadLine();
    }

    static int GenerateAllHands(List<Card> cards)
    {
        if (cards.Count == cardsInHand) return 1;

        List<Card> possibleNextCards = GetPossibleNextCards(cards);

        int numSubHands = 0;

        foreach (Card card in possibleNextCards)
        {
            List<Card> possibleNextHand = cards.ToList(); // copy list
            possibleNextHand.Add(card);
            numSubHands += GenerateAllHands(possibleNextHand);
        }

        return numSubHands;
    }

    static List<Card> GetPossibleNextCards(List<Card> hand)
    {
        int maxRank = hand.Max(x => x.Rank);

        List<Card> result = new List<Card>();

        // only use ranks >= max
        for (int rank = maxRank; rank < ranks; rank++)
        {
            List<int> suits = GetPossibleSuitsForRank(hand, rank);
            var possibleNextCards = suits.Select(x => new Card { Rank = rank, Suit = x });
            result.AddRange(possibleNextCards);
        }

        return result;
    }

    static List<int> GetPossibleSuitsForRank(List<Card> hand, int rank)
    {
        int maxSuit = hand.Max(x => x.Suit);

        // select number of ranks of different suits
        int[][] card = GetArray(hand, rank);

        for (int i = 0; i < suits; i++)
        {
            card[i][rank] = 0;
        }

        int[][] handRep = GetArray(hand, rank);

        // get distinct rank sets, then find which ranks they correspond to
        IEnumerable<int[]> distincts = card.Distinct(new IntArrayComparer());

        List<int> possibleSuits = new List<int>();

        foreach (int[] row in distincts)
        {
            for (int i = 0; i < suits; i++)
            {
                if (IntArrayComparer.Compare(row, handRep[i]))
                {
                    possibleSuits.Add(i);
                    break;
                }
            }
        }

        return possibleSuits;
    }

    class IntArrayComparer : IEqualityComparer<int[]>
    {
        #region IEqualityComparer<int[]> Members

        public static bool Compare(int[] x, int[] y)
        {
            for (int i = 0; i < x.Length; i++)
            {
                if (x[i] != y[i]) return false;
            }

            return true;
        }

        public bool Equals(int[] x, int[] y)
        {
            return Compare(x, y);
        }

        public int GetHashCode(int[] obj)
        {
            return 0;
        }

        #endregion
    }

    static int[][] GetArray(List<Card> hand, int rank)
    {
        int[][] cards = new int[suits][];
        for (int i = 0; i < suits; i++)
        {
            cards[i] = new int[ranks];
        }

        foreach (Card card in hand)
        {
            cards[card.Suit][card.Rank] = 1;
        }

        return cards;
    }
}
}

回答1:

This is because you have commented out //cards.Add(new Card() { Rank = 0, Suit = 0 });. Your cards list is empty, and your code cannot find max of an empty array - this is predictable.



回答2:

I am a little late to the party, but had the same need (for 5 card poker hands). Also working from Nick Larsen's (seemingly imperfect, since I also get the wrong number) answer on the other thread, just add a method to get the name of the card (I am sure someone could do this more elegantly, but it works):

    static string GetCardName(Card card)
    {
        string cardName;
        string cardFace;
        string cardSuit;

        switch (card.Rank)
        {
            case 0:
                cardFace = "2";
                break;
            case 1:
                cardFace = "3";
                break;
            case 2:
                cardFace = "4";
                break;
            case 3:
                cardFace = "5";
                break;
            case 4:
                cardFace = "6";
                break;
            case 5:
                cardFace = "7";
                break;
            case 6:
                cardFace = "8";
                break;
            case 7:
                cardFace = "9";
                break;
            case 8:
                cardFace = "10";
                break;
            case 9:
                cardFace = "J";
                break;
            case 10:
                cardFace = "Q";
                break;
            case 11:
                cardFace = "K";
                break;
            default:
                cardFace = "A";
                break; 
        }

        switch (card.Suit)
        {
            case 0:
                cardSuit = "H";
                break;
            case 1:
                cardSuit = "D";
                break;
            case 2:
                cardSuit = "S";
                break;
            default:
                cardSuit = "C";
                break;
        }

        cardName = cardFace + cardSuit;

        return cardName;
    }

Then, use that in a for loop so you can print it out or whatever you need:

    static void Main(string[] args)
    {
        List<Card> cards = new List<Card>();
        cards.Add(new Card() { Rank = 0, Suit = 0 });
        int numHands = GenerateAllHands(cards);
        int counter = 0;

        Console.WriteLine(numHands);
        Console.WriteLine(possibleHands.Count);

        foreach (Hand hand in possibleHands)
        {
            counter += 1;

            foreach (Card card in hand.Cards)
            {
                hand.HandString += GetCardName(card) + " ";
            }

            hand.HandString = hand.HandString.Trim();
        }

        Console.ReadLine();
    }


回答3:

This runs in about 3 seconds
Why write them out to a text file
You can generate them faster than you could read from a file

    public void PokerHands7from52()
    {
        for (byte i = 0; i < 52; i++)
            Debug.WriteLine("rank " + i % 13 + "  suite " + i / 13);

        Stopwatch sw = new Stopwatch();
        sw.Start();
        int counter = 0;
        for (int i = 51; i >= 6; i--)
        {
            for (int j = i - 1; j >= 5; j--)
            {
                for (int k = j - 1; k >= 4; k--)
                {
                    for (int m = k - 1; m >= 3; m--)
                    {
                        for (int n = m - 1; n >= 2; n--)
                        {
                            for (int p = n - 1; p >= 1; p--)
                            {
                                for (int q = p - 1; q >= 0; q--)
                                {
                                    // the 7 card are i, j, k, m, n, p, q
                                    counter++;
                                    if (counter % 10000000 == 0)
                                        Debug.WriteLine(counter.ToString("N0") + " " + sw.ElapsedMilliseconds.ToString("N0"));
                                }
                            } 
                        }

                    }
                }
            }
        }
        sw.Stop();
        System.Diagnostics.Debug.WriteLine("counter " + counter.ToString("N0") + "  should be 133,784,560");
        System.Diagnostics.Debug.WriteLine("sw " + sw.ElapsedMilliseconds.ToString("N0"));
    }
}