Question: Can someone please fix my code with the requirement from below? C++ You are coding a simple game called Pig. Players take turns rolling a
Can someone please fix my code with the requirement from below? C++
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If its is a: 1: You lose your turn, no turn total points are added to your grand total. 2-6: The number you rolled is added to your turn total. Hold: Your turn total is added to your grand total. Its now the next players turn. Problem Features.
These are additional details to help you solve the programming problem.
Make a simple AI with a random number. Each decision the com- puter rolls, 1-3 hold, 4-6 roll again.
"C" Specification Bundle. // Specification C1 - Fixed Seed Set the random number seed to a specific integer. That way, it will always generate the same random number sequence - easier to fix bugs that way. // Specification C2 - Student Name Allow name to accept first plus last name (ie 2 words). Display it somewhere in the output. // Specification C3 - Numeric Menu Use a numeric menu to collect the human players actions. See figure 1 for an example menu. // Specification C4 - Bulletproof Menu
Detect and re-prompt if incorrect data (i.e. not 1, 2, or 3) is en- tered.
"C" Bundle Menu Options. 1. 1. Roll 2. 2. Hold 3. 3. Quit Figure 1: Allowable menu options for this bundle.
"B" Specification Bundle. // Specification B1 - Display Turn Stats Keep track of the points each player scores each turn as well as the overall points both sides have each turn in the game. //Specification B2 - Display Due Date Display the date this assignment is due in your program greeting section. // Specification B3 - Hi Score on Heap Store the games high score in a variable in the heap. Dont forget to clean up after yourself before the program quits. // Specification B4 Display High Score Display the value you stored in B3 on the console at the end of the game. "A" Specification Bundle. // Specification A1 - D6() function
pig (pig) 3
This method returns a randomly generated int between 1 and 6 every time its called. Replace the code which does that in your assignment with this. // Specification A2 - RandomNumber() function Create a function which generates a random number between arguments lo and hi. // Specification A3 - Protect RandomNumber() input Using an if statement, protect your RandomNumber() function from bad data. Return a -1 error code if RandomNumber() receives bad input. Argument rules are: 1. Hi must be greater than lo. 2. Lo cannot be less than 1. 3. Hi cannot be greater than 100. // Specification A4 - Protect RandomNumber() output Make sure your RandomNumer() function doesnt send bad output. Use an if statement before your return to make sure the random
number isnt greater than 100. Return a -2 error code if you generate-bad output.
Here is my code: I have completed C1, C2, and I think I've did C3 but I am not sure
#include
#include
#include
#include
using namespace std;
// Function to roll a dice and returns its value
int rollDie()
{
// Generates random number between 1 and 6
int diceValue = 1 + (rand() % 6);
return diceValue;
}
// Function to roll dice for user
int userTurn(int &userTotalScore)
{
int userScore = 0;
bool runningStatus = true;
// To store user choice
string userChoice;
int diceValue = 0;
do
{
diceValue = rollDie();
if (diceValue == 1)
{
cout << "You rolled a 1. You get no points. ";
runningStatus = false;
}
else
{
// Adds the dice value to score
userScore += diceValue;
cout << "You rolled a " << diceValue
// Specification C3 - Numeric Menu (Lines 39-49)
<< ". Would you like to roll, hold or quit? (1 - Roll again / 2 - Hold / 3 - Quit) ";
cin >> userChoice;
if (userChoice == "2")
{
// Calculates total score
userTotalScore += userScore;
runningStatus = false;
}
}
if (userChoice == "3")
exit(0);
} while (runningStatus);
return 0;
}
int computerTurn(int &compTotalScore)
{
bool runningStatus = true;
int computerScore = 0;
int diceValue = 0;
do
{
// Calls the function to roll dice and stores the return value
diceValue = rollDie();
if (diceValue == 1)
{
cout << "Computer rolls a 1. Your turn. ";
computerScore = 0;
runningStatus = false;
}
else
// Adds the dice value
computerScore += diceValue;
} while (computerScore < 20 && runningStatus);
// Calculates the total score
compTotalScore += computerScore;
return 0;
}
//Specification C2 - Student Name
void playerName()
{
string fName, lName;
string fullName;
cout << "What is your first name?: ";
cin >> fName;
cout << " What is your last name?: ";
cin >> lName;
fullName = fName + " " + lName;
cout << " Player Name: " << fullName << endl;
}
// Main Function
int main()
{
// Specification C1 - Fixed Seed
unsigned seed = 123;
srand(seed);
playerName();
srand((int)time(0));
int userTotalScore = 0;
int compuerTotalScore = 0;
while (userTotalScore < 100 && compuerTotalScore < 100)
{
userTurn(userTotalScore);
computerTurn(compuerTotalScore);
cout << "You have " << userTotalScore << " and the computer has "
<< compuerTotalScore << ". ";
}
// Checks if user total score is greater than or equals to 100
// then user win
if (userTotalScore >= 100)
cout << "You win! ";
else
cout << "The Computer has " << compuerTotalScore << ". You lose! ";
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
