Lab 10: Yahtzee, part II (Updated)

Objectives:

  • Complete the Yahtzee game

Important Update!

Due to the shortage of time, I’ve decided to simplify the lab somewhat by supplying everyone with a template for the final Yahtzee program that includes (among other things) a completed version of the play_game method. You can obtain the template by running the git clone ... command we used in earlier labs and looking in the lab10 directory (check out lab 3 for details). You can also get it directly at http://github.com/michaelee/cs105/tree/master/lab10/yahtzee.rb.

Students who desire more of a challenge can avoid using the provided template and work from scratch, while those who feel strapped for time should download it and work off it pronto. Either way, exercise 3 of this lab will not be worth any points.

Note that one of the things the template file does for you is allow you to use the sum and sum_of methods on array objects. [1, 1, 2, 3].sum returns 7, and [1, 1, 2, 3].sum_of(1) returns 2. Nifty!

Overview

To complete your implementation of the Yahtzee game, you’ll be writing a few additional methods. These methods will divide the game into separate logical aspects: rolling and holding the dice for a single turn, coming up with score categories for a given set of dice, and playing the game (i.e., across multiple turns).

Rolling and Holding Dice

Your first job will be to implement the component of the program that simulates rolling 5 dice 3 times, and allows the user to hold or release any given die, or end the turn prematurely. This constitutes a single turn in Yahtzee. Note that we are not, at this point, interested in scoring or keeping track of score.

Exercise 1

Implement the method play_turn, which simulates (at most) 3 rolls of the dice, and allows the user to hold or release specified dice after the first and second roll by typing in the numbered die positions (between 1 and 5). The command ‘R’ should commence the next roll, and the command ‘F’ should allow the user to finish the turn prematurely.

play_turn does not take any parameters, but returns the final values of the 5 dice in an array.

Scoring

Next, you’ll be writing code to come up with score categories for a given roll. At this point we’re not keeping track of scores across multiple turns, so we’ll assume that all score categories are still open, and simply compute and return scores for all categories for the specified roll.

Exercise 2

Implement the method get_scores_by_category, which takes an array that represents 5 dice values as a parameter, and returns a Hash of category_name→score pairs.

Playing the Game

Now we’re ready to put it all together. The last bit of needed code will allow the user to play through 13 turns and choose a score category for each turn. It will also keep a running tally of the total score.

Exercise 3

Implement the method play_game, which takes no parameters, and returns the total score of the game of Yahtzee, played across 13 turns.

Leave a comment

You must be logged in to post a comment.