One of the most popular games of chance is a dice game known as “craps,” which is played in casinos and back alleys worldwide. The rules of the game are straightforward:
A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is 2, 3 or 12 on the first roll (called “craps”), the player loses (i.e., the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, then that sum becomes the player’s “point.” To win, you must continue rolling the dice until you “make your point.” The player loses by rolling a 7 before making the point.
The program in Fig. 6.10 simulates the game. In the rules, notice that the player must roll two dice on the first roll and on all subsequent rolls. We define function rollDice
(lines 62–74) to roll the dice and compute and print their sum. The function is defined once, but called from lines 20 and 44. The function takes no arguments and returns the sum of the two dice, so empty parentheses and the return type unsigned int
are indicated in the function prototype (line 8) and function header (line 62).
1 // Fig. 6.10: fig06_10.cpp
2 // Craps simulation.
3 #include <iostream>
4 #include <cstdlib> // contains prototypes for functions srand and rand
5 #include <ctime> // contains prototype for function time
6 using namespace std;
7
8 unsigned int rollDice(); // rolls dice, calculates and displays sum
9
10 int main()
11 {
12 // enumeration with constants that represent the game status
13 enum Status { CONTINUE, WON, LOST }; // all caps in constants
14
15 // randomize random number generator using current time
16 srand( static_cast<unsigned int>( time( 0 ) ) );
17
18 unsigned int myPoint = 0; // point if no win or loss on first roll
19 Status gameStatus = CONTINUE; // can contain CONTINUE, WON or LOST
20 unsigned int sumOfDice = rollDice(); // first roll of the dice
21
22 // determine game status and point (if needed) based on first roll
23 switch ( sumOfDice )
24 {
25 case 7: // win with 7 on first roll
26 case 11: // win with 11 on first roll
27 gameStatus = WON;
28 break;
29 case 2: // lose with 2 on first roll
30 case 3: // lose with 3 on first roll
31 case 12: // lose with 12 on first roll
32 gameStatus = LOST;
33 break;
34 default: // did not win or lose, so remember point
35 gameStatus = CONTINUE; // game is not over
36 myPoint = sumOfDice; // remember the point
37 cout << "Point is " << myPoint << endl;
38 break; // optional at end of switch
39 } // end switch
40
41 // while game is not complete
42 while ( CONTINUE == gameStatus ) // not WON or LOST
43 {
44 sumOfDice = rollDice(); // roll dice again
45
46 // determine game status
47 if ( sumOfDice == myPoint ) // win by making point
48 gameStatus = WON;
49 else
50 if ( sumOfDice == 7 ) // lose by rolling 7 before point
51 gameStatus = LOST;
52 } // end while
53
54 // display won or lost message
55 if ( WON == gameStatus )
56 cout << "Player wins" << endl;
57 else
58 cout << "Player loses" << endl;
59 } // end main
60
61 // roll dice, calculate sum and display results
62 unsigned int rollDice()
63 {
64 // pick random die values
65 unsigned int die1 = 1 + rand() % 6; // first die roll
66 unsigned int die2 = 1 + rand() % 6; // second die roll
67
68 unsigned int sum = die1 + die2; // compute sum of die values
69
70 // display results of this roll
71 cout << "Player rolled " << die1 << " + " << die2
72 << " = " << sum << endl;
73 return sum; // end function rollDice
74 } // end function rollDice
Player rolled 2 + 5 = 7
Player wins
Player rolled 6 + 6 = 12
Player loses
Player rolled 1 + 3 = 4
Point is 4
Player rolled 4 + 6 = 10
Player rolled 2 + 4 = 6
Player rolled 6 + 4 = 10
Player rolled 2 + 3 = 5
Player rolled 2 + 4 = 6
Player rolled 1 + 1 = 2
Player rolled 4 + 4 = 8
Player rolled 4 + 3 = 7
Player loses
Player rolled 3 + 3 = 6
Point is 6
Player rolled 5 + 3 = 8
Player rolled 4 + 5 = 9
Player rolled 2 + 1 = 3
Player rolled 1 + 5 = 6
Player wins
3.133.124.21