Write a program for the following dice game. You start with 10 points. The compu
ID: 3685338 • Letter: W
Question
Write a program for the following dice game. You start with 10 points. The computer will roll 2 dice and display what it rolled. It is now your turn to roll two dice, but you first must wager points ranging from 1 to the number of points you currently have. If you roll higher than what the computer rolled, you double the points you wagered. If you don’t, you lose your points. The game continues until you run out of points or when you enter -1 to stop.
You should display the rules of the game at the beginning. Each iteration, you should display how many rounds have passed, what the computer rolled, how many points the player has, and the range that they are able to wager in this round. You should also validate that the user has entered a valid wager.
Include functions: void displayGameRules()
int rollDie()
bool playerWins(int playerRoll, int computerRoll)
int calcNewScore(int playerWager, bool win)
bool validWager(int wager)
Explanation / Answer
// Example program
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void displayGameRules()
{
cout<<"You have ten points now."<<' ';
cout<<"In each round of the game first the computer rolls the two dice and the total score is diaplayed"<<' ';
cout<<"Then you wager some points between 1 to the points you currently have"<<' ';
cout<<"If you roll higher than the computer then you win"<<' ';
cout<<"If you win you get double the points you wagered, but if you lose you lose double the points you wagered"<<' ';
cout<<"The game stops when you lose all the points or input -1"<<' ';
}
int rollDie()
{
srand(time(NULL));
int r1 = rand()%6+1;
return r1;
}
bool playerWins(int playerRoll, int computerRoll)
{
if(playerRoll>computerRoll)
{
return true;
}
return false;
}
int calcNewScore(int playerWager, bool win)
{
if(win==true)
{
return 2*playerWager;
}
return -2*playerWager;
}
bool validWager(int wager,int score)
{
if(wager<=score)
{
return true;
}
return false;
}
int main()
{
int score=10;
int round=1,wager;
int croll,proll;
displayGameRules();
while(score>0)
{
cout<<"We are in round "<<round<<' ';
croll = rollDie()+rollDie();
cout<<"Computer roll is:"<<croll<<' ';
cout<<"Your score is:"<<score<<' ';
cout<<"You can wager from 1 to "<<score<<' ';
cout<<"Please enter your wager(-1 to stop the game):";
cin>>wager;
if(wager==-1)
break;
if(validWager(wager,score))
{
cout<<"Proper wager"<<' ';
}
else
{
cout<<"Invalid wager"<<' ';
}
proll = rollDie()+rollDie();
cout<<"Your roll is:"<<proll<<' ';
bool jk = playerWins(proll,croll);
if(jk)cout<<"You won this round ";
else cout<<"Computer won this round ";
score = score + calcNewScore(wager,jk);
cout<<"Your updated score is:"<<score<<' ';
round++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.