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:
- private void LoadModule(object sender, System.EventArgs e)
-
- {
-
-
-
- int stopvar, b, c, d, setmark;
-
- string card1, card2, currdir;
-
-
-
-
-
- currdir = Directory.GetCurrentDirectory();
-
- currdir = currdir.Substring(0, currdir.Length - 9);
-
-
-
-
-
- this.SetBounds(1, 1, 1800, 1800);
-
-
-
- this.StartPosition = FormStartPosition.CenterScreen;
-
-
-
-
-
-
-
- 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";
-
-
-
-
-
- for (a = 0; a < 52; a++) shufflevault[a] = " ";
-
-
-
-
-
- Random RandomNumber = new Random();
-
-
-
-
-
- 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 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.
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”.
CONCLUSIONC# 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.