Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

DICE GAME in c++: A human(you or me) will be playing against the computer. Each starts with 0 points, and the game ends when either

DICE GAME in c++:

A human(you or me) will be playing against the computer. Each starts with 0 points, and the game ends when either one or both of the players obtain a score of 150 or greater.

In each round, a player rolls three dice. The human goes first followed by the computer.

In the game, getting a 1 is not a good thing. When a player rolls the dice, four outcomes are possible: 1. Getting three 1's causes the player to lose all accumulated points and lose his/her turn. The other player will then roll. At that time the following message should be printed:
Three ones, lose all holdings, and lose turn 2. Getting two 1's causes the player to lose half of all accumulated points and lose his/her turn. At that time the following message should be printed: Two ones, lose half of holdings, and lose turn The other player will then roll. Use integer division, e.g. half of 35 is 17 and half of 50 is 25. 3. Getting a single 1 causes a player to lose a turn. When a player loses a turn there is no change in the amount of accumulated points. At that time the following message should be printed: Single one, lose turn 4. When there are no 1's the human is asked whether to bank the sum of the dice that is showing.

If the player decides to bank the points, the sum that is showing is added to the player's accumulated points and the dice are passed to the other player.

If the player decides not to bank, and tries to obtain a higher sum, the dice are rolled again. And, the numbers showing on the die are evaluated once again according to the above four possible outcomes..

When the player decides to bank, the sum showing is added to the accumulated points, and the dice are passed to the other player.

The following shows the first few rounds of a game: Rolled 4, 2, 6 sum = 12 Bank(y/n)? y Computer's turn Rolled 1, 4, 4 Single one, lose turn After round 1 human has 12 and computer has 0

Rolled 5, 3, 4 sum = 12 Bank(y/n)? y Computer's turn Rolled 1, 6, 1 Two ones, lose half of holdings, and lose turn After round 2 human has 24 and computer has 0

Rolled 4, 5, 6 sum = 15 Bank(y/n)? y Computer's turn Rolled 5, 2, 6 sum = 13 no bank Rolled 5, 1, 3 After round 3 human has 39 and computer has 0

Rolled 6, 6, 2 sum = 14 Bank(y/n)? y Computer's turn Rolled 1, 3, 5 Single one, lose turn After round 4 human has 53 and computer has 0

Rolled 4, 2, 3 sum = 9 Bank(y/n)? M Bank(y/n)? N Bank(y/n)? n Rolled 2, 3, 5 sum = 10 Bank(y/n)? y Computer's turn Rolled 4, 1, 5 Single one, lose turn After round 5 human has 63 and computer has 0

The question to bank or not, accepts only a lower case y or n. Any other input should result in a re-prompt. If you think about it, the re-prompting requires a loop. Above the values of M and N were rejected.

Hint: You can use either a string variable or a char variable to capture the user's response to bank.

And the following shows the last round of a game: Rolled 2, 5, 4 sum = 11 Bank(y/n)? y Computer's turn Rolled 1, 2, 1 Two ones, lose half of holdings, and lose turn After round 19 human has 152 and computer has 12

Human won

Two blanks lines showed be printed before the very last line. That last should either be: Human won Computer won Tie

During the same round, it is possible that both the human and computer scores get to exceed 150. The one with the higher score is the winner. You only have a tie if both scores that are above 150 are equal.

Programming considerations: This programming assignment involves random numbers, if-statements and loops.

The format of all prompts and output messages should be exactly duplicated.

A round is defined as the human playing and then the computer playing.

At the beginning of a round what the human has rolled is shown, like: Rolled 4, 2, 6

If there are no 1's the next line would show the sum of the three dice, like: sum = 12 Bank(y/n)?

At that point the program is waiting for y or n to be input followed by Enter. If a value other than y or n is enter, the program reissues a prompt. This is shown at the beginning of round 5

After the human answers with y to Bank? The program should print "Computer's turn", followed by what the computer rolled as shown here. Computer's turn Rolled 3, 5, 4 sum = 12 no bank Rolled 6, 6, 2 sum = 14 bank After round 12 human has 110 and computer has 22

The program must have some intelligence whether to "bank" the sum that was rolled. If the computer rejects a sum, "no bank" should be printed after the sum. If the computer accepts a sum, "bank" should be printed. This intelligence could be: a. as simple as always accepting b. only accepting if the sum is above a certain value c. altering the sum that is acceptable (possibly holding out for a higher sum) based upon whether the computer or human is leading, and the gap between them could be a factor. Programming for c will be worth more than b, and programming for b will be worth more than a.

At the end of a round [human followed by computer] a line like the following should be printed: After round 12 human has 110 and computer has 22

Two blank lines should follow the reporting of the score, as shown here: After round 12 human has 110 and computer has 22

Rolled 3, 4, 5 sum = 12 Bank(y/n)? y Computer's turn Rolled 6, 1, 4 Single one, lose turn After round 13 human has 122 and computer has 22

Hint: It might advisable to first develop a solution for just the human player. After that part is working, with some copying and pasting and giving the computer some variable names that are similar to those used with the human's code, you could add the functionality for your opponent.

Though we have not formally talked about writing functions. I think you should be able to implement a function that rolls a die. Doing so will make your code a little easier to read.

Do not have any variables declared above main. They are global (aka external) variables. Globals should be avoided. I will talk about globals, when we cover functions. And, with functions values should be passed in as arguments. We will get to functions soon.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions