Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program that plays the game of Yahtzee. Your game will allow 1-2 players, and at the end, you need to ask if

Write a C++ program that plays the game of Yahtzee.

Your game will allow 1-2 players, and at the end, you need to ask if the users want to play again. There are 5 six-sided dice in the game and you get three rolls to try to make something on the scoresheet. You must use an array to hold the values of the 5 dice and an array to hold the scores for the 16 categories for a player.

The top half of the scoresheet is only a sum of 1 or more of dice you are trying to get: 1s, 2s, 3s, 4s, 5s, and 6s, and if you score 63 or more points in this section, you get an additional 35 points.

The bottom half of the scoresheet consist of options that have pre-defined points or a total of all the dice. You have the options for a three of a kind (total of all dice, but has to have 3 or more of the same number), four of a kind (total of all dice, but has to have 4 or more of the same number), full house (25 pts, 2 dice the same and 3 dice the same), small straight (30 pts, 4 numbers in a row), large straight (40 pts, 5 numbers in a row), Yahtzee (50 pts, 5 dice the same number), up to 3 bonus Yahtzees (100 pts each, 5 dice the same number), and chance (total of all dice, any combination).

After your first roll, a player can choose to roll 0-5 specific dice again. The player needs to state how many dice they want to re-roll and which ones they want to re-roll by providing the array indices for each die. If the player chooses not to roll again or has rolled 3 times, then the player must choose a category not previously chosen on the scoresheet. If the dice do not match the rules for that category, then the player takes a 0 for that category. Once a category has a score (even a zero score), it cannot be chosen again, so think carefully about what value you are going to use to initialize the scoresheet!!!

The bonus Yahtzee can only be chosen if there is a non-zero score in the Yahtzee!!! The players turn is over once a category is chosen, and the game is over once all players have put a score in all categories. The final score is a total point from the two sections, including the bonus if the top section is 63 or greater.

Command Line Arguments

To support various numbers of players, you will run the program with the specific number at runtime, rather than prompting for the input. The user can enter the number of players, 1-2. Any other command line argument is an error.

1-player Example:

./yahtzee 1 

In addition to the earlier specifications, your program must meet these requirements:

Print an error message and recover, when the player doesnt supply a valid category. This includes selecting a category with a score, which can include a zero for the score. Make sure to carefully consider this when you are designing your program!

Print an error message when the user enters an invalid option as a command line argument. You do not need to recover from this.

Correctly determine the score for the category based on the dice (Remember, if the dice do not match the rules for the category, then a zero score is placed in that category).

Play the game correctly based on rules and number of players.

Continue to play until the user selects no.

You must not have any global variables

You must use a dynamic 1-d array for the second players scoresheet since there may or may not be a second player.

Your functions need to focus on performing a particular task. In other words, you need to use good modular design. If your function uses more than about 20 lines of code, this may be an indication that the code should be split into multiple functions. If the TA notices that your code is not sufficiently modular, you will lose points.

You must not have memory leaks.

Segmentation faults are not allowed (e.g. your program crashes).

No vectors

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

Step: 3

blur-text-image

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

The Routledge Companion To Accounting In Emerging Economies

Authors: Pauline Weetman, Ioannis Tsalavoutas

1st Edition

0367783819, 978-0367783815

Students also viewed these Databases questions