CSCI 261 - Programming Concepts (C++)

Summer 2018 - A5 - Yahtzee!

Quick Links: Canvas | CS @ Mines | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |
This assignment is due by Friday, June 01, 2018 11:59 PM.



Yahtzee!



For this assignment, we are going to implement most of a Yahtzee game and combine everything we have learned to date.

The first step to make Yahtzee is to know how to play. Here are the official rules. You can also play online here. Be sure to take note of two pieces of information each round:
  1. What conditions must be satisified to earn points
  2. How many points are scored for each category
Play through a complete game. Who won?

After you've played at least one complete game, now it's time to start building out the game. We will do this incrementally, so be sure you have the current step working and tested before moving on to the next piece.


Rolling the Dice



Yahtzee is a dice game and a game of random chance. We need to simulate this random chance of a die rolling. Begin by creating a function called rollDie that has no input and returns an int as output. This function should generate a random value between 1 and 6, inclusive. Once you've tested your function is working properly, move on to the next step.

Next, we need to represent a hand of dice. From above, we are representing a single die as an int. Therefore, to represent a hand of dice, we will use an array of ints. Create a global constant to denote the hand size (of 5), since we will be using this value all over our program.

Once this array is created, let's create a function called printHand that accepts the array as input and has no output. Be sure to look ahead to zyBooks 5.13 about how to pass arrays to a function, we will cover this next week. This function should simply print out each element of the array to the screen.

We'll now make a third function called rollDice that accepts the array as input and has no output. The function should then call the rollDie function and assign each element of the array with a random value. At this point, your program should have output similar to:

Your hand is: 4 1 2 5 6

When you are happy with the operation of these two functions, continue on.


Playing a Round



Let's begin putting in place the mechanism for the player to roll three times. Wrap your prior code to roll and print a hand of dice in a loop. This loop should run three times or stop early if the player decides not to roll again. An example of these two scenarios is shown below:

Your hand is: 1 6 2 1 5
2 rolls remaining.
Do you want to roll again? (Y or N) Y
Your hand is: 2 1 6 2 5
1 rolls remaining.
Do you want to roll again? (Y or N) Y
Your hand is: 6 4 3 1 4
0 rolls remaining.

Your hand is: 1 5 2 3 2
2 rolls remaining.
Do you want to roll again? (Y or N) Y
Your hand is: 4 4 4 6 6
1 rolls remaining.
Do you want to roll again? (Y or N) N

Next, if the user chooses to roll again we need to ask the user which dice they would like to keep. We'll need to ask the player for each die if they want to keep it or roll it again. We'll store the player's decision in an array of bools. Asking the player which dice to keep is only the first part of this step. The second step is to also pass this second array as a second parameter to the rollDice function. We also need to modify the body of the rollDice function to only reroll the dice that the user chose not to keep. Now, our game looks like below:

Your hand is: 2 2 5 1 3
2 rolls remaining.
Do you want to roll again? (Y or N) Y
Do you want to keep die #1? (Y or N) Y
Do you want to keep die #2? (Y or N) Y
Do you want to keep die #3? (Y or N) N
Do you want to keep die #4? (Y or N) N
Do you want to keep die #5? (Y or N) N
Your hand is: 2 2 2 6 5
1 rolls remaining.
Do you want to roll again? (Y or N) Y
Do you want to keep die #1? (Y or N) Y
Do you want to keep die #2? (Y or N) Y
Do you want to keep die #3? (Y or N) N
Do you want to keep die #4? (Y or N) N
Do you want to keep die #5? (Y or N) Y
Your hand is: 2 2 3 4 5
0 rolls remaining.


Checking for Conditions



After the player has rolled three times, or chosen to not roll again, we need to inspect the current hand of dice and determine which scoring scenarios are present and their corresponding score. This part will give you practice writing functions, as you will write at least 13 of them (probably 15 if done properly). We need to write a function for each of the scenarios below. The table lists each scenario, the conditions for it to be scored, and the corresponding score if present. You should notice that all of the functions have a similar signature (same input/output but different names). We'll want to name the functions using the following scheme: scoreOnes, scoreTwos, etc.

