C++ Problem. 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 it’s 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. It’s now the next player’s turn.
Requirements. • Display the game score every time the active player changes. • Use whatever techniques you think best for creating reliable code which is easy to read and maintain. Do not optimize for computer performance. Choose data structures which are easy to expand, or preferably, automatically re-size themselves (if needed). • Make your output as clear and easy to understand as possible. item Use structured programming or object oriented programming techniques. • You cannot use any data structures from the standard template library. No vectors or anything like that. • Make a simple AI with a random number. Each decision the com- puter rolls, 1-3 hold, 4-6 roll again. • Style guide elements apply: comments, layout, Program Greeting, Source File Header, and variables etc. etc.
Specification Bundles. These are additional requirements for the assignment. You code the specifications from the various groups to control the maximum potential grade you can get for this assignment. The more work you do, the better grade you can get. Specifications are bundled into groups: "A", "B", and "C". You must meet the specifications of the lowest group before I will count the specifications for the highest group. For example, you must meet the "C" specifications before I will count the "B" specifications. If you miss one element of a spec- ification bundle, that is the grade you will get for the assignment - regardless of how much extra work you do. Note: sometimes you will get comments stacking on top of each other - that’s OK even though you would never do that in the real world.
"C" Specification Bundle. 1. // Specification C1 - Time Seed Get the current time and use it for a random number seed. 2. // Specification C2 - Student Name Allow name to accept first plus last name (ie 2 words). Display it somewhere in the output. 3. // Specification C3 - Numeric Menu Use a numeric menu to collect the human players actions. See figure 1 for an example menu. 4. // Specification C4 - Bulletproof Menu Detect and re-prompt if incorrect data is entered.
"B" Specification Bundle. 1. // Specification B1 - Track each turn Keep track of the points each player scores each turn. 2. //Specification B2 - Randomize Start Randomly determine which player starts the game. 3. // Specification B3 - Alpha Menu Switch to an alphanumeric menu. Only accept the letters between the <>’s. See the next page for an example. List specification C3 like this: // Specification C3 - REPLACED. Put this comment right above specification B3. Note: Specification C4 now applies to this alphanumeric menu. 4. // Specification B4 – Resign Option The human can quit the current game and automatically restart a new one. See figure 2 for an example menu.
"A" Specification Bundle. 1. // Specification A1 - Main Event Loop Run the program in a main event loop. Prompt the player when the game ends if they want to play another game. Use a ’y’ or ’n’ prompt to stop playing games. 2. // Specification A2 - Group Stats Keep track of the number of games played, who won and lost each game and the number of turns each game took. Don’t forget to keep track of games resigned, too. You can use a struc for this is you wish. 3. // Specification A3 - Current Date Include in the program greeting the current date and time this program started. Use a library to automatically get this, do not ask the client to enter it. 4. Specification A4 - Upper and Lower Case You can accept upper and lower case letters for your menu in specification B3.
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
using namespace std;
// Function to roll a dice and returns its value
int rollDie()
{
// Generates random number between 1 and 6 inclusive
int diceValue = 1 + (rand() % 6);
return diceValue;
}// End of function
// Function to roll dice for user
// Roll until hitting a 1 or choosing to hold
int userTurn(int &userTotalScore)
{
// To store score
int userScore = 0;
// To store continue game status
bool runningStatus = true;
// To store user choice
string userChoice;
// To store rolled dice value
int diceValue = 0;
// Loops till runningStatus status is not true
do
{
// Calls the function to roll dice and stores the return value
diceValue = rollDie();
// Checks if dice value is 1 then set the running status to false
// to stop the loop
if (diceValue == 1)
{
cout << "You rolled a 1. You get no points.\n";
// Sets the running status to false to stop the loop
runningStatus = false;
}// End of if condition
// Otherwise
else
{
// Adds the dice value to score
userScore += diceValue;
// Accepts user choice
cout << "You rolled a " << diceValue
<< ". Would you like to roll again? (r - Roll again / h - Hold) ";
cin >> userChoice;
// Checks if user choice is "h" or "H"
if (userChoice == "h" || userChoice == "H")
{
// Calculates total score
userTotalScore += userScore;
// Sets the running status to false to stop the loop
runningStatus = false;
}// End of if condition
}// End of else
} while (runningStatus);// End of do - while loop
return 0;
}// End of function
// Function to roll dice for computer
// Keeps rolling until either a 1 or cumulative sum is at least 20 points
int computerTurn(int &compTotalScore)
{
// To store running status
bool runningStatus = true;
// To sore computer score
int computerScore = 0;
int diceValue = 0;
// Loops till runningStatus is not true and computer score is less than 20
do
{
// Calls the function to roll dice and stores the return value
diceValue = rollDie();
// Checks if dice value is 1 then set the running status to false
// to stop the loop
if (diceValue == 1)
{
cout << "Computer rolls a 1. Your turn.\n";
computerScore = 0;
runningStatus = false;
}// End of if condition
// Otherwise
else
// Adds the dice value
computerScore += diceValue;
} while (computerScore < 20 && runningStatus);// End of do - while loop
// Calculates the total score
compTotalScore += computerScore;
return 0;
}// End of function
// main function definition
int main()
{
srand((int)time(0));
// Sets the user and computer total score to 0
int userTotalScore = 0;
int compuerTotalScore = 0;
// Loops till user and computer total score is less than 100
while (userTotalScore < 100 && compuerTotalScore < 100)
{
// Calls the function to roll dice for user
userTurn(userTotalScore);
// Calls the function to roll dice for computer
computerTurn(compuerTotalScore);
cout << "You have " << userTotalScore << " and the computer has "
<< compuerTotalScore << ".\n";
}// End of while loop
// Checks if user total score is greater than or equals to 100
// then user win
if (userTotalScore >= 100)
cout << "You win!\n";
// Otherwise computer win
else
cout << "The Computer has " << compuerTotalScore << ". You lose!\n";
return 0;
}// End of main function
Sample Output:
You rolled a 3. Would you like to roll again? (r - Roll again /
h - Hold) r
You rolled a 1. You get no points.
You have 0 and the computer has 22.
You rolled a 4. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 5. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 4. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 6. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 5. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 6. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 1. You get no points.
Computer rolls a 1. Your turn.
You have 0 and the computer has 22.
You rolled a 6. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 1. You get no points.
You have 0 and the computer has 44.
You rolled a 3. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 1. You get no points.
You have 0 and the computer has 66.
You rolled a 1. You get no points.
You have 0 and the computer has 89.
You rolled a 1. You get no points.
Computer rolls a 1. Your turn.
You have 0 and the computer has 89.
You rolled a 6. Would you like to roll again? (r - Roll again / h -
Hold) rr
You rolled a 1. You get no points.
Computer rolls a 1. Your turn.
You have 0 and the computer has 89.
You rolled a 1. You get no points.
Computer rolls a 1. Your turn.
You have 0 and the computer has 89.
You rolled a 3. Would you like to roll again? (r - Roll again / h -
Hold) r
You rolled a 5. Would you like to roll again? (r - Roll again / h -
Hold) h
Computer rolls a 1. Your turn.
You have 8 and the computer has 89.
You rolled a 2. Would you like to roll again? (r - Roll again / h -
Hold)
Get Answers For Free
Most questions answered within 1 hours.