In: Computer Science
I want to make the Main function of this program to only drive. So, make more functions, and change main function to just drive.
This program Dice Game.
Based on this C++ program below:
#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;
}
//Modified C++ program
#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;
}
void getUserInput(int &user){
   cin>>user;
}
int decideWinner(int user,bool enableCheat){
int computer,sum,u_diff,c_diff;
   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)return 1;
   else return 0;
}
bool isValidInput(int user){
   if(user>=3 && user<=18)return
true;
   return false;
}
int main()
{
int user,u_win=0,c_win=0;
int gameCount=0;
bool enableCheat=false;
int lastGameWon=0;
do
{
   cout<<"User guess is(1 - 18) :";
   getUserInput(user);
   if(isValidInput(user)){
       int val =
decideWinner(user,enableCheat);
       gameCount++;
       if(val==1)
       {
          
cout<<"user won\n";
           u_win++;
          
if(lastGameWon+1==gameCount)enableCheat=true;
          
lastGameWon=gameCount;
       }
       else if(val==0){
          
cout<<"computer won\n";
           c_win++;
          
enableCheat=false;
       }
   }
      
  
   else{
       cout<<"please enter between 1
to 18\n";
   }
}while(user!=-1);
cout<<"User won: "<<u_win<<" times.";
return 0;
}