Shuffle A Sequentially Ordered Deck Of Playing Cards Using C#

INTRODUCTION

A long time customer of mine, who was also a gambling fan, asked me to create a non-wagering version of the Texas Hold’Em poker game. At this time, I was just beginning to learn C# coding. I needed to move away from the C++ programming code I had done for years and learn this more modern and robust software development platform. I thought this would be a “low pressure way to get my C# sea legs”. The example I will discuss is for a 6 player Texas Hold’Em poker game including the dealer. This logic has also been expanded to include 9 and 10 player games.

THE FORM’S “LOAD” EVENT FIRES THE CARD DECK SHUFFLING CODE

Upon entering the 6 player Windows Forms form in the application, the “Load” event is triggered to start the card deck shuffling code. But first, we need to do some initialization chores as detailed in the code snippet below: 
  1. private void LoadModule(object sender, System.EventArgs e)  
  2.   
  3. {  
  4.   
  5.     // declare local variables.   
  6.   
  7.     int stopvar, b, c, d, setmark;  
  8.   
  9.     string card1, card2, currdir;  
  10.   
  11.   
  12.   
  13.     // get current working directory of game installation  
  14.   
  15.     currdir = Directory.GetCurrentDirectory();  
  16.   
  17.     currdir = currdir.Substring(0, currdir.Length - 9);  
  18.   
  19.   
  20.   
  21.     // set boundaries of form.  
  22.   
  23.     this.SetBounds(1, 1, 1800, 1800);  
  24.   
  25.     // set the start position of the form to the center of the screen.  
  26.   
  27.     this.StartPosition = FormStartPosition.CenterScreen;  
  28.   
  29.   
  30.   
  31.     // assign sequential playing card designators to ‘cardvault’  
  32.   
  33.     // character array.  
  34.   
  35.     cardvault[0] = "2C";  
  36.   
  37.     cardvault[1] = "2S";  
  38.   
  39.     cardvault[2] = "2H";  
  40.   
  41.     cardvault[3] = "2D";  
  42.   
  43.     cardvault[4] = "3C";  
  44.   
  45.     cardvault[5] = "3S";  
  46.   
  47.     cardvault[6] = "3H";  
  48.   
  49.     cardvault[7] = "3D";  
  50.   
  51.     cardvault[8] = "4C";  
  52.   
  53.     cardvault[9] = "4S";  
  54.   
  55.     cardvault[10] = "4H";  
  56.   
  57.     cardvault[11] = "4D";  
  58.   
  59.     cardvault[12] = "5C";  
  60.   
  61.     cardvault[13] = "5S";  
  62.   
  63.     cardvault[14] = "5H";  
  64.   
  65.     cardvault[15] = "5D";  
  66.   
  67.     cardvault[16] = "6C";  
  68.   
  69.     cardvault[17] = "6S";  
  70.   
  71.     cardvault[18] = "6H";  
  72.   
  73.     cardvault[19] = "6D";  
  74.   
  75.     cardvault[20] = "7C";  
  76.   
  77.     cardvault[21] = "7S";  
  78.   
  79.     cardvault[22] = "7H";  
  80.   
  81.     cardvault[23] = "7D";  
  82.   
  83.     cardvault[24] = "8C";  
  84.   
  85.     cardvault[25] = "8S";  
  86.   
  87.     cardvault[26] = "8H";  
  88.   
  89.     cardvault[27] = "8D";  
  90.   
  91.     cardvault[28] = "9C";  
  92.   
  93.     cardvault[29] = "9S";  
  94.   
  95.     cardvault[30] = "9H";  
  96.   
  97.     cardvault[31] = "9D";  
  98.   
  99.     cardvault[32] = "10C";  
  100.   
  101.     cardvault[33] = "10S";  
  102.   
  103.     cardvault[34] = "10H";  
  104.   
  105.     cardvault[35] = "10D";  
  106.   
  107.     cardvault[36] = "JC";  
  108.   
  109.     cardvault[37] = "JS";  
  110.   
  111.     cardvault[38] = "JH";  
  112.   
  113.     cardvault[39] = "JD";  
  114.   
  115.     cardvault[40] = "QC";  
  116.   
  117.     cardvault[41] = "QS";  
  118.   
  119.     cardvault[42] = "QH";  
  120.   
  121.     cardvault[43] = "QD";   
  122.   
  123.     cardvault[44] = "KC";  
  124.   
  125.     cardvault[45] = "KS";  
  126.   
  127.     cardvault[46] = "KH";  
  128.   
  129.     cardvault[47] = "KD";  
  130.   
  131.     cardvault[48] = "AC";  
  132.   
  133.     cardvault[49] = "AS";  
  134.   
  135.     cardvault[50] = "AH";  
  136.   
  137.     cardvault[51] = "AD";  
  138.   
  139.     // initialize the character array ‘shufflevault’ that  
  140.   
  141.     // will hold the shuffled card order.  
  142.   
  143.     for (a = 0; a < 52; a++) shufflevault[a] = " ";  
  144.   
  145.     // create object for random number generator for  
  146.   
  147.     // a specified number range.  
  148.   
  149.     Random RandomNumber = new Random();  
  150.   
  151.     // using the above object, generate a random number  
  152.   
  153.     // between 1 and 51.  
  154.   
  155.     setmark = RandomNumber.Next(1, 51);  
  156.   

 
Notice the card deck character array “cardvault”, is in sequential order beginning with the lowest value of “2” and then increasing to the highest value of “Ace”. Also, all four suites of a specific playing card are grouped together, in other words – “AC, AS, AH, AD” translates to “Ace of Clubs, Ace of Spades, Ace of Hearts and Ace of Diamonds”. The same methodology applies to all the other cards.

BEGIN SHUFFLING THE CARD DECK

I use 2 nested “do-while” loops to shuffle the sequentially ordered playing card deck. The outer do-while loop cycles through all 52 cards in the sequentially ordered card deck. With each pass through the outer do-while loop, a random number between 1 and 51 is generated.

The inner do-while loop does the actual shuffling of the playing cards, using the randomly generated number, that is stored in the variable “setmark”. Then the random number is assigned to a variable, “c”. The random number is decremented and that is assigned to a variable, “d”. The inner do-while loop will cycle around while the “stopvar” variable is 0 and the variable “d” is greater than or equal to 0 and the variable “c” is less than or equal to 51.

While cycling through the inner do-while loop, as many as 2 indexes in the shuffled order card deck character array “shufflevault” may be updated. Using the “c” and “d” variables as array indexes, “shufflevault[c]” and/or “shufflevault[d]” may be updated with “cardvault[b]” if the following conditions are met.

Using the variable “c” as an array index in the character array “shufflevault”, it will be updated with the value in the character array “cardvault” using the outer do-while loop variable “b” as its index if “shufflevault[c]” is empty. The outer do-while loop variable “b” will be incremented and the variable “stopvar” will be set to 1.

Using the variable “d” as an array index in the character array “shufflevault”, it will be updated with the value in the character array “cardvault” using the outer do-while loop variable “b” as its index if “shufflevault[d]” is empty and the “stopvar” variable is still set to 0. The outer do-while loop variable “b” will be incremented and the variable “stopvar” will be set to 1.

The variable “c” will be incremented and the variable “d” will be decremented before the inner do-while loop cycles around and repeats its logic.

  1. // begin the outer loop to shuffle the cards in the deck,  
  2.   
  3. // which is also the character array ‘cardvault’.  
  4.   
  5. b = 0;  
  6.   
  7. do  
  8.   
  9. {  
  10.   
  11.     // initialize inner loop variable for stopping  
  12.   
  13.     // the processing.  
  14.   
  15.     stopvar = 0;  
  16.   
  17.     // get the randomly generated number and assign to  
  18.   
  19.     // the variable ‘c’.  
  20.   
  21.     c = setmark;  
  22.   
  23.     // decrement the number in ‘c’ and assign to the  
  24.   
  25.     // variable ‘d’.  
  26.   
  27.     d = c - 1;  
  28.   
  29.     do  
  30.   
  31.     {  
  32.   
  33.   
  34.   
  35.         // if the space in the ‘c’ index of the character  
  36.   
  37.         // array ‘shufflevault’ is empty, then assign the  
  38.   
  39.         // contents of ‘cardvault[b]’ to ‘shufflevault[c]’.  
  40.   
  41.         // increment the outer loop variable ‘b’ by one, then  
  42.   
  43.         // assign one to the inner loop stop variable, ‘stopvar’.  
  44.   
  45.         if (shufflevault[c] == " ")  
  46.   
  47.         {  
  48.   
  49.             shufflevault[c] = cardvault[b];  
  50.   
  51.             b++;  
  52.   
  53.             stopvar = 1;  
  54.   
  55.         }  
  56.   
  57.   
  58.   
  59.         // if the space in the ‘d’ index of the character  
  60.   
  61.         // array ‘shufflevault’ is empty and the   
  62.   
  63.         // ‘stopvar’ variable is still zero, then assign the  
  64.   
  65.         // contents of ‘cardvault[b]’ to ‘shufflevault[d]’.  
  66.   
  67.         // increment the outer loop variable ‘b’ by one, then  
  68.   
  69.         // assign one to the inner loop stop variable, ‘stopvar’.  
  70.   
  71.         if (shufflevault[d] == " " && stopvar == 0)  
  72.   
  73.         {  
  74.   
  75.             shufflevault[d] = cardvault[b];  
  76.   
  77.             b++;  
  78.   
  79.             stopvar = 1;  
  80.   
  81.         }  
  82.   
  83.   
  84.   
  85.         // increment ‘c’ variable and decrement  
  86.   
  87.         // the ‘d’ variable.  
  88.   
  89.         c++;  
  90.   
  91.         d--;  
  92.   
  93.   
  94.   
  95.         // what this does is assign or "shuffle" playing cards from  
  96.   
  97.         // their sequential positions to random positions in the "deck"  
  98.   
  99.         // by incrementing 'c' and decrementing 'd' while 'd' is greater  
  100.   
  101.         // than or equal to 0 and 'c' is less than or equal to 51.  
  102.   
  103.         // then the inner loop is exited and a new random number is selected  
  104.   
  105.         // between 1 and 51 and the process is repeated while the 'b' variable  
  106.   
  107.         // is less than 52 in the outer loop.   
  108.   
  109.   
  110.   
  111.         // end the inner loop.  
  112.   
  113.     } while (d >= 0 && c <= 51 && stopvar == 0);  
  114.   
  115.   
  116.   
  117.     // generate a random number between 1 and 51.  
  118.     setmark = RandomNumber.Next(1, 51);  
  119.   
  120.   
  121.   
  122.     // end the outer loop.  
  123.   
  124. while (b < 52); 
 
DEAL TWO SHUFFLED CARDS TO EACH OF SIX PLAYERS

Now that the sequentially ordered card deck in the character array “cardvault” has been “shuffled” into the character array “shufflevault”, it is time to deal 2 shuffled playing cards to each of the 5 players and the dealer. I use 6 character arrays denoted as "player1vault", "player2vault", "player3vault", "player4vault", "player5vault" and "player6vault" as seen in the code snippet below to receive the 2 character codes for specific playing cards from the “shufflevault” character array at specified indexes.

Also, I use a variable I call “placeindeck” that marks the spot where undistributed playing cards begin in the shuffled card deck. After dealing 2 playing cards to each of 6 players, the 13th card in the deck (denoted by 12 in the code) will be the next undistributed card to be used since the first dealt card starts at index 0 in the character array “shuffledeck”. Another variable I use is called “p1”, which is initialized to 0. This means the hold cards have not yet been revealed. After they have, the “p1” variable will be set to 1.

Next, the shuffled card deck indexes for the dealer are initialized (a = 5 and b = 11) as noted below. Also, the string variables for holding the bitmap image file names of playing cards in the current directory are set to “” for the string variables, card1 and card2. After testing for the 2 character code in “shufflevault[a]”, the current directory is concatenated to the specific bitmap image file name that corresponds to the 2 character code in the “if-then” logic comparison. This concatenated expression will be assigned to the “card1” string variable. Likewise with “shufflevault[b]” and “card2”.

  1. // deal 2 cards to each of six players from the  
  2.   
  3. // ‘shufflevault’ character array, which is also  
  4.   
  5. // the shuffled card deck.   
  6.   
  7.   
  8.   
  9. // player1vault, or player 1, receives shuffled cards from  
  10.   
  11. // from indexes 0 and 6 in the ‘shufflevault’  
  12.   
  13. // shuffled card deck, so to speak.  
  14.   
  15. player1vault[0] = shufflevault[0];  
  16.   
  17. player1vault[1] = shufflevault[6];  
  18.   
  19. // player2vault, or player 2, receives shuffled cards from  
  20.   
  21. // from indexes 1 and 7 in the ‘shufflevault’  
  22.   
  23. // shuffled card deck.  
  24.   
  25. player2vault[0] = shufflevault[1];  
  26.   
  27. player2vault[1] = shufflevault[7];  
  28.   
  29. // player3vault, or player 3, receives shuffled cards from  
  30.   
  31. // from indexes 2 and 8 in the ‘shufflevault’  
  32.   
  33. // shuffled card deck.  
  34.   
  35. player3vault[0] = shufflevault[2];  
  36.   
  37. player3vault[1] = shufflevault[8];  
  38.   
  39. // player4vault, or player 4, receives shuffled cards from  
  40.   
  41. // from indexes 3 and 9 in the ‘shufflevault’  
  42.   
  43. // shuffled card deck.  
  44.   
  45. player4vault[0] = shufflevault[3];  
  46.   
  47. player4vault[1] = shufflevault[9];  
  48.   
  49. // player5vault, or player 5, receives shuffled cards from  
  50.   
  51. // from indexes 4 and 10 in the ‘shufflevault’  
  52.   
  53. // shuffled card deck.  
  54.   
  55. player5vault[0] = shufflevault[4];  
  56.   
  57. player5vault[1] = shufflevault[10];  
  58.   
  59. // player6vault, or player 6, is the dealer. the cards  
  60.   
  61. // assigned to the dealer will be used to  
  62.   
  63. // to designate ‘card1’ and ‘card2’ as noted  
  64.   
  65. // below. They come from indexes 5 and 11 in the   
  66.   
  67. // ‘shufflevault’ shuffled card deck.  
  68.   
  69. player6vault[0] = shufflevault[5];  
  70.   
  71. player6vault[1] = shufflevault[11];  
  72.   
  73.   
  74.   
  75. // the ‘placeindeck’ variable marks the spot in the  
  76.   
  77. // shuffled deck where the undistributed cards begin.  
  78.   
  79. placeindeck = 12;  
  80.   
  81.   
  82.   
  83. // this variable is initially set to 0 meaning the hold  
  84.   
  85. // cards have not been revealed; set to 1 afterwards.  
  86.   
  87. p1 = 0;  
  88.   
  89.   
  90.   
  91.   
  92.   
  93.   
  94.   
  95. // initialize shuffled deck indexes for dealer and variables for  
  96.   
  97. // holding bitmap image file names of dealer's cards.  
  98.   
  99. a = 5;  
  100.   
  101. b = 11;  
  102.   
  103. card1 = "";  
  104.   
  105. card2 = "";  
  106.   
  107.   
  108.   
  109.   
  110.   
  111. // here, the path or current directory is concatenated to  
  112.   
  113. // the bitmap file name associated with the 2 character code  
  114.   
  115. // contained in the ‘a’ index of the ‘shufflevault’ variable.  
  116.   
  117. // this in turn is assigned to ‘card1’.  
  118.   
  119. if (shufflevault[a] == "2C")  
  120.   
  121. {  
  122.   
  123.     card1 = currdir + "2_clubs.bmp";  
  124.   
  125. }  
  126.   
  127. if (shufflevault[a] == "2S")  
  128.   
  129. {  
  130.   
  131.     card1 = currdir + "2_spades.bmp";  
  132.   
  133. }  
  134.   
  135. if (shufflevault[a] == "2H")  
  136.   
  137. {  
  138.   
  139.     card1 = currdir + "2_hearts.bmp";  
  140.   
  141. }  
  142.   
  143. if (shufflevault[a] == "2D")  
  144.   
  145. {  
  146.   
  147.     card1 = currdir + "2_diamonds.bmp";  
  148.   
  149. }  
  150.   
  151. .  
  152.   
  153. .  
  154.   
  155. .  
  156.   
  157. .  
  158.   
  159. .  
  160.   
  161. .  
  162.   
  163. // here, the path or current directory is concatenated to  
  164.   
  165. // the bitmap file name associated with the 2 character code  
  166.   
  167. // contained in the ‘b’ index of the ‘shufflevault’ variable.  
  168.   
  169. // this in turn is assigned to ‘card2’.  
  170.   
  171. if (shufflevault[b] == "2C")  
  172.   
  173. {  
  174.   
  175.     card2 = currdir + "2_clubs.bmp";  
  176.   
  177. }  
  178.   
  179. if (shufflevault[b] == "2S")  
  180.   
  181. {  
  182.   
  183.     card2 = currdir + "2_spades.bmp";  
  184.   
  185. }  
  186.   
  187. if (shufflevault[b] == "2H")  
  188.   
  189. {  
  190.   
  191.     card2 = currdir + "2_hearts.bmp";  
  192.   
  193. }  
  194.   
  195. if (shufflevault[b] == "2D")  
  196.   
  197. {  
  198.   
  199.     card2 = currdir + "2_diamonds.bmp";  
  200.   
  201. }  
  202.   
  203. .  
  204.   
  205. .  
  206.   
  207. .  
  208.   
  209. .  
  210.   
  211. .  
  212.   
  213. .  
  214.   
  215. (remaining code for setting up the playing screen follows…..) 
 
CONCLUSION

C# is one of the most modern and versatile programming languages for most any project. To view the entire code for this Windows Forms form “Load” event, please visit my Jazztime in C# Sharp Archive web page. From the list box near the bottom, please click the selection described as “January 1, 2013 Through March 31, 2013” and you will be redirected to the page containing the code listing. I have used C# for a number of application development projects. Please see the rest of my website to learn more about my developer skills as well as my computer repair services.


Similar Articles