Write a program. You are only allowed to use the following functions : #include<
ID: 3810206 • Letter: W
Question
Write a program. You are only allowed to use the following functions :
#include<iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
Explanation / Answer
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int flag=0;
void printDice(int dice)
{
if(dice==1)
{
cout<<" ******* * * * * * * * * * * * *******";
}
else if(dice==2)
{
cout<<" ******* * * * * * ** * * * * * *******";
}
else if(dice==3)
{
cout<<" ******* * * * * * *** * * * * * *******";
}
else if(dice==4)
{
cout<<" ******* * * * ** * * ** * * * * * *******";
}
else if(dice==5)
{
cout<<" ******* * * * ** * * * * * ** * * * *******";
}
else if(dice==6)
{
cout<<" ******* * * * ** * * ** * * ** * * * *******";
}
}
int Dieroll()
{
int die;
die=rand() % 6 + 1;
return die;
}
int SumDiece(int die1,int die2)
{
int sum=0;
sum=die1+die2;
return sum;
}
int main()
{
int dice1, dice2 = 0;
int rollDice;
char repeat = 'y';
while (repeat == 'y' || repeat == 'Y')
{
srand(time(0));
dice1 =Dieroll();
printDice(dice1);
dice2 = Dieroll();
printDice(dice2);
rollDice = SumDiece(dice1,dice2);
cout << " The sum of Your rolls is: " << rollDice;
if (rollDice == 7 || rollDice == 11)
{
cout << ". Winner !" << endl ;
}
else if (rollDice == 2 || rollDice == 3 || rollDice == 12)
{
cout << ". You lose!" << endl;
}
else if (rollDice == 4 || rollDice == 5 ||rollDice == 6 ||rollDice == 8 || rollDice == 9 || rollDice == 10)
{
do
{
//srand(time(0));
dice1 =Dieroll();
printDice(dice1);
dice2 = Dieroll();
printDice(dice2);
int sum2 = SumDiece(dice1,dice2);
cout << " The sum of Your rolls is: " << sum2;
if( sum2 == rollDice )
{
cout << ". Winner !" << endl;
flag=1;
break;
}
else if( sum2 == 7 )
{
cout << ". You Lose!" << endl;
flag=1;
break;
}
}while(flag!=1);
}
cout <<"Another game? Y(es) or N(o)" << endl;
cin >> repeat;
if(repeat == 'n' || repeat == 'N')
{
cout << "Thank you for playing!"<< endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.