This is a bonus programming exercise. Submitting it is not a must. However, solv
ID: 3582415 • Letter: T
Question
This is a bonus programming exercise. Submitting it is not a must. However, solving it will give you up to $ bonus points on your final grade. You can do this exercise individually or as a team. The team can have no more than two students. In this game, the user plays against the computer. The two players (the user and the computer) take turns in rolling two die. Both the players race to reach a score of 50. The first player who scores 50 or more points wins. The game starts with the user's turn. Each turn, a player repeatedly rolls two die until either a 1 is rolled on any of the two die or the player decides to "hold". The following are the rules of the game: If the player does not roll a 1 on any of the two die, the sum of the two die is added to his turn-total and the player's turn continues. If the player rolls a 1 on one of the two die, he score nothing and it becomes the next player's turn. If 1s are rolled, the player's entire total score is lost, and the turn ends. If a player chooses to "hold", his tum-total is added to his/her total score, and it becomes the next player's turn. The first player to score 50 or more points wins. Program Requirements Your program should be resilient to having wrong input. The program input should not be case sensitive. Therefore, if the user enters an input character (e.g., ' H' for hold), the program should accept and interpret both small and capital characters in the same way (e.g, both ' H' and 'h' are accepted as the input to hold). You can make the computer player follow any intelligent strategy to win. However, to simplify the requirements, you can make it have the strategy of making random choices with 50% chance for rolling and 50% for holding. For example, the following C++ statement will set the computer response to 'R' for rolling, or 'H' for holding with 50% probability for each: response = rand()%2? 'R': 'H'; Pseudo Code The following shows a sample pseudo code for this game program: Initialize turnTotal, userScore and computerScore to zero Initialize turn to user Loop until one of the scores reaches 50 or higher If user turn Call function to handle the user turnExplanation / Answer
Hello ,
I expalined all the code with Comment. Please add comment if you have any doubts
Source code :-
-----------------------------------------
#include <iostream>
using namespace std;
volatile int userScore = 0;
volatile int systemScore = 0;
//Function Prototype for User Turn
void userTurn();
//Function Prototype for System Turn
void systemTurn();
int main(int argc, char *argv[])
{
cout << "Welcome.." << endl;
userTurn();
return 0;
}
//Function Definition for System Turn
void systemTurn(){
cout<<"Enter System Turn"<<endl;
//Run loop for System turn, until Hold or '1' is rolled on any Dice or Score Reached 50
while(true){
//Variable to store user input (H/R)
char input = 0;
/**
* get Random System Choice
* R : Roll , H : Hold
*/
input = rand()%2 ? 'R' : 'H';
//USer asked to Hold then System Turn will Start
if( input == 'H'){
cout<<"System Gives Turn to you"<<endl;
break;
}
//User choice is Rool the Dice
if(input == 'R'){
//Roll Result of Dice1 and Dice2
int diceVal1,diceVal2 = 0;
//To Roll the Dice , Generate a Random Number between 1..6
diceVal1 = rand() % 6 + 1;
cout << "System Dice-1 Result :" << diceVal1<<endl;
diceVal2 = rand() % 6 + 1;
cout << "System Dice-2 Result :" << diceVal2<<endl;
//Both Dice result is 1 so make user Score to 0 and Start System Turn
if(diceVal1 == 1 && diceVal2 == 1){
userScore = 0;
break;
}
//Any of Dice result is 1 so make Start System Turn
else if(diceVal1 == 1 || diceVal2 == 1){
break;
}
//Add user Score
else{
systemScore += (diceVal1 +diceVal2);
if(systemScore >= 50){
cout << "System Won the Match" << endl;
break;
}
else{
continue;
}
}
}
}
//Start System Turn if USer Score is below 50
if(systemScore < 50 && userScore < 50){
cout<<"Exit System Turn going to user turn"<<endl;
userTurn();
}
else{
cout<<"Exit System Turn"<<endl;
}
}
//Function Definition for User Turn
void userTurn(){
cout<<"Enter User Turn "<<endl;
//Run loop for user turn, until Hold or '1' is rolled on any Dice or Score Reached 50
while(true){
//Variable to store user input (H/R)
char input = 0;
/**
* Ask User About Choice
* R : Roll , H : Hold
*/
cout << "Enter Your Choice" << endl;
cout << "R : Roll" << endl;
cout << "H : Hold" << endl;
input = getchar();
//Ignore Enter key
if(input == ' ' ){
input = getchar();
}
//USer asked to Hold then System Turn will Start
if(input == 'h' or input == 'H'){
break;
}
//User choice is Rool the Dice
else if(input == 'r' or input == 'R'){
//Roll Result of Dice1 and Dice2
int diceVal1,diceVal2 = 0;
//To Roll the Dice , Generate a Random Number between 1..6
diceVal1 = rand() % 6 + 1;
cout << "Your Dice-1 Result :" << diceVal1<<endl;
diceVal2 = rand() % 6 + 1;
cout << "Your Dice-2 Result :" << diceVal2<<endl;
//Both Dice result is 1 so make user Score to 0 and Start System Turn
if(diceVal1 == 1 && diceVal2 == 1){
userScore = 0;
break;
}
//Any of Dice result is 1 so make Start System Turn
else if(diceVal1 == 1 || diceVal2 == 1){
break;
}
//Add user Score
else{
userScore += (diceVal1 +diceVal2);
if(userScore >= 50){
cout << "Congatulations , Your Won the Match" << endl;
break;
}
else{
continue;
}
}
}
//USer choice is wrong so ask choice again
else{
cout << "Your Choice is Wrong" << endl;
continue;
}
}
//Start System Turn if USer Score is below 50
if(userScore < 50 && systemScore < 50){
cout<<"Exit User Turn going to System turn"<<endl;
systemTurn();
}
else
cout<<"Exit User Turn "<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.