C++ A dice game called "21" It is a variation on BlackJack where one player plays against the computer trying to get 21 or as close to 21 without going over. Here are the rules of the game: You will play with dice that have numbers from 1 to 11. To win, the player or the computer has to get to 21, or as close as possible without going over. If the player or computer goes over 21, they instantly lose. If there is a tie, the computer wins. Starting the game: The player is asked to give the computer a name. For now, we'll simply call the computer opponent, "computer." The game starts with rolling four dice. The first two dice are for the player. These two dice are added up and the total outputted to the screen. The other two dice are for the computer. Likewise, their total is outputted to the screen. Player: If a total of 21 has been reached by either the player or the computer, the game instantly stops and the winner is declared. Otherwise, the player is asked if they want to roll one die that will be added to their total. Or they want to stop. The player should enter an 'r' to roll the die or an 's' to stop. The player may continue rolling until they have reached 21 or over. Finishing the game: If the player total is > 21 then the computer is declared the winner. Otherwise, the computer has to make a decision to roll or to stop. If the computer total >= the player total, the computer stops. If the computer total < the player total then the computer continues to roll one die until the computer total is >= the player total. The winner is declared Building off the game you wrote, called "21," you are going to add file writing and file reading. For each game played you will write the final results for the player and the computer. The data written should look like this: 19 21 18 23 21 16 You will have the player data first, a tab '\t', and then the computer result followed by a newline '\n' So, in the example above, in the first game, the player got 19 and the computer got 21. In the second game, the player got 18 and the computer got 23. After the player stops the program, you will close the writing file stream and open the input file stream of the newly created file. Read each line of numbers and calculate: The average result of the player for all the games played. The average result of the computer for all the games played. The total wins for the player and the computer. Then, declare who is the best 21 player! The output from above will look like this assuming the computer is named, "Ruby." Your game average was 19.3 Ruby's game average was 20.0 You won 2 out of 3 games. Ruby won 1 out of 3 games. You are the grand champion!

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 27PE
icon
Related questions
Question

C++

A dice game called "21"

It is a variation on BlackJack where one player plays against the computer trying to get 21 or as close to 21 without going over.

  • Here are the rules of the game:
    • You will play with dice that have numbers from 1 to 11.
    • To win, the player or the computer has to get to 21, or as close as possible without going over.
    • If the player or computer goes over 21, they instantly lose.
    • If there is a tie, the computer wins.  
    • Starting the game:
      • The player is asked to give the computer a name. For now, we'll simply call the computer opponent, "computer."
      • The game starts with rolling four dice.
        • The first two dice are for the player. These two dice are added up and the total outputted to the screen.
        • The other two dice are for the computer. Likewise, their total is outputted to the screen.
  •  
    • Player:
      • If a total of 21 has been reached by either the player or the computer, the game instantly stops and the winner is declared.
      • Otherwise, the player is asked if they want to roll one die that will be added to their total.
        • Or they want to stop.
        • The player should enter an 'r' to roll the die or an 's' to stop.
      • The player may continue rolling until they have reached 21 or over.
  •  
    • Finishing the game:
      • If the player total is > 21 then the computer is declared the winner.
      • Otherwise, the computer has to make a decision to roll or to stop.
        • If the computer total >= the player total, the computer stops.
        • If the computer total < the player total then the computer continues to roll one die until the computer total is >= the player total.
      • The winner is declared

Building off the game you wrote, called "21," you are going to add file writing and file reading. 

  • For each game played you will write the final results for the player and the computer.
    • The data written should look like this:

19            21

18            23

21            16

    • You will have the player data first, a tab '\t', and then the computer result followed by a newline '\n' 
      • So, in the example above, in the first game, the player got 19 and the computer got 21.
      • In the second game, the player got 18 and the computer got 23.

 

  • After the player stops the program, you will close the writing file stream and open the input file stream of the newly created file.
    • Read each line of numbers and calculate:
      • The average result of the player for all the games played.
      • The average result of the computer for all the games played.
      • The total wins for the player and the computer.
    • Then, declare who is the best 21 player!
    • The output from above will look like this assuming the computer is named, "Ruby."

Your game average was 19.3

Ruby's game average was 20.0

You won 2 out of 3 games.

Ruby won 1 out of 3 games.

You are the grand champion!

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Program on Numbers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning