CPP Dice Game ask User for a number number is sum of three dice rolls - program
ID: 3729214 • Letter: C
Question
CPP Dice Game ask User for a number number is sum of three dice rolls - program creates a computers guess program rolls a dice three times - values entered into array winner determined by the closest guess Example: user guess is 10 computer guess is 12 sum of three rolls is 9 user wins Example: user guess is 10 computer guess is 12 sum of three rolls is 11 computer wins: user ONLY wins if guess is closer than computer Requirements: 1) Program 1) 2) 3) 4) Keeps the score for user and computer Displays the score after each round Continues until user enters -1 Should use random numbers (rand) 2) Functions 1) 2) to roll the dice (meaning entering into array) determine the guess's difference from the sum 3) Main 1) 2) 3) asks user for input calls the functions outputs the results with the scoreExplanation / Answer
Code
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int user,computer,sum,uDif,cDif,uWin=0,cWin=0;
do
{
cout<<"User guess is :";
cin>>user;
if(user>=3&&user<=18)
{
cout<<"Computer guess is :";
computer=rand()%16+3;
cout<<computer<<" ";
cout<<"Sum of the three rolls is :";
sum=rand()%16+3;
cout<<sum<<" ";
uDif=user-sum;
cDif=computer-sum;
if(uDif<0){uDif=uDif* -1;}
if(cDif<0){cDif=cDif* -1;}
if(uDif<cDif)
{
cout<<"user won ";
uWin++;
}
else
{
cout<<"computer won ";
cWin++;
}
cout<<"User won: "<<uWin<<" times, Computer won: "<<cWin<<" times ";
}
else
{
cout<<"Enter number between 1-18 ";
}
} while(user!=-1);
return 0;
}