Get startedGet started for free

Craps

1. Craps

Our final lesson with dice involves the casino game Craps.

2. Introduction to craps

Craps is another game that depends on rolling two dice. There are several rules to Craps but, in this lesson, we will focus on the pass line bet. The pass line bet is a wager that any player at the table can make at the beginning of a round of play. The round of play begins with a person, referred to as the shooter, rolling two dice. On the shooter's first roll, if a 7 or 11 is rolled, any player who made the pass line bet wins that bet. Alternatively, if the shooter rolls a 2, 3, or 12 on the first roll, any player who made the pass line bet loses it. If any other value is rolled, it establishes what is known as a point, and the pass line bet is neither won nor lost, yet.

3. When a point is established

Suppose that the shooter's first roll is a 5. The value of 5 is now the point, and the shooter will continue to roll the two dice. If another 5 is rolled before a 7, the pass line bet is won. If a 7 is rolled before another 5, the pass line bet is lost. If the roll is any other value, the shooter continues to roll until rolling a 5 or a 7. Whichever comes first will dictate whether the pass line bet is won or lost. Our question is, what is a player's probability of winning the pass line bet?

4. While loop

Unlike in the last puzzle, there is not a set number of rolls, so we cannot use replicate or a for loop. Instead, we are rolling until a certain condition is met, either a 7 or the current point is rolled. We can use a while loop to accomplish this, as demonstrated here. Suppose we roll one die until a 6 is rolled. We can use a while loop with the condition that the roll is not equal to 6, and the code will continue simulating rolls until a 6 is rolled. Note that we initially set roll equal to 0. Otherwise, the while loop would produce an error stating that roll does not exist.

5. Compound condition in a while loop

Since we are rolling until either the point or a 7 is rolled, we need to specify a compound condition. Here we demonstrate this principle by rolling a single die, and continue rolling until a 5 or 6 is rolled. Once either is rolled, the code will stop. Note that we use the and operator because we need the roll to be not 6 and not 5 to continue.

6. The %in% operator

If we need to check whether the object is one of several values, it is convenient to use the percent in percent operator from base R. Suppose we roll a single die and want to know if the resulting denomination was an even value. Here, we can do this by checking whether the value of roll is in the set of all possible even rolls, namely 2, 4, and 6, using the code that is in the if statement shown here. As noted, the actual roll value is 2, so the statement "The roll is even" is printed.

7. Let's play some Craps!

Let's do it!