This week I will teach you how to print out playing 17 times betting $8 each time, total bet $136. You will start by laying down your 8$ chips and watching the fun. Then you will see the winning pattern and be able to write the correct code to replicate it. I will use C# to write this program, but you will be able to apply what you learn to any other language. Let’s get started.
The Setup
First, you will need to download and install the free Visual Studio.NET 2017 on your computer. You will also need to download the C# language and create a new project in Visual Studio.NET. Let’s call our project “CS17” for short.
Now that you have everything downloaded and installed, let’s move on to the programming. First, delete everything from your previous attempt at writing this program and overwrite in your current Visual Studio.NET code editor.
The Basics
To start up, open up a new.cs file in your current Visual Studio.NET code editor and paste the following code:
- (1)
- (2)
- 17 Times
- Betting $8 Each Time
- (4)
- Total Bet $136
What this program does is take an integer number and multiply it by 17 and then add $8 to that product. Let’s break this down. The (1)
is just a label. It is used to identify this line of code as part of a group and make it easier to find later. The function below, multiplyBy17
, is what actually performs the integer multiplication. You will learn more about functions in a second.
The next line of code, (2)
, simply checks to make sure that the user inputs a value between 1 and 17, inclusive. If the user inputs anything else, it will tell them that they need to enter a number between 1 and 17 because it is required for the program to operate properly. The &&
operator is a short-circuit operator. It is used to avoid writing lengthy if/else conditional statements to handle edge cases. In this case, since the user only has to enter a number between 1 and 17, the program will only ever encounter those inputs and avoid checking for every other number.
Advanced Topics
In your current code, you will now need to add the following statements to the bottom of the program:
A counter for the dice game
This is so that you know how many times you have played the game. To do this, add the following line of code:
CurrentCount += 1;
Then, as soon as you hit the return
key, Visual Studio.NET will give you an alert that it has detected that you are breaking out of the loop. Now, you must add another counter so that it can keep track of the number of times you have won. Add the following line of code:
CurrentCount = CurrentCount % 17;
This code will take the last number the user entered (in this case 17) and divide it by 17 and then add one to the result. What this does is reset the counter whenever the user wins. You cannot divide by zero, so if the result is zero, it will add one instead. For example, if the user enters 17 and wins, the result will be 1 and the counter will be 18. You can follow the pattern, but you need to be careful and make sure that you divide by 17 every time instead of just adding one whenever the result is even.
A variable to store the amount won
In order to be able to print out the amount won, you will need to save that information somewhere. Visual Studio.NET makes this very easy. First, add the following line of code to the top of the program (before the loop that multiplies by 17):
int amountWon = 0;
Then, as soon as you hit the return
key, Visual Studio.NET will give you an alert that it has detected that you are breaking out of the loop. Add the following line of code at the bottom of the program:
amountWon = CurrentCount * 8;
As you can see, this code multiplies the amount won by 8 and stores it in the variable amountWon
. Now that you have stored the amount won, you can use that variable to print out the grand total at the end. Add the following line of code at the bottom of the program:
Console.WriteLine($”{amountWon} in {CurrentCount} Play”);
This code will write out the value of the amountWon
variable. You cannot use a variable that has not been declared yet, so make sure that you add the following line of code at the top of your program:
- var amountWon = 0;
Now, if you test your program by entering random numbers and hitting enter
, you should see the following output on the console:
- 9
- 18
- 10
- 11
- 17
- 12
- 16
- 15
- 14
- 13
- 2
You won 17 times playing $8 blackjack. The total amount you won was $136.
As for the source of your victory: each of the 17 wins was due to a card counting system you implemented. Specifically, you were discarding your cards by keeping track of which cards were worth more and which were worth less. While this won you the game, it also meant that you were constantly having to adjust your counter as the deck shuffled during the course of play. As you gained experience, you were able to get the hang of this and only occasionally had to adjust the counter. Still, in retrospect, knowing how crucial this was and how much of a handicap it placed on your opponents, it is not difficult to imagine that they may have been able to overcome your counting advantage.
What Happened If I Missed A Bet?
One of the things that make poker so interesting is that it is possible to lose a lot of money very quickly if you are not careful. The same is true of Blackjack. It is also possible to lose the game if the cards are not dealt randomly enough. There are ways that you can prevent this, but it is always possible for the game to become “loaded.” In other words, it is possible for there to be several bad beats in a row. To prevent this from happening, you must make sure that each hand is dealt randomly enough so that your opponent cannot predict the outcome. If you want to learn more, take a look at the mathematical principles of fair dice rolling.