Please write this in C++!! E.3 Dice Game Using rand0 Requirements: Write a dice
ID: 3728046 • Letter: P
Question
Please write this in C++!!
E.3 Dice Game Using rand0 Requirements: Write a dice rolling program to roll a die to reach a total dice value of 25. Specifications: For this question, you will be simulating the rolling of dice. This can be accomplished by using the built in rand function discussed in lecture unit 5, slides 50 through 58. You will use this function to randomly generate a number between 1 and 6 inclusive to simulate a single roll of a die. Create a function rollDice that returns an integer result of the rolled dice. The function will have the following prototype: 1. 2. int rollDice Inside your main function, you will tell the user the goal score of the game (25) and tell them their current score (starts at 0 and gets larger the more dice they roll). Ask the user to choose if they would like to roll another die or stay at their current score. If they wish to roll, call the rollDice function and add the returned result to the user's current score. You should also display the result returned from the rollDice function in the main with a print statement. For example, 3. 4. 5. coutExplanation / Answer
Dear Student,
below i written the C++ code as per the requirement.
Please note the below program has been tested on ubuntu 16.04 system and compiled using g++ compiler. This code will also work on other IDE's
-----------------------------------------------------------------------------------------------------------------------------------
Program:
-----------------------------------------------------------------------------------------------------------------------------------
//header files
#include<iostream>
#include<cstdlib>
#include<ctime>
//namespace
using namespace std;
//function rollDice
int rollDice()
{
//set a seed value
srand(time(NULL));
return rand()%6+1;
}
//start of the main function
int main()
{
//variables
int goal = 25, res;
int total = 0;
char ans;
//start of do while loop this loop runs until the user chose n or N
do
{
//display the goal score and running score
cout<<"The goal is a score of: "<<goal<<endl;
cout<<"Your current running score is: "<<total<<endl;
cout<<"Would you like to roll the die? (y/n): ";
cin>>ans;
//if ans is yes
if(ans == 'y' || ans == 'Y')
{
//call to function
res = rollDice();
//calculate total score
total = total + res;
//if total score is less than 25
if(total < 25)
{
cout<<"The result of the current roll is: "<<res<<endl;
cout<<endl;
}
//total is greater than 25
else if(total > 25)
{
cout<<"Your current running score is: "<<total<<endl;
cout<<"You rolled too many..!!!"<<endl;
break;
}
}
//if ans is No break the program
else if(ans == 'n' || ans == 'N')
{
cout<<"Game Over!"<<endl;
cout<<"Your score was: "<<total<<endl;
cout<<"Program ended with exit code: 0"<<endl;
break;
}
//else print a new line
else
{
cout<<endl;
}
}while(ans != 'y' || ans != 'Y');
return 0;
}
//end of the main function
================================================================
Sample Output:
nirmalsharma@ubuntu:~/chegg_solutions/11_03_2018$ g++ roll_dice.cpp
nirmalsharma@ubuntu:~/chegg_solutions/11_03_2018$ ./a.out
The goal is a score of: 25
Your current running score is: 0
Would you like to roll the die? (y/n): y
The result of the current roll is: 3
The goal is a score of: 25
Your current running score is: 3
Would you like to roll the die? (y/n): h
The goal is a score of: 25
Your current running score is: 3
Would you like to roll the die? (y/n): y
The result of the current roll is: 3
The goal is a score of: 25
Your current running score is: 6
Would you like to roll the die? (y/n): y
The result of the current roll is: 1
The goal is a score of: 25
Your current running score is: 7
Would you like to roll the die? (y/n): y
The result of the current roll is: 1
The goal is a score of: 25
Your current running score is: 8
Would you like to roll the die? (y/n): y
The result of the current roll is: 3
The goal is a score of: 25
Your current running score is: 11
Would you like to roll the die? (y/n): k
The goal is a score of: 25
Your current running score is: 11
Would you like to roll the die? (y/n): Y
The result of the current roll is: 6
The goal is a score of: 25
Your current running score is: 17
Would you like to roll the die? (y/n): y
The result of the current roll is: 5
The goal is a score of: 25
Your current running score is: 22
Would you like to roll the die? (y/n): Y
Your current running score is: 26
You rolled too many..!!!
*****Please Note output will be differ when you run the code in your system because we are playing with the random numbers.
===============================================================
Kindly Check and Verify Thanks...PLease rate..!!!