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