Lab 11: Yahtzee
Objectives
In this lab you'll practice using and examining arrays once more, and will also try your hand at writing methods that return values.
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 lab11
subdirectory of your repository.
Yahtzee
Yahtzee is a popular game involving multiple rolls of five dice to come up with combinations that are assigned scores. These scores are later combined to come up with a final score for the player. Multiple players can compete against each other for the highest score. The "catch" of the game is that, once a combination has been used for a score, that same combination cannot be used again.
Combinations in the game include:
- Three of a kind (e.g., three 1s, three, 2s, etc.)
- Four of a kind (e.g., four 1s, four 2s, etc.)
- Full house: three of a kind and a pair (e.g., three 1s and two 2s)
- Small straight: four sequential numbers (1-2-3-4, 2-3-4-5, or 3-4-5-6)
- Large straight: five sequential numbers (1-2-3-4-5 or 2-3-4-5-6)
- "Yahtzee": five of a kind (e.g., five 1s)
You can read more about Yahtzee here. We'll be worrying about how to score these combinations in subsequent exercises, but for now let's work on being able to determine whether a given dice roll (represented as an array of integers) qualifies for each of the combinations described above.
Exercises 1-6
Open the file "yahtzee.rb", and you'll find the (empty) methods three_of_a_kind?
, four_of_a_kind?
, full_house?
, small_straight?
, large_straight?
, and yahtzee?
, each of which accepts an array of 5 integers and is meant to return either true
or false
depending on whether the array represents the named combination. Your job is to provide working implementations for all of these methods.
The method call three_of_a_kind?([1, 2, 3, 1, 1])
, for instance, should return true
.
As you implement each of the six methods, you'll find that running the file (via ruby yahtzee.rb
) will test and give you feedback as to whether your implementation(s) pass the pre-written tests. You shouldn't need to modify the given test code, but you may find it an interesting read.
Have fun!