C++ language (how to get the dices to roll randomly?) Use the following descript
ID: 3587834 • Letter: C
Question
C++ language (how to get the dices to roll randomly?)
Use the following description of the game of dice to solve the attached puzzle.
What are dice or what is a die – the singular form of dice?
A die is a cube of some hard, durable material, usually white in color. On each face of a die are a number of spots, usually black in color, arranged symmetrically on the face, each face having a different number of spots, ranging in number from one to six.
One player plays until he wins or loses. The player tosses a pair of dice on the ground and counts the number of spots on the upper most faces of the dice.
A player either wins or loses, as craps allows no tie games. The game ends as soon as a player wins or loses. A player wins, loses or defines his point on the first toss of the dice. A player who defines his point continues to toss the dice until he wins or loses.
A player who tosses 7 or 11 on the first toss wins. A player who tosses 2, 3 or 12 loses on the first toss. A player who tosses anything else defines his point and tosses until he tosses his point again and wins or tosses 7 and loses.
//random number includes
#include <ctime>
#include <random>
//standard includes
#include <string>
#include <iostream>
using namespace std;
int main()
{
default_random_engine e;
uniform_int_distribution<int> u(1, 6);
// puzzle begins
int roll_count = 1;
int point = die_sum;
roll_count++;
int die1 = u(e);
int die2 = u(e);
die1 = u(e);
die2 = u(e);
int die_sum = die1 + die2;
die_sum = die1 + die2;
cout << "You rolled " << die_sum << " (" << die1 << " and " << die2 << ")"
<< " on roll " << roll_count << endl;
cout << "You rolled " << die_sum << " (" << die1 << " and " << die2 << ")"
<< " on roll " << roll_count << endl;
cout << "You win on the first roll";
cout << "You lose on the first roll";
cout << "You win on roll " << roll_count << endl;
cout << "You lose on roll " << roll_count << endl;
cout << "Your point is " << point << endl;
cout << "Roll again!" << endl;
do
else
else
else
if(die_sum == 2 || die_sum == 3 || die_sum == 12)
if (die_sum == 7 || die_sum == 11)
if (die_sum == 7)
while (die_sum != 7 && die_sum != point);
{}
{}
// puzzle ends
cout << endl;
system("pause");
return 0;
}
Explanation / Answer
//random number includes
#include <ctime>
#include <random>
//standard includes
#include <string>
#include <iostream>
using namespace std;
int main()
{
random_device random_device;
mt19937 e{random_device()};
uniform_int_distribution<int> u{1, 6};
// puzzle begins
int roll_count = 1;
int die1 = u(e);
int die2 = u(e);
int die_sum = die1 + die2;
cout << "You rolled " << die_sum << " (" << die1 << " and " << die2 << ")"
<< " on roll " << roll_count << endl;
if (die_sum == 7 || die_sum == 11) {
cout << "You win on the first roll";
} else if (die_sum == 2 || die_sum == 3 || die_sum == 12) {
cout << "You lose on the first roll";
} else {
int point = die_sum;
cout << "Your point is " << point << endl;
do {
die1 = u(e);
die2 = u(e);
die_sum = die1 + die2;
roll_count++;
cout << "You rolled " << die_sum << " (" << die1 << " and " << die2 << ")"
<< " on roll " << roll_count << endl;
if (die_sum == 7) {
cout << "You lose on roll " << roll_count << endl;
}
else if (die_sum == point)
{
cout << "You win on roll " << roll_count << endl;
}
else {
cout << "Roll again!" << endl;
}
} while (die_sum != 7 && die_sum != point);
}
// puzzle ends
cout << endl;
// system("pause");
return 0;
}
Fixed all issue from your code
basically die is rolled by getting a number from uniform distribution.
Here is simpler solution
#include <string>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL));
// puzzle begins
int roll_count = 1;
int die1 = rand()%6;
int die2 = rand()%6;
int die_sum = die1 + die2;
cout << "You rolled " << die_sum << " (" << die1 << " and " << die2 << ")"
<< " on roll " << roll_count << endl;
if (die_sum == 7 || die_sum == 11) {
cout << "You win on the first roll";
} else if (die_sum == 2 || die_sum == 3 || die_sum == 12) {
cout << "You lose on the first roll";
} else {
int point = die_sum;
cout << "Your point is " << point << endl;
do {
die1 = rand()%6;
die2 = rand()%6;
die_sum = die1 + die2;
roll_count++;
cout << "You rolled " << die_sum << " (" << die1 << " and " << die2 << ")"
<< " on roll " << roll_count << endl;
if (die_sum == 7) {
cout << "You lose on roll " << roll_count << endl;
}
else if (die_sum == point)
{
cout << "You win on roll " << roll_count << endl;
}
else {
cout << "Roll again!" << endl;
}
} while (die_sum != 7 && die_sum != point);
}
// puzzle ends
cout << endl;
// system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.