In: Computer Science
Using C++ and using a boolean variable with local scope to control the looping structure:
How do I create a program that will simulate a game of craps with the below qualification:
Containing two functions -an int rollDice() function that will simulate the rolling of two dice and the main function. When called the roll Dice() function will generate two random numbers between 1 and 6 inclusive, add them, and return the sum as the integer variable “point”. The function will also output the value of the “roll” of each digital die and the value of “point” to cout. Selection and looping structures in the int main() function will determine the impact of each roll (win, lose, or roll again) on the game, print the results, and continue the game if necessary.

#include <iostream>
#include <cstdlib>
using namespace std;
int rollDice() {
int a = rand() % 6 + 1;
int b = rand() % 6 + 1;
cout << "Dice 1: " << a
<< ", Dice 2: " << b << endl;
return a + b;
}
int main() {
srand(time(NULL));
int sum = rollDice();
if (sum == 7 || sum == 11) {
cout << "you
won!" << endl;
}
else if (sum == 2 || sum == 3 || sum == 12)
{
cout << "you
lose!" << endl;
}
else {
int point =
sum;
do {
cout
<< "Dice sum: " << sum << endl;
cout
<< "Rolling again." << endl;
sum
= rollDice();
} while (sum != 7
&& sum != point);
if (sum == 7)
{
cout
<< "you lose!" << endl;
}
else if (sum ==
point) {
cout
<< "you won!" << endl;
}
}
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.