1.3-Object-Georienteerd-Programmeren

Yahtzee

We are going to build an application that allows us to roll 5 dice. We are going to visualize the dice by drawing them on the canvas. In the last part of the assignment you are going to determine if Yahtzee has been thrown.

Implementation

We need three classes for this assignment: a class Yahtzee, which simulates the game, a class Turn, which represents a round (or in other words a collection of dice) and a class Dice which represents a single die.

Create a class Dice with a roll() method that returns a random number from 1-6. Next, create a class Turn in which 5 dice are rolled. Finally, the class Yahtzee creates a new Turn instance for each “round.”

Below is the sequence diagram that visualizes this.

Visualization

In the images folder you will find images of all sides of a dice. First create a draw(int x, int y, int width) method in the Dice class that can draw one die at a given location. To do this, use the images from the images directory.

Then create a draw() method in the Turn class that allows all the dice to be drawn. Finally, the Yahtzee class must also have a draw() method that draws the dice of the current round as well as the combination found (we will come back to this).

Below is the sequence diagram that visualizes this.

Determine Yahtzee

If all the dice are equal then it is Yahtzee. If Yahtzee has been rolled, print it in the screen. To do this, write a method isYathzee() in the Turn class that determines if there is indeed Yahtzee in that round. Then use this method in the draw() method of the class Yahtzee to draw this caption.

Below is the sequence diagram that visualizes this with a possible algorithm, you are of course free to solve this in a different way.

Bonus: Determining Other Combinations (3 + 1)

All possible combinations for Yahtzee can be found here.. This is a pretty tough exercise! So start simple and work this up as you go along. For example, start with four-of-a-kind and three-of-a-kind and create your own methods for these, similar to the isYahtzee() methods. Finally, implement the determineCombination() method to which returns a String representation indicating what the highest possible combination is.

Hints: