Lab 12: Yahtzee (continued)
Objectives
Over this week and the next, we'll put the finishing touches on your implementation of Yahtzee®. While you can complete exercise 1 this week, we'll be discussing one final element that you'll need to complete exercises 2 and 3 (you can jump ahead to section 12.3: "The Hash Class" yourself, however, if you'd like to get a head start).
Update your repository
As always, start by running the following command in your repository to pull the most recent changes:
git pull
Logistics
All files referenced in this lab will be found in the labXX
subdirectory of your repository.
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.
The following is a sample run of a program that invokes play_turn
once. Note that you may choose to format your output however you wish, though the current dice values should always be visible:
-------------------------- Roll # 1: [4, 4, 1, 5, 2] ---------------------------
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 1
Now holding dice numbers: 1
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 2
Now holding dice numbers: 1, 2
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> r
-------------------------- Roll # 2: [4, 4, 1, 1, 5] ---------------------------
Now holding dice numbers: 1, 2
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 3
Now holding dice numbers: 1, 2, 3
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 4
Now holding dice numbers: 1, 2, 3, 4
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> r
------------------------- Final roll: [4, 4, 1, 1, 5] --------------------------
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. To do this, you'll need to make use of the 6 methods you write in lab 11 to determine if a given roll corresponds to a "special" category.
We'll be using the traditional Yahtzee scoring rules, shown below:
Category | Score |
---|---|
Ones | Count and add ones |
Twos | Count and add twos |
Threes | Count and add threes |
Fours | Count and add fours |
Fives | Count and add fives |
Sixes | Count and add sixes |
3 of a kind | Total of all dice |
4 of a kind | Total of all dice |
Full House | 25 |
Small Straight | 30 |
Large Straight | 40 |
Yahtzee | 50 |
Chance | Total of all dice |
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.
The following is a (abbreviated) run through a full game of Yahtzee.
************************* TURN # 1, CURRENT SCORE = 0 **************************
-------------------------- Roll # 1: [3, 4, 5, 5, 1] ---------------------------
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 3
Now holding dice numbers: 3
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 4
Now holding dice numbers: 3, 4
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> r
-------------------------- Roll # 2: [2, 3, 5, 5, 3] ---------------------------
Now holding dice numbers: 3, 4
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 2
Now holding dice numbers: 2, 3, 4
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 5
Now holding dice numbers: 2, 3, 4, 5
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> r
------------------------- Final roll: [5, 3, 5, 5, 3] --------------------------
How would you like to score your roll?
(Only remaining score categories are shown)
1. Aces: (0 points)
2. Twos: (0 points)
3. Threes: (6 points)
4. Fours: (0 points)
5. Fives: (15 points)
6. Sixes: (0 points)
T. Three of a kind: (21 points)
F. Four of a kind: (0 points)
H. Full House: (25 points)
S. Small Straight: (0 points)
L. Large Straight: (0 points)
Y. Yahtzee: (0 points)
C. Chance: (21 points)
Enter the letter for your score category:
> h
************************* TURN # 2, CURRENT SCORE = 25 *************************
-------------------------- Roll # 1: [5, 1, 2, 4, 1] ---------------------------
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 2
Now holding dice numbers: 2
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 3
Now holding dice numbers: 2, 3
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 4
Now holding dice numbers: 2, 3, 4
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> r
-------------------------- Roll # 2: [3, 1, 2, 4, 3] ---------------------------
Now holding dice numbers: 2, 3, 4
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 5
Now holding dice numbers: 2, 3, 4, 5
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> r
------------------------- Final roll: [3, 1, 2, 4, 3] --------------------------
How would you like to score your roll?
(Only remaining score categories are shown)
1. Aces: (1 points)
2. Twos: (2 points)
3. Threes: (6 points)
4. Fours: (4 points)
5. Fives: (0 points)
6. Sixes: (0 points)
T. Three of a kind: (0 points)
F. Four of a kind: (0 points)
S. Small Straight: (30 points)
L. Large Straight: (0 points)
Y. Yahtzee: (0 points)
C. Chance: (13 points)
Enter the letter for your score category:
> s
************************* TURN # 3, CURRENT SCORE = 55 *************************
-------------------------- Roll # 1: [3, 5, 5, 5, 5] ---------------------------
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> f
------------------------- Final roll: [3, 5, 5, 5, 5] --------------------------
How would you like to score your roll?
(Only remaining score categories are shown)
1. Aces: (0 points)
2. Twos: (0 points)
3. Threes: (3 points)
4. Fours: (0 points)
5. Fives: (20 points)
6. Sixes: (0 points)
T. Three of a kind: (23 points)
F. Four of a kind: (23 points)
L. Large Straight: (0 points)
Y. Yahtzee: (0 points)
C. Chance: (23 points)
Enter the letter for your score category:
> f
************************* TURN # 4, CURRENT SCORE = 78 *************************
.
.
.
.
.
************************ TURN # 13, CURRENT SCORE = 165 ************************
-------------------------- Roll # 1: [3, 5, 1, 1, 4] ---------------------------
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 3
Now holding dice numbers: 3
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> 4
Now holding dice numbers: 3, 4
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> r
-------------------------- Roll # 2: [3, 4, 1, 1, 6] ---------------------------
Now holding dice numbers: 3, 4
Enter a number (1-5) to hold or release the die at that position.
You can also enter 'R' to roll again, or 'F' to finish this turn.
> r
------------------------- Final roll: [2, 5, 1, 1, 3] --------------------------
How would you like to score your roll?
(Only remaining score categories are shown)
Y. Yahtzee: (0 points)
Enter the letter for your score category:
> y
********************************************************************************
You scored 165 points. Congratulations!
********************************************************************************
You made a new High Score!
Play again? (Y/N)
> n