SoFunction
Updated on 2025-03-07

Several ways to implement playing cards sorting in C# code

In poker games, you can always use a lot of hand sorting. I have summarized several methods for reference and recorded them by the way to facilitate future use.

This I made is a deck of playing cards represented by (1-13: Spades A-K || 14 - 26: Red Peaches || 27 - 39: Plum Blossoms || 39 - 52: Square || 53.54: Xiao Wang. King), so that the array is divided by 13 equals the poker suit (such as: 25/13 = 2 is red), and the array value is modulo equal to the poker points (such as: 25%13 = 12 is Q), so 25 represents the playing cards of Red Peach Q.

When dealing with special rules, write a list separately and just form a group.

For example: When Lai Zi is fighting against Landlord, after selecting Lai Zi cards, the opponent needs to sort again. Then new List saves Lai Zi cards. After selecting Lai Zi cards, save them in the list, call sorting again, and then grouping can be achieved. The array of hand sorting you want can be displayed in some form.

On code:

 //Parameter: Array of card values ​​to be sorted & array length public int[] PaiXu(int[] card, int number = 0) 
 {
    //(" ... sort card value suits for hand cards ... ... ");    if (number == 0){ number = ; }
    if ( == 0){ return card; }
    // ============Sorted by card value =====================    int temp = 0;
    for (int i = 0; i < ; i++) //Bubbling sort... from large to small    {
      for (int j = 0; j <  - 1 - i; j++)
      {
        if (card[j] < card[j + 1])
        {
          temp = card[j];
          card[j] = card[j + 1];
          card[j + 1] = temp;
        }
      }
    }    
    List<int> hei = new List<int>();
    List<int> hong = new List<int>();
    List<int> mei = new List<int>();
    List<int> fang = new List<int>();
    List<int> wang = new List<int>();
    for (int i = 0; i < ; i++)
    {
      #region ====== Grouped according to suits..Size Kings A separate group...After subsequent processing of A in suits is done separately ==========      switch (sendFlower(card[i]))
      {
        case 3: //Black Peach          (card[i]);
          break;
        case 2: //Red Peach          (card[i]);
          break;
        case 1: //plum bossom          (card[i]);
          break;
        case 0: //Square film          (card[i]);
          break;
        case 4: //Xiao Wang        case 5: //The King          (card[i]);
          break;
      }
      #endregion
    }
    QuA(hei); // Separate treatment of A     QuA(hong);
    QuA(mei);
    QuA(fang);
    #region ============= Merge Sort Decks=========    List<int> cardlist = new List<int>(); 
    for (int i = 0; i < ; i++)  //king    {
      (wang[i]);
    }
    // ============= Merge Group Square ================    List<int> cardtemp = new List<int>();   
    cardtemp = PaiXuZuPin(hei, hong, mei, fang);
    for (int i = 0; i < ; i++)
    {
      (cardtemp[i]);
    } 
    int[] cards = new int[];
    for (int i = 0; i < ; i++)
    {
      cards[i] = cardlist[i];
    }
    #endregion        
    return cards;   
  } 
  /// <summary>
  /// Take A -- Put A in each suit card in front (...)  /// </summary>
  /// <param name="hei">Fantasy Card</param>  void QuA(List&lt;int&gt; hei)
  {
    if ( == 0) return;
    List&lt;int&gt; cardlist = new List&lt;int&gt;();
    for (int i = 0; i &lt; ; i++) // Add cards to new list    {
      (hei[i]);
    }
    if ( &gt; 2)
    {
      if (hei[ - 2] % 13 == 1)  //If there are two A (two cards)      {
        (0, hei[ - 2]);
        (0, hei[ - 1]);
        for (int i = 0; i &lt; ; i++)
        {
          hei[i] = cardlist[i];
        }
        return;
      }
    }    
    if (hei[ - 1] % 13 == 1)  //If there is an A    {
      (0, hei[ - 1]);
    }
    for (int i = 0; i &lt; ; i++)
    {
      hei[i] = cardlist[i];
    }   
  }
 /// &lt;summary&gt;
  /// group according to the order of the deck  /// &lt;/summary&gt;
 public List&lt;int&gt; PaiXuZuPin(List&lt;int&gt; one, List&lt;int&gt; two, List&lt;int&gt; three, List&lt;int&gt; four)
  {
    List&lt;int&gt; cardlist = new List&lt;int&gt;();
    for (int i = 0; i &lt; ; i++)  
    {
      (one[i]);
    }
    for (int i = 0; i &lt; ; i++)  
    {
      (two[i]);
    }
    for (int i = 0; i &lt; ; i++)  
    {
      (three[i]);
    }
    for (int i = 0; i &lt; ; i++)    
    {
      (four[i]);
    }
    return cardlist;
  }
  /// &lt;summary&gt;
  /// Choose the suit according to the card value 5: King | 4: Xiao Wang | 3: Spades | 2: Red Peaches | 1: Plum Blossoms | 0: Square Pieces  /// &lt;/summary&gt;
  /// &lt;param name="card"&gt;&lt;/param&gt;
  public int sendFlower(int card)
  {
    if (card &gt;= 1 &amp;&amp; card &lt;= 13)
    {
      return 3;
    }else if (card &gt;= 14 &amp;&amp; card &lt;= 26)
    {
      return 2;
    }
    else if (card &gt;= 27 &amp;&amp; card &lt;= 39)
    {
      return 1;
    }
    else if (card &gt;= 40 &amp;&amp; card &lt;= 52)
    {
      return 0;
    }
    else if (card == 53)
    {
      return 4;
    }
    return 5;
  }

PS: The code is for reference only, optimized and processed by yourself

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links