In the game of Lucky Sevens, the player rolls a pair of dice. If the face values
ID: 3840807 • Letter: I
Question
In the game of Lucky Sevens, the player rolls a pair of dice. If the face values of the dice add up to 7, the player wins $4; otherwise, the player loses $1. Write a C++ program that takes as input the amount of money the player has. The program should play the game until the player has no money. The program should print the number of rolls it took to break the player. Use the random number generator to simulate the rolling of two dice. Keep track of maximum amount of money the player ever had and the number of rolls it took to reach it See below for a sample input/output The user should have the option to play again if he/she chooses. Sample input/output: Enter the amount of money you have: 100 It took 420 rolls to loose all your money. Your maximum was $103.00 and was reached after 6 rolls. Play again (y/n)?y Enter the amount of money you have: 20 It took 30 rolls to loose all your money. Your maximum was $24.00 and was reached after 1 rolls. Play again (y/n)?y Enter the amount of money you have: 50 It took 270 rolls to loose all your money. Your maximum was $66.00 and was reached after 123 rolls. Play again (y/n)?nExplanation / Answer
#include<iostream.h>
#include<stdlib.h>
#include <time.h>
void main(){
int amount;
int dice1;
int dice2;
int max; //maximum value of the amount
char choice;
int count; // number of rolls
int max_reached // the count when amount reached maximum
srand(time(NULL));
do {
cout << "Enter the amount of money you have: ";
cin >> amount;
max = 0;
count = 0;
while (amount > 0){
count++;
dice1 = rand() % 7 + 1;
dice2 = rand() % 7 + 1;
if (dice1 + dice2 == 7){
amount = amount + 4;
if (amount > max){
max = amount;
max_reached = count;
}
}
else {
amount = amount - 1;
}
}
cout << "It took " << count << " rolls to lose all your money." << endl;
cout << "Your maximum was $" << max << "and was reached after " << max_reached << "rolls" << endl;
cout << "Play again (y/n)?" ;
cin >> choice;
} while (choice != 'n')
}
#include<iostream.h>
#include<stdlib.h>
#include <time.h>
void main(){
int amount;
int dice1;
int dice2;
int max; //maximum value of the amount
char choice;
int count; // number of rolls
int max_reached // the count when amount reached maximum
srand(time(NULL));
do {
cout << "Enter the amount of money you have: ";
cout >> amount;
max = 0;
count = 0;
while (amount > 0){
count++;
dice1 = rand() % 7 + 1;
dice2 = rand() % 7 + 1;
if (dice1 + dice2 == 7){
amount = amount + 4;
if (amount > max){
max = amount;
max_reached = count;
}
}
else {
amount = amount - 1;
}
}
cout << "It took " << count << " rolls to lose all your money." << endl;
cout << "Your maximum was $" << max << "and was reached after " << max_reached << "rolls" << endl;
cout << "Play again (y/n)?" ;
cin >> choice;
} while (choice != 'n')
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.