Scenario Description Points
Ones Ones Sum of Ones
Twos Twos Sum of Twos
Threes Threes Sum of Threes
Fours Fours Sum of Fours
Fives Fives Sum of Fives
Sixes Sixes Sum of Sixes
Three Of A Kind Three dice of the same value Sum of Dice
Four Of A Kind Four dice of the same value Sum of Dice
Full House Two dice of one value and three dice of a different value 25
Small Straight Four dice in a row (1-4, 2-5, 3-6) 30
Large Straight Five dice in a row (1-5, 2-6) 40
Yahtzee Five dice of the same value 50
Chance Any combination of dice Sum of Dice

After you complete the first two functions for scoring ones and twos, have you noticed that they are very similar save for one detail? Perhaps that can be extracted into its own function that can then be called six times. When you are happy with the performance of each function, we will now ask the player which category they want to score. Present the player with a menu like the following:

Which category do you want to score this hand as?
( 1) Ones
( 2) Twos
( 3) Threes
( 4) Fours
( 5) Fives
( 6) Sixes
( 7) Three of a Kind
( 8) Four of a Kind
( 9) Full House
(10) Small Straight
(11) Large Straight
(12) Yahtzee!
(13) Chance
Category #: 

When the player provides their selection, print the corresponding score for that category.


Play the Game



Now to pull it all together. We now have the functionality to play one round of a Yahtzee game. We want the player to be able to play an entire game of Yahtzee. An entire game consists of 13 rounds. We need to have three components incorporated:
  1. The user can play thirteen rounds - each consisting of rolling and choosing a category to score.
  2. We need to keep track of the player's total score across all rounds.
  3. The player cannot score the same category twice.
A sample play of the first two rounds is shown below:

Your hand is: 4 5 3 1 2
2 rolls remaining.
Do you want to roll again? (Y or N) N

Which category do you want to score this hand as?
( 1) Ones
( 2) Twos
( 3) Threes
( 4) Fours
( 5) Fives
( 6) Sixes
( 7) Three of a Kind
( 8) Four of a Kind
( 9) Full House
(10) Small Straight
(11) Large Straight
(12) Yahtzee!
(13) Chance
Category #: 11
Your current score is: 40

Your hand is: 2 1 2 4 1
2 rolls remaining.
Do you want to roll again? (Y or N) Y
Do you want to keep die #1? (Y or N) Y
Do you want to keep die #2? (Y or N) Y
Do you want to keep die #3? (Y or N) Y
Do you want to keep die #4? (Y or N) N
Do you want to keep die #5? (Y or N) Y

Your hand is: 2 1 2 2 1
1 rolls remaining.
Do you want to roll again? (Y or N) N

Which category do you want to score this hand as?
( 1) Ones
( 2) Twos
( 3) Threes
( 4) Fours
( 5) Fives
( 6) Sixes
( 7) Three of a Kind
( 8) Four of a Kind
( 9) Full House
(10) Small Straight
(12) Yahtzee!
(13) Chance
Category #: 9
Your current score is: 65

Your hand is: 5 4 1 2 1
2 rolls remaining.
Do you want to roll again? (Y or N) N

Which category do you want to score this hand as?
( 1) Ones
( 2) Twos
( 3) Threes
( 4) Fours
( 5) Fives
( 6) Sixes
( 7) Three of a Kind
( 8) Four of a Kind
(10) Small Straight
(12) Yahtzee!
(13) Chance
Category #: 8
Your current score is: 65

Congratulations! You've implemented a fully functioning yahtzee game at this point. See what high score you can get.


Hints



  • You'll probably want to use another array to keep track of the score.


Functional Requirements



  • All functions should be placed in separate files. The function prototypes should be placed in to a .h file and the corresponding function definitions should be placed in to a .cpp file.
  • Use const appropriately for parameters.


Grading Rubric


Your submission will be graded according to the following rubric.

PointsRequirement Description
2 All code submitted properly.
12 All labs completed and submitted
10 Game is properly playable.
4 Functional requirements above met.
2 Game is properly randomized.
2 (1) Comments used
(2) Coding style followed
(3) Appropriate variable names, constants, and data types used
(4) Instructions followed
32 Total Points

