Question

C++ Problem. You are coding a simple game called Pig. Players take turns rolling a die....

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.

Homework Answers

Answer #1

#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)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You are coding a simple game called Pig. Players take turns rolling a die. The die...
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,...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
One of the most popular games of chance is a dice game known as “craps”, played...
One of the most popular games of chance is a dice game known as “craps”, played in casinos around the world. Here are the rules of the game: A player rolls two six-sided die, which means he can roll a 1, 2, 3, 4, 5 or 6 on either die. After the dice come to rest they are added together and their sum determines the outcome. If the sum is 7 or 11 on the first roll, the player wins....
You and a friend are rolling a set of 7 dice. The game works such that...
You and a friend are rolling a set of 7 dice. The game works such that if a die shows the values 1, 2, 3, or 4 you will get a point for that die. Each die that shows 5 or 6 your friend will get a point for. Construct a probability model for a single roll of the dice then answer the following. A)What is the probability you made 2 points? B)What is the probability that your friend will...
You and a friend are rolling a set of 5 dice. The game works such that...
You and a friend are rolling a set of 5 dice. The game works such that if a die shows the values 1, 2, 3, or 4 you will get a point for that die. Each die that shows 5 or 6 your friend will get a point for. Construct a probability model for a single roll of the dice then answer the following. Step 1 of 5: What is the probability you made 2 points? Step 2 of 5:...
You and a friend are rolling a set of 6 dice. The game works such that...
You and a friend are rolling a set of 6 dice. The game works such that if a die shows the values 1, 2, or 3 you will get a point for that die. Each die that shows 4, 5, or 6 your friend will get a point for. Construct a probability model for a single roll of the dice then answer the following. Step 1 of 5: What is the probability you made 2 points? Step 2 of 5:...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each disk will include some marker to denote to whom it belongs.  At the end...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a 3x3 grid. The winner is the first player to place 3 consecutive marks in a horizontal, vertical or diagonal row. Understanding the Problem: In this assignment, you will: 1) Create a tic tac toe board data structure. 2) Check for game over. 3) Clear the tic tac toe game for each game. 4) Display the tic tac toe board to screen. 5) Simulate playing...
Problem 4)A simplified version of the dice game 10,000 is played using 5 dice. The player...
Problem 4)A simplified version of the dice game 10,000 is played using 5 dice. The player rolls the 5 six-sided dice, each 1 that is rolled, the player achieves a score of 100. A) How many possible ways are there to roll i l's over these 5 dice? (Hint: use combinations) B) The probability of any one dice rolling to a value of 1 is 1/5 (since each dice has six sides). Use the binomial probability distribution. Calculate the probability...
The premise of this project is to make a basic GUI and code for the game...
The premise of this project is to make a basic GUI and code for the game Keno. The specifics of the gui and game are below. Description: In this project you will implement the popular casino and state lottery game, Keno. This is a somewhat simple game to understand and play which should allow you to focus on learning GUI development in JavaFX and trying your hand at event driven programing. Implementation Details: You may add as many classes, data...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT