The program is in C++ Background: Craps is a dice game in which the players make
ID: 3791947 • Letter: T
Question
The program is in C++
Background: Craps is a dice game in which the players make wagers on the outcome of the roll, or series of rolls, of a pair of dice2 . To start a round, the shooter makes a "come-out" roll. A come-out roll of 2, 3 or 12 is called "craps" and is an immediate loss. A come-out roll of 7 or 11 is a "natural", and is an immediate win. The other possible rolls are called the “point” numbers: 4, 5, 6, 8, 9, and 10. If the shooter rolls one of these numbers on the come-out roll, this establishes the "point", and the point number must be rolled again before a seven on subsequent rolls in order to win.
Assignment: You will complete this game that you started in the last homework. Give the user 5 chips initially. Subtract 1 for a loss and add one for a win. Stop when the user is out of chips or has 10 chips. Output the number of chips at the end of each game. Your program must do the following:
* use a while loop when 0 < chips <= 11
* use a while loop when attempting the point value
* stop when chips are 0 or greater than 10
Sample Output:
You rolled 6 and a 3 Point is 9.
Now you will roll again
You rolled 6 and a 4
Push on, roll again. Y
ou rolled 3 and a 4
Bad roll, you lose.
CHIPS: 4 You rolled 2 and a 1 craps. You lose CHIPS: 3 You rolled 5 and a 2 You win!
CHIPS: 4 You rolled 5 and a 4 Point is 9. Now you will roll again You rolled 1 and a 3 Push on, roll again. You rolled 5 and a 4 Rolled the point. You win.
Explanation / Answer
CODE:
#include <iostream>
#include <cstdlib>
#include <ctime>
// namespace included.
using namespace std;
unsigned int rollDice();
int main(){
// variables declared.
unsigned int myPoint = 0;
unsigned int sumOfDice;
// die1 and die2 have the die value returned from rollDice() function.
int die1 = 0;
int die2 = 0;
int chips = 5;
// repeat the loop till the number of chips are between 0 < chips <= 11
while((0 < chips) && (chips <= 11)){
// Shooter is playing
cout << endl << "-----------------------------------------" << endl;
cout << "Current points are " << chips << endl;
die1 = rollDice();
die2 = rollDice();
sumOfDice = die1 + die2;
cout << "Chips: " << chips << endl;
cout << "You rolled " << die1 << " and a " << die2 << ". Chips are "<< chips << endl;
switch(sumOfDice){
// if the sumOfDice is 7 or 11, points gained.
case 7:
case 11:
cout << "You win!" << endl;
chips++;
break;
// if sumOfDice is 2,3, or 12, points lost.
case 2:
case 3:
case 12:
cout << "Bad roll, you lose." << endl;
chips--;
break;
default:
cout << "Now you will roll again" << endl;
myPoint = sumOfDice;
die1 = rollDice();
die2 = rollDice();
sumOfDice = die1 + die2;
if(sumOfDice == myPoint){
chips++;
}
else{
chips--;
}
cout << "You rolled " << die1 << " and a " << die2 << ". Chips are "<< chips << endl;
break;
}
}
}
// return the dice value.
unsigned int rollDice(){
int roll = rand() % (7 - 1) + 1;
return roll;
}
OUTPUT:
$ g++ craps2.cpp
$ ./a.out
-----------------------------------------
Current points are 5
Chips: 5
You rolled 2 and a 5. Chips are 5
You win!
-----------------------------------------
Current points are 6
Chips: 6
You rolled 4 and a 2. Chips are 6
Now you will roll again
You rolled 6 and a 2. Chips are 5
-----------------------------------------
Current points are 5
Chips: 5
You rolled 5 and a 1. Chips are 5
Now you will roll again
You rolled 4 and a 2. Chips are 6
-----------------------------------------
Current points are 6
Chips: 6
You rolled 3 and a 2. Chips are 6
Now you will roll again
You rolled 3 and a 2. Chips are 7
-----------------------------------------
Current points are 7
Chips: 7
You rolled 6 and a 5. Chips are 7
You win!
-----------------------------------------
Current points are 8
Chips: 8
You rolled 1 and a 1. Chips are 8
Bad roll, you lose.
-----------------------------------------
Current points are 7
Chips: 7
You rolled 5 and a 5. Chips are 7
Now you will roll again
You rolled 6 and a 3. Chips are 6
-----------------------------------------
Current points are 6
Chips: 6
You rolled 4 and a 4. Chips are 6
Now you will roll again
You rolled 3 and a 3. Chips are 5
-----------------------------------------
Current points are 5
Chips: 5
You rolled 3 and a 2. Chips are 5
Now you will roll again
You rolled 2 and a 2. Chips are 4
-----------------------------------------
Current points are 4
Chips: 4
You rolled 6 and a 1. Chips are 4
You win!
-----------------------------------------
Current points are 5
Chips: 5
You rolled 1 and a 1. Chips are 5
Bad roll, you lose.
-----------------------------------------
Current points are 4
Chips: 4
You rolled 6 and a 4. Chips are 4
Now you will roll again
You rolled 2 and a 5. Chips are 3
-----------------------------------------
Current points are 3
Chips: 3
You rolled 2 and a 5. Chips are 3
You win!
-----------------------------------------
Current points are 4
Chips: 4
You rolled 4 and a 4. Chips are 4
Now you will roll again
You rolled 4 and a 6. Chips are 3
-----------------------------------------
Current points are 3
Chips: 3
You rolled 3 and a 2. Chips are 3
Now you will roll again
You rolled 3 and a 3. Chips are 2
-----------------------------------------
Current points are 2
Chips: 2
You rolled 6 and a 1. Chips are 2
You win!
-----------------------------------------
Current points are 3
Chips: 3
You rolled 6 and a 5. Chips are 3
You win!
-----------------------------------------
Current points are 4
Chips: 4
You rolled 2 and a 3. Chips are 4
Now you will roll again
You rolled 3 and a 4. Chips are 3
-----------------------------------------
Current points are 3
Chips: 3
You rolled 3 and a 3. Chips are 3
Now you will roll again
You rolled 5 and a 2. Chips are 2
-----------------------------------------
Current points are 2
Chips: 2
You rolled 4 and a 4. Chips are 2
Now you will roll again
You rolled 3 and a 4. Chips are 1
-----------------------------------------
Current points are 1
Chips: 1
You rolled 3 and a 6. Chips are 1
Now you will roll again
You rolled 5 and a 4. Chips are 2
-----------------------------------------
Current points are 2
Chips: 2
You rolled 2 and a 6. Chips are 2
Now you will roll again
You rolled 1 and a 4. Chips are 1
-----------------------------------------
Current points are 1
Chips: 1
You rolled 1 and a 4. Chips are 1
Now you will roll again
You rolled 3 and a 4. Chips are 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.