Homework: Yahtzee

Assigned
  • January 29, 2026
Due
  • February 12, 2026 10:00pm
Collaboration
    Regular policies for collaboration apply to this homework assignment. Make sure you review and understand the policies on the syllabus before you discuss your work on this assignment with anyone else.
Submitting
    Submit your completed yahtzee.c source file on Gradescope. You can submit as many times as you want up until the deadline. Make sure the final version of your implementation is committed and pushed to your git repository by the deadline.

Your task for this assignment is to implement a simplified version of the class dice game Yahtzee. The sections below outline the rules for the game you’ll be implementing, what you’ll need to get started on the assignment, and how your work will be assessed.

Game Rules

Please read this section carefully even if you are already familiar with Yahtzee. These rules are simplified from the full game in ways that should make your implementation much less complex.

First, the basics: you’ll be implement a single player version of Yahtzee. The game will last for six turns, and each turn follows the same progression:

  1. The player rolls five six-sided dice.
  2. The player can choose any subset of the five dice to re-roll. The player could choose to re-roll all the dice, none of the dice, or any number in-between.
  3. The player can again choose a subset of the five dice to re-roll.
  4. The player chooses an unused line on the score sheet to use for this turn. The game calculates the score for the roll and enters it in the chosen line.

The score sheet has six different lines, and each can be used only once. Each line has a different scoring calculation, which is as follows:

  1. Ones: Calculate the score by adding up all the dice that show a one after the third roll. If the five dice have final values of 1, 3, 2, 1, and 1 then the score on this line would be 1 + 1 + 1 = 3.

  2. Twos: Calculate the score by adding up all the twos. If the five dice have final values of 2, 5, 2, 6, and 1 then the score on this line would be 2 + 2 = 4.

  3. Threes: Calculate the score by adding up all the threes. If the five dice have final values of 3, 1, 3, 3, and 6 then the score on this line would be 3 + 3 + 3 = 9.

  4. Fours: Calculate the score by adding up all the fours. If the five dice have final values of 1, 3, 6, 2, and 1 then the score on this line would be zero because there were no fours rolled.

  5. Fives: Calculate the score by adding up all the fives. If the five dice have final values of 5, 5, 5, 5, and 5 then the score on this line would be 5 + 5 + 5 + 5 + 5 = 25.

  6. Sixes: Calculate the score by adding up all the sixes. If the five dice have final values of 6, 5, 5, 4, and 4 then the score on this line would be 6.

At the end of each turn, the player can choose any of the score lines to use as long as they haven’t been used during the current game. In the final turn of the game there will be no choice about where to enter the score, since there will only be one open line on the score sheet. The player cannot choose to skip scoring a turn; every turn must be entered in an empty space on the score sheet even if it gives a score of zero.

An Example Turn

Let’s walk through one turn of the game as an example. We start by rolling all five dice, which give the values:

1, 2, 3, 2, 6

This looks like a promising first roll for the twos line on the score sheet, so we’ll re-roll the 1, 3, and 6. That gives this result (with new rolls shown in bold):

2, 2, 4, 4, 4

Now it looks like fours may be a better choice for this turn, so we’ll re-roll the twos for the third and final roll. We’re left with the final result (new rolls in bold):

4, 4, 4, 1, 1

If the fours line of the score sheet is available, the player could enter a score of 12 for this turn. But they can pick any available score line they want. The scores for each line would be as follows:

ones:
score is 2 (calculated by adding the two ones)
twos:
score is 0 (no twos)
threes:
score is 0 (no threes)
fours:
score is 12 (three fours)
fives:
score is 0 (no fives)
sixes:
score is 0 (no sixes)

The player might choose the ones line if they think they can get more than three fours on a future turn. Or, if the ones and fours lines are already taken they’d have to pick one of the zero lines. But let’s say for this example that the fours line is available so the player enters a score of 12 on the fours line.

The next turn repeats this whole process, except the player cannot use the fours score line for the rest of the game.

Your Task

Implement the simplified single player Yahtzee game described above. You will need to decide how and when to prompt users to make decisions during the game. Do your best to make the game understandable and easy to use, with the obvious limitation that it is a text-only interface.

