The Game is called Twenty-One. The user rolls dice (6 sided) in an attempt to hi
ID: 3678356 • Letter: T
Question
The Game is called Twenty-One. The user rolls dice (6 sided) in an attempt to hit exactly 21. If the user goes over 21, they lose. If they hit 21 exactly, they win. Then they can play the game again if they want. At first the user should roll 2 dice at a time until their total hits 15 or above. Once they hit 15 or above then they should only roll one dice. If they hit 21, then they win. If they go over, then they lose. (Use a random number generator to simulate the dice rolls.) I need a C++ program that satisfy all these conditions.
Explanation / Answer
#include <iostream>
#include <cstdlib> /* srand, rand */
#include <ctime> // time
using namespace std;
int main() {
srand((int)time(0));
char ch;
int total,d1,d2,i,k=1;
do{
cout<<"--------------------------------- ";
cout<<" Game "<<k++;
cout<<" --------------------------------- ";
total =0;
i = 0;
while(total<15){
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
cout<<"d"<<++i<<" = "<<d1<<endl;
cout<<"d"<<++i<<" = "<<d2<<endl;
total += d1+d2;
cout<<"Curent Total ="<< total<<endl;
}
while(total<21){
d1 = rand() % 6 + 1;
total += d1;
cout<<"d"<<++i<<" = "<<d1<<endl;
cout<<"Curent Total ="<< total<<endl;
}
if(total == 21)
cout<<"you win this game"<<endl;
else
cout<<"you loose this game"<<endl;
cout<<"Dou you wish to continue ? (Y/N)"<<endl;
cin>>ch;
cout<<endl<<endl<<endl;
}while(ch == 'Y' || ch == 'y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.