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

peraiah vallambotlaPosted Nov 20, 2015, 12:03 AM
HI where is the code for setbounds() method and where we declare the array cardvault. Please provide the complete data.
Douglas MillerPosted May 7, 2014, 11:31 PM
that's an interesting idea....
Sam HobbsPosted Apr 25, 2014, 1:36 PM
Thank you for describing your code in such detail. An alternative algorithm for shuffling cards is to randomly pick a card and move the chosen card from the unshuffled to the shuffled. The unshuffled deck would decrease in size therefore the range of random numbers would also decrease.