IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#   Print This     
 
Shuffle a Sequentially Ordered Deck of Playing Cards Using C#
 
Posted 13 months ago on 11/13/2020
Take Away:

This article describes C# coding I use to shuffle a deck of playing cards.

KB104920

INTRODUCTION

A long time customer of mine 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 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. privatevoid LoadModule(object sender, System.EventArgs e)  
  2. {  
  3. // declare local variables.    
  4. int stopvar, b, c, d, setmark;  
  5. string card1, card2, currdir;  
  6. // get current working directory of game installation.  
  7. currdir = Directory.GetCurrentDirectory();  
  8. currdir = currdir.Substring(0, currdir.Length - 9);  
  9. // set boundaries of form.
  10. this.SetBounds(1, 1, 1800, 1800);  
  11. // set the start position of the form to the center of the screen.
  12. this.StartPosition = FormStartPosition.CenterScreen;  
  13. // assign sequential playing card designators to ‘cardvault’ character array.
  14. cardvault[0] = "2C";  
  15. cardvault[1] = "2S";  
  16. cardvault[2] = "2H";  
  17. cardvault[3] = "2D";  
  18. cardvault[4] = "3C";  
  19. cardvault[5] = "3S";  
  20. cardvault[6] = "3H";  
  21. cardvault[7] = "3D";  
  22. cardvault[8] = "4C";  
  23. cardvault[9] = "4S";  
  24. cardvault[10] = "4H";  
  25. cardvault[11] = "4D";  
  26. cardvault[12] = "5C";  
  27. cardvault[13] = "5S";  
  28. cardvault[14] = "5H";  
  29. cardvault[15] = "5D";  
  30. cardvault[16] = "6C";  
  31. cardvault[17] = "6S";  
  32. cardvault[18] = "6H";  
  33. cardvault[19] = "6D";  
  34. cardvault[20] = "7C";  
  35. cardvault[21] = "7S";  
  36. cardvault[22] = "7H";  
  37. cardvault[23] = "7D";  
  38. cardvault[24] = "8C";  
  39. cardvault[25] = "8S";  
  40. cardvault[26] = "8H";  
  41. cardvault[27] = "8D";  
  42. cardvault[28] = "9C";  
  43. cardvault[29] = "9S";  
  44. cardvault[30] = "9H";  
  45. cardvault[31] = "9D";  
  46. cardvault[32] = "10C";  
  47. cardvault[33] = "10S";  
  48. cardvault[34] = "10H";  
  49. cardvault[35] = "10D";  
  50. cardvault[36] = "JC";  
  51. cardvault[37] = "JS";  
  52. cardvault[38] = "JH";  
  53. cardvault[39] = "JD";  
  54. cardvault[40] = "QC";  
  55. cardvault[41] = "QS";  
  56. cardvault[42] = "QH";  
  57. cardvault[43] = "QD";   
  58. cardvault[44] = "KC";  
  59. cardvault[45] = "KS";  
  60. cardvault[46] = "KH";  
  61. cardvault[47] = "KD";  
  62. cardvault[48] = "AC";  
  63. cardvault[49] = "AS";  
  64. cardvault[50] = "AH";  
  65. cardvault[51] = "AD";  
  66. // initialize the character array ‘shufflevault’ that will hold
  67. // the shuffled card order.
  68. for(a = 0; a < 52; a++) shufflevault[a] = " ";  
  69. // create object for random number generator for a specified number range.
  70. Random RandomNumber = newRandom();  
  71. // using the above object, generate a random number between 1 and 51.
  72. setmark = RandomNumber.Next(1, 51); 

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 particular playing card are grouped together, i.e. – 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, which 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. // which is also the character array ‘cardvault’.  
  3. b = 0;  
  4. do  
  5. {  
  6. // initialize inner loop variable for stopping the processing.  
  7. stopvar = 0;  
  8. // get the randomly generated number and assign to the variable ‘c’.  
  9. c = setmark;  
  10. // decrement the number in ‘c’ and assign to the variable ‘d’.
  11. d = c - 1;  
  12. do
  13. {  
  14. // if the space in the ‘c’ index of the character
  15. // array ‘shufflevault’ is empty, then assign the
  16. // contents of ‘cardvault[b]’ to ‘shufflevault[c]’.
  17. // increment the outer loop variable ‘b’ by one,
  18. // then assign one to the inner loop stop variable, ‘stopvar’.
  19. if(shufflevault[c] == " ")  
  20. {  
  21. shufflevault[c] = cardvault[b];  
  22. b++;  
  23. stopvar = 1;  
  24. }  
  25. // if the space in the ‘d’ index of the character
  26. // array ‘shufflevault’ is empty and the 
  27. // ‘stopvar’ variable is still zero, then assign 
  28. // the contents of ‘cardvault[b]’ to 
  29. // ‘shufflevault[d]’. increment the outer loop 
  30. // variable ‘b’ by one, then assign one to the 
  31. // inner loop stop variable, ‘stopvar’.
  32. if(shufflevault[d] == " " && stopvar == 0)  
  33. {  
  34. shufflevault[d] = cardvault[b];  
  35. b++;  
  36. stopvar = 1;  
  37. }  
  38. // increment ‘c’ variable and decrement the ‘d’ variable.
  39. c++;  
  40. d--;  
  41. // what this does is assign or shuffle playing    
  42. // cards from their sequential positions to random 
  43. // positions in the deck by incrementing ‘c‘ and 
  44. // decrementing ‘d‘ while ‘d‘ is greater
  45. // than or equal to 0 and ‘c‘ is less than or equal 
  46. // to 51. then the inner loop is exited and a new 
  47. // random number is selected between 1 and 51   and 
  48. // the process is repeated while the ‘b‘ variable
  49. // is less than 52 in the outer loop.                     
  50. // end the inner loop.
  51. while(d >= 0 && c <= 51 && stopvar == 0);  
  52. // generate a random number between 1 and 51.
  53. setmark = RandomNumber.Next(1, 51);  
  54. // end the outer loop.
  55. 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. 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. // ‘shufflevault’ character array, which is also  
  3. // the shuffled card deck.   
  4. // player1vault, or player 1, receives shuffled cards from  
  5. // from indexes 0 and 6 in the ‘shufflevault’  
  6. // shuffled card deck, so to speak.  
  7. player1vault[0] = shufflevault[0];  
  8. player1vault[1] = shufflevault[6];  
  9. // player2vault, or player 2, receives shuffled cards from
  10. // from indexes 1 and 7 in the ‘shufflevault’ shuffled card deck.
  11. player2vault[0] = shufflevault[1];  
  12. player2vault[1] = shufflevault[7];  
  13. // player3vault, or player 3, receives shuffled cards from
  14. // from indexes 2 and 8 in the ‘shufflevault’
  15. // shuffled card deck.
  16. player3vault[0] = shufflevault[2];  
  17. player3vault[1] = shufflevault[8];  
  18. // player4vault, or player 4, receives shuffled cards from
  19. // from indexes 3 and 9 in the ‘shufflevault’
  20. // shuffled card deck.
  21. player4vault[0] = shufflevault[3];  
  22. player4vault[1] = shufflevault[9];  
  23. // player5vault, or player 5, receives shuffled cards from
  24. // from indexes 4 and 10 in the ‘shufflevault’
  25. // shuffled card deck.
  26. player5vault[0] = shufflevault[4];  
  27. player5vault[1] = shufflevault[10];  
  28. // player6vault, or player 6, is the dealer. the cards
  29. // assigned to the dealer will be used to
  30. // to designate ‘card1’ and ‘card2’ as noted
  31. // below. They come from indexes 5 and 11 in the 
  32. // ‘shufflevault’ shuffled card deck.
  33. player6vault[0] = shufflevault[5];  
  34. player6vault[1] = shufflevault[11];  
  35. // the ‘placeindeck’ variable marks the spot in the
  36. // shuffled deck where the undistributed cards begin.
  37. placeindeck = 12;  
  38. // this variable is initially set to 0 meaning the hold
  39. // cards have not been revealed; set to 1 afterwards.
  40. p1 = 0;  
  41. // initialize shuffled deck indexes for dealer and 
  42. // variables for holding bitmap image file names of dealer‘s cards.a = 5;  
  43. b = 11;  
  44. card1 = "";  
  45. card2 = "";  
  46. // here, the path or current directory is concatenated to
  47. // the bitmap file name associated with the 2 character 
  48. // code contained in the ‘a’ index of the ‘shufflevault’ 
  49. // variable. this in turn is assigned to ‘card1’.
  50. if(shufflevault[a] == "2C")  
  51. {  
  52. card1 = currdir + "2_clubs.bmp";  
  53. }  
  54. if(shufflevault[a] == "2S")  
  55. {  
  56. card1 = currdir + "2_spades.bmp";  
  57. }  
  58. if(shufflevault[a] == "2H")  
  59. {  
  60. card1 = currdir + "2_hearts.bmp";  
  61. }  
  62. if(shufflevault[a] == "2D")  
  63. {  
  64. card1 = currdir + "2_diamonds.bmp";  
  65. }  
  66. .  
  67. .  
  68. .  
  69. .  
  70. .  
  71. .  
  72. // here, the path or current directory is concatenated to
  73. // the bitmap file name associated with the 2 character 
  74. // code contained in the ‘b’ index of the ‘shufflevault’ 
  75. // variable. this in turn is assigned to ‘card2’.
  76. if(shufflevault[b] == "2C")  
  77. {  
  78. card2 = currdir + "2_clubs.bmp";  
  79. }  
  80. if(shufflevault[b] == "2S")  
  81. {  
  82. card2 = currdir + "2_spades.bmp";  
  83. }  
  84. if(shufflevault[b] == "2H")  
  85. {  
  86. card2 = currdir + "2_hearts.bmp";  
  87. }  
  88. if(shufflevault[b] == "2D")  
  89. {  
  90. card2 = currdir + "2_diamonds.bmp";  
  91. }  
  92. .  
  93. .  
  94. .  
  95. .  
  96. .  
  97. .  
  98. (remaining code forsetting up the playing screen follows…..)  

CONCLUSION

C# is one of the most modern and versatile programming languages. I have used C Sharp 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.


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P195A1
Enter key:
Article Contributed By NE Ohio Computer Guy:

Please visit my software developer website for more information about my services. I offer application development as well as Android app coding services. My developer skills are best suited to dealing with custom software projects. I can perform programming for Corel Paradox as well as C# and PHP.

In my local area of northeast Ohio, I can cater to computer repair and "fix my computer" issues. And don‘t forget to check out my YouTube Channel. It is full of cool videos and has something for everyone!

Use my contact web page today to reach me about any software design ideas you have.

Visit Profile

 KB Article #104920 Counter
226
Since 11/13/2020
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2021 Prestwood IT Solutions.   [Security & Privacy]