write a program that plays a dice game with the user. Here are the rules. The ga
ID: 3630347 • Letter: W
Question
write a program that plays a dice game with the user. Here are the rules.
The game is about repeated "throws" of a pair of dice.
Each die has six faces, numbered 1 through 6.
A throw results in a number that is the total of the two top faces.
The first throw establishes the player's LKNBR.
If that LKNBR is 7 or 11, the player automatically wins.
If that LKNBR is 2, the player automatically loses.
Otherwise, the player continues throwing until she wins (by throwing her LKNBR again) or loses (by throwing a 7 or an 11).
After each game is resolved (i.e., the user either wins or loses), she should be given the option of playing again.
Explanation / Answer
please rate - thanks
with functions
#include<iostream>
#include <stdlib.h>
using namespace std;
void GetRoll(int&);
void CalcSum(int,int,int&);
void printRoll(int,int,int);
int main()
{int dice,die1,die2,LKNBR;
char c;
bool gameover;
srand(time(0));
do
{gameover=false;
LKNBR=0;
while(!gameover)
{
GetRoll(die1);
GetRoll(die2);
CalcSum(die1,die2,dice);
printRoll(die1,die2,dice);
if(LKNBR==0)
{if(dice==2)
{cout<<"you lose ";
gameover=true;
}
else if(dice==7||dice==11)
{cout<<"you win ";
gameover=true;
}
else
{LKNBR=dice;
cout<<"LKNBR is "<<LKNBR<<endl;
}
}
else
{if(LKNBR==dice)
{cout<<"you win ";
gameover=true;
}
else
if(dice==7||dice==11)
{gameover=true;
cout<<"you lose ";
}
}
}
cout<<"Play again?(y/n): ";
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}
void printRoll(int d1,int d2,int roll)
{cout<<"You rolled "<<d1<<" + "<<d2<<" = "<<roll<<endl;
}
void GetRoll(int & d)
{d=rand()%6+1;
}
void CalcSum(int d,int d2,int &s)
{s=d+d2;
}
---------------------------------------
without functions
#include<iostream>
#include <stdlib.h>
using namespace std;
int main()
{int dice,die1,die2,LKNBR;
char c;
bool gameover;
srand(time(0));
do
{gameover=false;
LKNBR=0;
while(!gameover)
{
die1=rand()%6+1;
die2=rand()%6+1;
dice=die1+die2;
cout<<"You rolled "<<die1<<" + "<<die2<<" = "<<die1+die2<<endl;
if(LKNBR==0)
{if(dice==2)
{cout<<"you lose ";
gameover=true;
}
else if(dice==7||dice==11)
{cout<<"you win ";
gameover=true;
}
else
{LKNBR=dice;
cout<<"LKNBR is "<<LKNBR<<endl;
}
}
else
{if(LKNBR==dice)
{cout<<"you win ";
gameover=true;
}
else
if(dice==7||dice==11)
{gameover=true;
cout<<"you lose ";
}
}
}
cout<<"Play again?(y/n): ";
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.