title: Generate all distinct 7 card combinations of a poker hand?
body: <p>I'm trying to generate all the distinct combinations of a poker hand, as described here:</p>
  
  <p><a href="https://stackoverflow.com/questions/3829457/generating-all-5-card-poker-hands">Generating all 5 card poker hands</a></p>
  
  <p>But I keep getting stuck. And when trying NickLarsen's C# answer at above URL I get an unhandled exception error at line 49. (<a href="https://stackoverflow.com/a/3832781/689881">https://stackoverflow.com/a/3832781/689881</a>)</p>
  
  <p>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</p>
  
  <p>Also, I actually want all 7 card combinations (instead of 5).
  For example the first two lines might look like this:</p>
  
  <p>2c2d2h2s3c3d3h<br>
  2c2d2h2s3c3d3s</p>
  
  <p>How do I achieve this? Speed is NOT that important.</p>
  
  <p>Below is the code from NickLarsen (with my modifications) that fails:</p>
  
  <pre><code>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&lt;Card&gt; cards = new List&lt;Card&gt;();
          //cards.Add(new Card() { Rank = 0, Suit = 0 });
          int numHands = GenerateAllHands(cards);
  
          Console.WriteLine(numHands);
          Console.ReadLine();
      }
  
      static int GenerateAllHands(List&lt;Card&gt; cards)
      {
          if (cards.Count == cardsInHand) return 1;
  
          List&lt;Card&gt; possibleNextCards = GetPossibleNextCards(cards);
  
          int numSubHands = 0;
  
          foreach (Card card in possibleNextCards)
          {
              List&lt;Card&gt; possibleNextHand = cards.ToList(); // copy list
              possibleNextHand.Add(card);
              numSubHands += GenerateAllHands(possibleNextHand);
          }
  
          return numSubHands;
      }
  
      static List&lt;Card&gt; GetPossibleNextCards(List&lt;Card&gt; hand)
      {
          int maxRank = hand.Max(x =&gt; x.Rank);
  
          List&lt;Card&gt; result = new List&lt;Card&gt;();
  
          // only use ranks &gt;= max
          for (int rank = maxRank; rank &lt; ranks; rank++)
          {
              List&lt;int&gt; suits = GetPossibleSuitsForRank(hand, rank);
              var possibleNextCards = suits.Select(x =&gt; new Card { Rank = rank, Suit = x });
              result.AddRange(possibleNextCards);
          }
  
          return result;
      }
  
      static List&lt;int&gt; GetPossibleSuitsForRank(List&lt;Card&gt; hand, int rank)
      {
          int maxSuit = hand.Max(x =&gt; x.Suit);
  
          // select number of ranks of different suits
          int[][] card = GetArray(hand, rank);
  
          for (int i = 0; i &lt; suits; i++)
          {
              card[i][rank] = 0;
          }
  
          int[][] handRep = GetArray(hand, rank);
  
          // get distinct rank sets, then find which ranks they correspond to
          IEnumerable&lt;int[]&gt; distincts = card.Distinct(new IntArrayComparer());
  
          List&lt;int&gt; possibleSuits = new List&lt;int&gt;();
  
          foreach (int[] row in distincts)
          {
              for (int i = 0; i &lt; suits; i++)
              {
                  if (IntArrayComparer.Compare(row, handRep[i]))
                  {
                      possibleSuits.Add(i);
                      break;
                  }
              }
          }
  
          return possibleSuits;
      }
  
      class IntArrayComparer : IEqualityComparer&lt;int[]&gt;
      {
          #region IEqualityComparer&lt;int[]&gt; Members
  
          public static bool Compare(int[] x, int[] y)
          {
              for (int i = 0; i &lt; 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&lt;Card&gt; hand, int rank)
      {
          int[][] cards = new int[suits][];
          for (int i = 0; i &lt; suits; i++)
          {
              cards[i] = new int[ranks];
          }
  
          foreach (Card card in hand)
          {
              cards[card.Suit][card.Rank] = 1;
          }
  
          return cards;
      }
  }
  }
  </code></pre>