Here are Dice Game which programed in C++ below. For this program, add a "cheater" function, which will prevent user from winning three times in the row (two times is OK, too). Also, add comments.
#include
#include
using namespace std;
int main()
{
int user,computer,sum,u_diff,c_diff,u_win=0,c_win=0;
do
{
cout<<"User guess is(1 - 18) :";
cin>>user;
if(user>=3&&user<=18) //the sum of three dice should be between 3 to 18
{
cout<<"Computer guess is :";
computer=rand()%16+3;
cout<
cout<<"Sum of the three rolls is :";
sum=rand()%16+3; //rand()%16 will generate the random number between 0 to 15.
cout<
u_diff=user-sum;
c_diff=computer-sum;
if(u_diff<0){
u_diff=u_diff* -1; //u_diff,c_diff is the diffence and it should always be positive.
}
if(c_diff<0){
c_diff*=c_diff* -1; //if u_diff or c_diff is negetive than we make it positive.
}
if(u_diff
cout<<"user won\n";
u_win++;
} else{
cout<<"computer won\n";
c_win++;
}
cout<<"User won: "<
}else if(user >= 19 || user <= 0){
cout<<"please enter between 1 to 18\n";
}
}while(user!=-1);
return 0;
}
Thanks for the question.
Below is the code you will be needing Let me know if you have
any doubts or if you need anything to change.
Thank You !!
Notes: Please comment this line cout<<"Using cheat"<<endl; inside the cheater function. I kept it for testing the logic.
Everytime user wins two successive games, the cheater function will be called and it will allow computer to win. See the screeenshot for the output.
Let me know for any questions or doubts.
thank you !!
===========================================================================
#include <iostream>
#include <cstdlib>
using namespace std;
int cheater(int computer, int user){
cout<<"Using cheat"<<endl;
if(computer>user)return computer+rand()%2;
if(computer==3 || computer==18)return computer;
if(user>computer)return computer -1;
}
int main()
{
int user, computer,sum,u_diff,c_diff,u_win=0,c_win=0;
int gameCount=0;
bool enableCheat=false;
int lastGameWon=0;
do
{
cout<<"User guess is(1 - 18) :";
cin>>user;
if(user>=3&&user<=18) //the sum of three dice should
be between 3 to 18
{
gameCount+=1;
cout<<"Computer guess is :";
computer=rand()%16+3;
cout<<computer<<endl<<"User guess is :
"<<user<<endl;
cout<<"Sum of the three rolls is :";
sum=rand()%16+3; //rand()%16 will generate the random number
between 0 to 15.
if(enableCheat)sum=cheater(computer,user);
cout<<sum<<endl;
u_diff=user-sum;
c_diff=computer-sum;
if(u_diff<0){
u_diff=u_diff* -1; //u_diff,c_diff is the diffence and it should
always be positive.
}
if(c_diff<0){
c_diff*=c_diff* -1; //if u_diff or c_diff is negetive than we make
it positive.
}
if(u_diff<c_diff){
cout<<"user won\n";
u_win++;
if(lastGameWon+1==gameCount)enableCheat=true;
lastGameWon=gameCount;
} else{
cout<<"computer won\n";
c_win++;
enableCheat=false;
}
cout<<"User won: "<<u_win<<" times.";
}else if(user >= 19 || user <= 0){
cout<<"please enter between 1 to 18\n";
}
}while(user!=-1);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.