In order to provide the motivation for this demonstration, I would like us to consider the casino game of Craps. Now I realize many of you won’t have played the game of Craps. But, the essence of the game is that it involves rolling a pair of dice on a table, on the casino table. And the question is whether the sum of the numbers that come up on the dice, obviously the minimum number is 2.
And the largest number that could be the total is 12. If that number, if, the, the game depends on whether that number, is repeated more frequently than the number 7 comes up. I didn’t express that very clearly. And it is not at this point terribly important.
But, the important thing is the game involves rolling a pair of dice and considering what the total number of pips on the two dice rolled is. Okay. So in this case, I have designed a number of functions, which will help us with our program. At the beginning of the program here, you will see that I provided the function prototypes. We are going to have two functions here. One function, called “rollaDie” is going to simulate the rolling, the random rolling of the dice on a casino table.
And it returns a value of type ‘int,’ as you will see it will be one of the six numbers, 1-6. But from C++’s point of view, it is a value of type ‘int.’ As we have seen before, I have a function called “abortProgram,” whose sole purpose is to put the message on the screen: “Press enter or q to terminate the program.” This doesn’t return anything at all, so the return type for that function is ‘void.’
Let’s look at ‘abortProgram’ first. I put that at the end of this source file, the function prototypes, of course, just like the Table of Contents of a book appear at the beginning. But the details, the implementation of ‘abortProgram’ appears at the end of the program.
It is down here. So it is a very simple, it’s a very simple function. It prompts the user to enter a line. It reads it into a string variable called ‘line.’ And when the user does that, the program terminates.
Notice, by the way, the scope of the variable ‘line’ is from the point of declaration to the end of the code block in which it is declared. Which in this case is right there, that is the scope of the variable ‘line.’ In our program, there is another variable ‘line,’ it appears in ‘main.’ Right here.
The scope of this variable ‘line’ is the remainder of the function ‘main’ which goes on down through here. In this case, we have two variables called ‘line.’ They are both of type ‘string,’ but they have different scopes and they are completely different from one another.
The ‘line’ that’s in ‘abort,’ the string ‘line’ that is in ‘abortProgram’ has nothing whatever to do with the string ‘line’ that appears in the function called ‘main.’ Two variables with the same name, but non-overlapping scopes. Okay. The next method I want to look at is the method called ‘rollADie.’ It returns an integer in the range 1-6. And it does it as follows: There is a built in function in the math library.
I have included the math library at the beginning here, #include cmath [pronounced: pound-include-C-math.] That is the include of the math library. It has a function called ‘rand,’ which generates a random number in the range 0 to a very very large number indeed. If we divide that by the number 6, remember that the percent operator [%] gives us the remainder when we divide a number by 6. The remainder on dividing any number by 6 is going to be in the range 0-5. So if I add 1 to that, you will see that ‘die’ becomes a randomly number, a randomly generated integer in the range 1-6.
We then switch on the value of that die. Remember that the argument of a switch statement can be either an ‘int’ or a ‘char.’ In this case it is an ‘int.’ and we have several cases, cases 1-6. In each case, we tell the user what got rolled on that random roll of the die.
And we return that value. ‘return die’ returns that integer as the return value of the function ‘rollADie.’ Notice, by the way, that each case ends with a ‘break’ statement.
Otherwise, you will get some output, like “you rolled a 1, you rolled a 2, you rolled a 3, you rolled a 4.” We need to make sure that each case is individually self-contained. And we do that with a ‘break’ statement.
Okay. Having done that, we can then use these functions in our main program. We are going to ask the user if they want to play the game that they signed up for.
We will read their reply. If they didn’t answer anything, we are going to take that as being a yes. Otherwise, we are going to get the first character in the string that they entered. So, ‘line.at(0)’ returns the zeroth character of the string, the string in line.
The zeroth character is the first character because, characters in a string number from zero. So, if the user entries nothing, we will take that as a y. Otherwise, we will take the very first character and we will switch on the char because that char will be the first character the user entered. If they entered a lower case n or a capital N, we will bid them farewell and abort the program.
If they entered a Y/y, we will welcome them to the game and we’ll break out of the switch, which will cause the flow of control to go on later into the main program. And if they don’t enter an N or a Y, we will also abort the program with an error message. Now, once they have entered the program, we need to seed the random number generator. ‘srand’ is the function which seeds it. “Seed-Rand.” And we will seed the random number generator with the current time of day.
We do that by running the time function, which is in the ctime library. [pronounced C-Time library] The reason that we need to seed the random number generator is because without it, the random number generator will generate precisely the same sequence of random numbers every time we run the program. And of course if you generate exactly the same numbers, the same sequence of numbers every time you run the program, the numbers aren’t going to be very random. So we need to start the sequence with a newly generated random number every time we run the program. And we do that by seeding the random number generator once and once only at the beginning of the program with a statement that looks like that. As a mentioned earlier, there are two die in the game.
So we have two ‘ints.’ We’ll roll a die. The first time we roll it we will assign the number 1-6 to die one.
We will call the function ‘rollADie’ again. We will assign the value that got rolled to die 2. And in the game of Craps, the so-called ‘point’ is the sum of die one plus die two.
So the ‘point’ is the value is the total number of pips on the two dice that we just rolled. Now the rules are such that if the point happens to be 7 or the point happens to be 11, the player wins. If the point happens to be 2, 3, or 12, the player loses. Otherwise, if it’s none of those, we keep rolling the dice again.
And we have a ‘do’ statement here. In fact, excuse, me, it is a ‘do-while’ statement from here to here. We ask the user to “Press enter to roll the dice.” We roll the two dice again. We calculate the total.
And if the total is equal either to the point or the number 7, the rolls of the dice cease. But all the time the total on the two dice is not equal to 7 or the point, we keep rolling. The way the game works is if the total is equal to the point, then the player wins. And if the total is anything but the point, then the player loses. It is a complicated game.
I won’t go into the details. I will let you look them up on the web. The purpose of this is to show you how to use functions in C++ and also to show you how to use the random number generator and various other things that we will look at later.
So, let’s just play this game one more time. I am going to run the program. We will play the game of Craps. As you can see, I rolled a 4 and a 5, my point is 9. So if I roll a 9 again, before 7, I am going to win.
Let’s roll the dice. That was a 3. That is neither 9 or 7.
In this case, on the second roll of the dice, I rolled a 6 and a 1. The total is 7, so the player loses. I have lost this particular bet because the 7 came up before my point came up for a second time. So, 7 came up before 9.
Press enter to quit and that is the end of the program. Thank you.