1
As loath as I am to use a gender-specific example, women didn’t serve in the Royal Navy until 1917.
coins on any combination of the squares: these become the bets. Then he
rolls three
six-sided dice with faces that match the squares on the mat. For each die that matches
a square that has a bet on it, the sailor wins that amount of money. Here are some
examples of how the sailor might play, and what the payout is:
Bet
Roll
Payout
5 pence on Crown
Crown, Crown, Crown 15 pence
5 pence on Crown
Crown, Crown, Anchor 10 pence
5 pence on Crown
Crown, Heart, Spade
5 pence
5 pence on Crown
Heart, Anchor, Spade 0
3 pence on Crown, 2 on Spade Crown, Crown, Crown 9 pence
3 pence on Crown, 2 on Spade Crown, Spade, Anchor 5 pence
1 pence on all squares
Any roll
3 pence (not a good strategy!)
I chose this example because it is not too complex, and with a little imagination, dem‐
onstrates the main control flow statements. While it’s unlikely that you will ever need
to simulate the gambling behaviors of 19th-century sailors, this type of simulation is
quite common in many applications. In the case of Crown and Anchor, perhaps we
have constructed a mathematical model to determine if we should open a Crown and
Anchor booth to raise money for charity at our next company event. The simulation
we construct in this chapter can be used to support the correctness of our model.
The game itself is simple, but there are many thousands of ways it could be played.
Our sailor—let’s call him Thomas (a good, solid British name)—will start off very
generic, and his behavior will become more detailed as we proceed.
Let’s begin with the basics: starting and stopping conditions. Every time Thomas gets
shore leave, he takes 50 pence with him to spend on Crown and Anchor. Thomas has
a limit: if he’s lucky enough to double his money, he quits, walking away with at least
100 pence in his pocket (about half his monthly wages). If he doesn’t double his
money, he gambles until he’s broke.
We’ll break the playing of the game into three parts: placing the bets, rolling the dice,
and collecting the winnings (if any). Now that we have a very simple, high-level pic‐
56 | Chapter 4: Control Flow