This assignment is due by Friday, June 01, 2018 11:59 PM.


Submission


Always, always, ALWAYS update the header comments at the top of your main.cpp file. And if you ever get stuck, remember that there is LOTS of help available. The following instructions are copied from How to Submit Homework.


It is critical that you follow these steps when submitting homework.

If you do not follow these instructions, your assignment will receive a major deduction. Why all the fuss? Because we have several hundred of these assignments to grade, and we use computer tools to automate as much of the process as possible. If you deviate from these instructions, our grading tools will not work. And that makes us very unhappy. And when we're unhappy, we give penalties. Thus, make us happy.


Submission Instructions



Here are step-by-step instructions for submitting your homework properly:
  1. File and folder names are extremely important in this process. Please double-check carefully, to ensure things are named correctly.
    1. The top-level folder of your project must be named Set5
    2. Inside Set5, create 7sub-folders that are required for this Set. The name of each sub-folder is defined in that Set (e.g.,L5A, L5B, L5C, L5D, L5E, L5F, and A5).
    3. Copy your program main.cpp and supporting files into the subdirectories of Set5 (steps 1-2), zip this Set5 folder (steps 3-4), and then submit the zipped file (steps 5-11) to Canvas.
    4. For example, when you zip/submit Set5, there will be 7 sub-folders called L5A, L5B, L5C, L5D, L5E, L5F, and A5 inside the Set5 folder, and each of these sub-folders will have a file called main.cpp and nothing else.

  2. Using Windows Explorer (not to be confused with Internet Explorer), find the file named "main.cpp" located inside the folder for the particular lab or homework assignment you will submit. For example, find main.cpp in your Z:\CSCI261\Set5\ folder. Repeat this for all supporting files.

    STOP: Are you really sure you are viewing the correct assignment's folder?

  3. Now, for A5, right click on the main.cpp to copy the file. Then, return to the Set5/A5 folder and right click to paste the file. In other words, put a copy of your homework's main.cpp source code and supporting files into the Set5/A5 folder.

    Follow the same steps for L5A, to put a copy of your lab's main.cpp and supporting files into the Set5/L5A folder. Repeat this process for Set5/L5B, Set5/L5C, Set5/L5D, Set5/L5E, Set5/L5F.

    STOP: Are you sure your Set5 folder now has all your code to submit?

  4. Now, right-click on the "Set5" folder.
    1. In the pop-up menu that opens, move the mouse "Send to..." and expand the sub-menu.
    2. In the sub-menu that opens, select "Compressed (zipped) folder".

    STOP: Are you really sure you are zipping a Set5 folder with sub-folders that each contain a main.cpp file in it?

  5. After the previous step, you should now see a "Set5.zip" file.

  6. Now visit the Canvas page for this course and click the "Assignments" button in the sidebar.

  7. Find Set5, click on it, find the "Attach file" area, and then click the "Browse My Computer" button.

  8. Find the "Set5.zip" file created earlier and click the "Open" button.

    STOP: Are you really sure you are selecting the right homework assignment? Are you double-sure?

  9. WAIT! There's one more super-important step. Click on the blue "Submit" button to submit your homework.

  10. No, really, make sure you click the "Submit" button to actually submit your homework. Clicking the "Open" button in the previous step kind of makes it feel like you're done, but you must click the Submit button as well! And you must allow the file time to upload before you turn off your computer!

  11. Canvas should say "This assignment is complete. Click OK to review the results.". Click "OK" and view the files within the zip file you submitted. In other words, verify you submitted what you think you submitted!
In summary, you must zip the "Set5" folder and only the "Set5" folder, this zip folder must have several sub-folders, you must name all these folders correctly, you must submit the correct zip file for this homework, and you must click the "Submit" button. Not doing these steps is like bringing your homework to class but forgetting to hand it in. No concessions will be made for incorrectly submitted work. If you incorrectly submit your homework, we will not be able to give you full credit. And that makes us unhappy.


This assignment is due by Friday, June 01, 2018 11:59 PM.

Last Updated: 05/24/18 23:35


Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0