You must complete your work for this assignment using a git repository on the class git server. The instructor will create this repository for you. When you log in you should see a repository called yahtzee-USERNAME, where USERNAME is your college username. You have access to modify this repository directly, so do not create a fork. Instead, run the commands below to clone the repository and open it with VSCode:

$ cd ~/csc161/homework
$ git clone https://git.cs.grinnell.edu/csc161/yahtzee-USERNAME.git yahtzee
$ cd yahtzee
$ code .

Make sure you replace USERNAME in the example above with your Grinnell username (not your full email address) so you have the correct repository path. Notice that this time we are using git clone with an extra option at the end. This tells git to save the cloned repository in a directory named yahtzee isntead of yahtzee-USERNAME.

If you are unable to access your git repository, please contact the instructor immediately! The repository could be configured incorrectly, but you may also have an issue with the configuration of your user account on the git server that we should address as soon as possible.

You have quite a bit of flexibility about how you approach this assignment. Use your experience with the rock, paper, scissors game and your growing C expertise to make decisions that keep your implementation clear and manageable. Don’t forget to commit and push changes frequently. If you run into any issues with git please contact course staff as soon as possible so we can help you resolve them.

The starter code includes a Makefile, which means you can compile your code with the following command:

$ make
clang -g -Wall -Werror -o yahtzee yahtzee.c

The make command will run clang automatically, and it prints the command when it runs it, as in the example above. But, if you haven’t edited yahtzee.c since the last time you compiled the program then make won’t run clang again. Instead, you’ll see output like this:

$ make
make: Nothing to be done for `all'.

Using make may be easier than remembering and typing a clang command each time you compile your code, but you are welcome to use either. We will discuss make in much greater detail later in the semester, but for now it is just a tool to make compiling easier.

You’ll still run the yahtzee program as normal:

$ ./yahtzee

Requirements

Your assignment will be assessed on binary scale: it either meets the requirements for the assignment and receives credit, or it does not. If the work you submit does not receive homework credit you can use a token to resubmit it. You can also use a token to submit the assignment up to 48 hours late. Keep in mind, assignments that are not submitted are not eligible for resubmission.

To receive credit, your yahtzee implementation must meet the following requirements:

Game Rules:
You must correctly implement all of the game rules described above. Don’t forget to seed the random number generator at the start of your program.
Code Style:
Your implementation must follow all of the code style requirements for the class as of the time the assignment was released. These requirements include consistent indentation, always using curly braces around loop or conditional bodies, writing comments, using clear variable names, and more. Refer to course slides for a more complete discussion of code style reuquirements.
Compilation:
Your program must compile without errors or warnings. Compiler warnings are almost always a sign that your intentions do not match precisely what the code describes. If you need help resolving compiler warnings or errors you are welcome to discuss these with course staff and evening tutors.
Input Validation:
Your program must gracefully handle invalid user input, repeatedly prompting the user for responses until they give a valid response. The input validation should match what we used for the rock, paper, scissors game.
Functions:
Your code must be organized into functions with a clear, sensible purpose. Your implementation should not have any unreasonably large or complex functions. If you are unsure about whether or not a function is too large or complex you can ask the instructor for feedback on your design.
Safety:
Your program must not rely on unsafe or undefined behavior. This means you should never read from uninitialized variables. If you use pointers or arrays in your implementation, these must be used safely; your program should never access beyond the bounds of an array, and should not access storage locations after they go out of scope (e.g. do not return pointers to a function’s local variables). Unsafe code may cause your program to crash, but programs that run correctly can still include unsafe or undefined behavior.

Class Concepts

You can complete this entire assignment using concepts we’ve covered in class as of the assignment’s release, but you may find it helpful to incorporate material we cover in class after the assignment is released. You are welcome to use concepts covered in class during the assignment period, but you may not use any C features we have not discussed before the assignment deadline.

Academic Honesty

As with all work you do in this course, the academic honesty policy applies to your work on this homework assignment. Please review the syllabus for policies on individual homework to make sure you understand the resources you can and cannot use for this work. Academic honesty violations will be reported to the committee on academic standing, and can result in penalties for your grade on this assignment or the entire course if you are found responsible for the violation.