Lab 10: Yahtzee, part I

Objectives

  • Understand how to write methods that accept parameters and return values

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

Implement the 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 returns either true or false depending on whether the array represents the named combination.

The method call three_of_a_kind?([1, 2, 3, 1, 1]), for instance, should return true. Save all six methods in a file named “yahtzee.rb”. For each method, you should also include at least one sample call that returns true and one that returns false.

Because these are effectively mini-exercises, each solution will be worth 4 points.

Check back for subsequent additions to this lab!

Leave a comment

You must be logged in to post a comment.