Yahtzee Programming Project
Introduction
Yahtzee (see
http://en.wikipedia.org/wiki/Yahtzee
for a rule overview) is a dice game in which players compete to achieve the highest possible total score. Each turn, a player rolls five dice: he/she may elect to re-roll some (or all) of the dice, or to keep the roll. A player may re-roll at most twice in any turn. Having rolled the dice, the player then chooses how best to score the roll: there are several categories (e.g., Fives, Three of a Kind, Small Straight), each with a different set of requirements for scoring. Over the course of the game, the player may use each category only once.
For this assignment, you will implement a couple classes where each class will contain a number of methods. These classes are needed to simulate the essential aspects of a single turn of Yahtzee. When the code is completed, a user should be able to roll the dice (up to three times) and then see the points that each roll could provide in each of the scoring categories.
Writing/Compiling/Running a Java Program
Here is a refresher on the steps that you will use to write, compile, and run a Java program.
Open a text editor. If you are using Windows, I suggest using Notepad++; if you are using a Mac, I suggest using BBedit. But you can use whatever text editor that you want. Open up the editor, and use it to write all of your code.
Save your code to a directory that is easy to remember and find. I suggest creating a special CS007 folder that is located inside of your Documents folder. Note that for this project you will have multiple different files; make sure to save them all to the same folder.
Open up your command line application. For Windows, this will be the Command Prompt, and for Mac this will be the Terminal.
Navigate to the folder where your file is located. You can do this using the following commands:
ls (Mac) and dir (Windows): lists all of the files and folders in the current folder that you are in. You can use this to check if you are in the right place, and/or see what sub-directories are available for you to navigate to. All you do is type either ls or dir, and hit enter, and it will show you all of the contents of your current folder.
cd (Mac and Windows): change from your current directory to a different directory. For example, suppose you are in the Documents folder, and inside of Documents there is a folder called CS007. If you use ls or dir, it should list CS007 as one of the available folders. You can then type cd CS007 and hit enter, you will move into the CS007 folder.
Once you think you are in the right folder, use ls or dir to confirm that your folder contains all of the java files for the Yahtzee project
Once you are in the folder with with all of your java files, type the command javac *.java and then press enter. This is telling the computer to compile every single file in the directory with a .java extension.
For all parts of the project, YahtzeeGame.java will contain your main method that you use to actually play the game. So to run your program, type the command java YahtzeeGame and then press enter.
If you would like, here are video tutorials on how to write, compile, and run a java program on
Mac
and
Windows. Note that steps 5 and 6 above are slightly different from the video, but the videos will help you a lot with steps 1-4.
Programming Style
For Part 1 we will not grade you on programming style, but we will point out any places where your style needs to be fixed. For Parts 2 and 3 we will grade you on programming style. Here is what we are looking for:
Spaces around operators
If you use an operator, such as =, int x=1+2*7;, you should write int x = 1 + 2 * 7;
For increment/decrement operators, you do not need to put space to the left of the operator. You may write int x = y++;
Indentation
Every time you start a new set of curly braces, all of the code inside of the curly braces should be indented one level extra level. This applies whenever you create a new:
So for example, here is some poorly indented code
class Indentation { public static void main(String[] args) { for(int i = 0; i 10; i++) { if(i % 2 == 0) { System.out.println(i); } } } }
|
Here is how you would indent this code correctly
class Indentation {
public static void main(String[] args)
{
for(int i = 0; i 10; i++)
{
if(i % 2 == 0)
{
System.out.println(i);
}
}
} }
|
|
Variable Names
Make your variable names as descriptive as possible! Here is some code with generic variable names.
int x = 500; int y = 60; int z = x / y; System.out.println(z);
|
Now here is that same code with more descriptive variable names.
int minutes = 500; int minutesPerHour = 60; int hours = minutes / minutesPerHour; System.out.println(hours);
|
Much easier to read!
You may use i, j, and k when you are writing a loop. Otherwise, use descriptive variable names.
Part I: The Class Die (20 Points)
Due Sunday, 11/01/2020 at 11:59pm
Yahtzee consists of five dice. So, the first class that we are going to implement is the class Die where an instance (object) can be used to represent a die. In general, we roll a die and look at its value. Thus, this class should contain two methods, roll() and getValue(). The roll() method should set the value of the die to random number between 1 and 6 (inclusive). For the getValue() method, it simply returns the current value of the die. The filename of this class should be Die.java and its structure should contain the following constructors and methods:
public class Die {
public Die()
{
}
public void roll()
{
}
public int getValue()
{
} }
|
You must come up with your own set of instance variables necessary to implement this class such that its instance will behave just like an actual die.
Here are some hints for how to generate random dice rolls:
You will need to use Java’s
Random
class. In order to use this class, add the following line at the top of your class: import java.util.Random;
You create a Random object using the new keyword, just like you do with any other object: Random generator = new Random();
You may want to make an instance field of type Random. You can initialize this instance field in the constructor, and then use it to generate random numbers in your roll method.
The nextInt method of the Random class can be used to generate random integers. You call it as follows: generator.nextInt(n); where n can be any integer value or variable.
Testing the Die Class
Once your class Die is implemented, you should test your class to ensure that it works properly. For this project, our main method will be in the class YahtzeeGame (in YahtzeeGame.java). So, let's create this class with the following code to test our Die class:
public class YahtzeeGame {
public static void main(String[] args)
{
Die d = new Die();
for(int i = 0; i 10; i++)
{
d.roll();
System.out.println("roll(): " + d.getValue());
}
System.out.println("getValue(): " + d.getValue());
} }
|
When you are ready to test your code, you can do so as follows:
Open up either Terminal (Mac) or Command Prompt (Windows)
Navigate to the directory where you stored all of your files, using cd
Compile all your code: type javac *.java and then hit enter. If you have any compiler errors, fix those before you move to the next step.
Run your code: java YahtzeeGame
The output of the above program should look like the following:
roll(): 5 roll(): 5 roll(): 1 roll(): 6 roll(): 3 roll(): 6 roll(): 2 roll(): 5 roll(): 4 roll(): 4 getValue(): 4
|
Note: the result of each roll should be random and the result of the getValue() method should be the same as the last roll.
Be careful; once a die is constructed, it should be initialized with a current value which can be random. Otherwise, you may get an unexpected value when a user calls d.getValue() without calling d.roll() first.
Remember, you need to re-compile your files every time you make a change. Save your file(s), and then use the following command to (re-)compile all of your java files.
Submitting your Code for Part 1
Please submit the following files on Canvas:
Die.java - you can copy/paste the starter code, modify it, and then submit your modified version of the file.
YahtzeeGame.java - you can just copy and paste the code that we gave you without any modifications. But you still need to submit this file to make it easy for the grader.
Part 1 Rubric
Code compiles and runs without errors: 5 points
Coding style: 0 points (but we will still comment on this. Coding style will be graded more harshly on parts 2 and 3)
Instance fields: 4 points
Die constructor: 4 points
Die roll method: 4 points
Die getValue method: 4 points
Part II: The Class Yahtzee (50 Points)
Due Sunday, 11/15/2020 at 11:59pm
For this part, we will create the class that allows you to roll and score the dice used in a game of Yahtzee. This will be the longest part of the project, so I suggest that you don’t leave this one until the last minute.
We will grade Part 2 for programming style. You should make sure you review the section on style guidelines before you start this section.
Setting up the Class Yahtzee
We are going to look at an instance of the class Yahtzee as an object that consists of five dice. A user can roll some or all dice. A user can also look at current values of all five dice, check the score of each category as well as get the current score card. First, let's focus on five dice and its related methods.
For simplicity, we can use a one-dimensional array named dice of type Die to represent all five dice. A user can roll some or all dice as he/she wishes. S/he can also check the value of each individual die. Thus, the structure of the class Yahtzee (for now) should look like the following:
public class Yahtzee {
private Die[] dice;
public Yahtzee()
{
dice = new Die[5];
for(int i = 0; i 5; i++)
{
dice[i] = new Die();
}
rollAllDice();
}
public void rollADie(int dieNumber)
{
}
public void rollAllDice()
{
}
public int getADie(int dieNumber)
{
}
public String showDice()
{
} }
|
From the above code, there are four methods related to dice as follows:
rollADie(int dieNumber):This method allows the user to roll a specific die. The dieNumber should be between 1 and 5 (inclusive). Do not forget that we use the variable dice of type array. In other words, die number 1 is associated with the variable dice[0], die number 2 is associated with dice[1], and so on. This method should return nothing.
rollAllDice(): When called, this method simply rolls all die and return nothing. This method should make use of the rollADie method
getADie(int dieNumber): This method simply returns the current value of a given die number.
showDice() returns a string representation of all dice and their value in the format shown below:
+------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 2 | 5 | 2 | 3 | 6 | +------+---+---+---+---+---+
|
Note: this method does not print anything on the console screen. It simply returns a String in the above format. In other words, you should see something like the above example if a user uses the following statement:
System.out.println(yahtzee.showDice());
|
where yahtzee is a reference variable of type Yahtzee.
Testing the Yahtzee Class
As usual, you should test your class a couple times to ensure that those methods are implemented correctly. The following code (YahtzeeGame.java) can be used to test them:
public class YahtzeeGame {
public static void main(String[] args)
{
Yahtzee yahtzee = new Yahtzee();
yahtzee.rollAllDice();
System.out.println(yahtzee.showDice());
System.out.println("Get value of die number 3 is " + yahtzee.getADie(3));
yahtzee.rollADie(3);
System.out.println(yahtzee.showDice());
System.out.println("After re-roll the die number 3: " + yahtzee.getADie(3));
} }
|
When you are ready to test your code, you can do so as follows:
Open up either Terminal (Mac) or Command Prompt (Windows)
Navigate to the directory where you stored all of your files, using cd
Compile all your code: type javac *.java and then hit enter. If you have any compiler errors, fix those before you move to the next step.
Run your code: java YahtzeeGame
The following is an example of the output of the above program:
+------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 1 | 2 | 6 | 5 | 5 | +------+---+---+---+---+---+ Get value of die number 3 is 6 +------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 1 | 2 | 2 | 5 | 5 | +------+---+---+---+---+---+ After re-roll the die number 3: 2
|
Note that your output won’t be exactly the same because the dice rolls are random, but once your output looks something like this (and the output is reporting the correct value for die 3), you can move on to the next section.
Scoring Utility Methods
The scoring of Yahtzee is quite complicated. To simplify this process, we are going to use another one-dimensional array of type int of size 6 elements named counts.
private int[] counts;
counts[0] will store the number of the n dice rolls that have the value 1, counts[1] will store the number of the n dice rolls that have the value 2, and so on. This counts variable should be updated after a die is rolled. The following methods will help us easily update the variable counts:
private int countUp(int value): This method returns the number of n dice rolls that have the value value. For example, if the dice rolls were 5, 1, 3, 6, 5, then countUp(5) should return 2 since there are two dice with value 5.
private void updateCounts(): This method simply updates counts[0] to counts[5]. The method should be called after each roll - so you should go back and update your rollADie(int dieNumber) method so that it calls updateCounts. With the countUp() method implemented, this methods can be as simple as
private void updateCounts() {
for(int i = 0; i 6; i++)
counts[i] = countUp(i + 1); }
|
At this point, the structure of your class Yahtzee should look like the following:
public class Yahtzee {
private Die[] dice;
private int[] counts;
public Yahtzee()
{
dice = new Die[5];
for(int i = 0; i 5; i++)
dice[i] = new Die();
counts = new int[6];
rollAllDice();
}
public void rollAllDice() {...}
public void rollADice(int dieNumber) {...}
public int getADie(int dieNumber) {...}
public String showDice() {...}
private int countUp(int value)
{
}
private void updateCounts()
{
for(int i = 0; i 6; i++)
{
counts[i] = countUp(i + 1);
System.out.println("Number of " + (i + 1) + "s rolled: " + counts[i]);
}
} }
|
Note: in the last section you made it so that the rollADie method updates the counts variable. Furthermore, your rollAllDice() method should make use of the rollADie method. When the constructor calls the rollAllDice method, this should automatically update the variable counts.
Note: it is important that you initialize counts before you call rollAllDice, otherwise you will get a NullPointerException.
Note: the statement System.out.println() in the updateCounts() method is for our debugging purpose only. It should be removed once you already verify that your methods countUp() and updateCounts() work correctly.
Testing the Utility Methods
to verify that your countUp() and updateCounts() methods are implemented correctly modify the main() method of the class YahtzeeGame as follows:
public class YahtzeeGame {
public static void main(String[] args)
{
Yahtzee yahtzee = new Yahtzee();
System.out.println(yahtzee.showDice());
} }
|
When you are ready to test your code, you can do so as follows:
Open up either Terminal (Mac) or Command Prompt (Windows)
Navigate to the directory where you stored all of your files, using cd
Compile all your code: type javac *.java and then hit enter. If you have any compiler errors, fix those before you move to the next step.
Run your code: java YahtzeeGame
The following is an example of an output of the above program:
Number of 1s rolled: 0 Number of 2s rolled: 2 Number of 3s rolled: 1 Number of 4s rolled: 0 Number of 5s rolled: 1 Number of 6s rolled: 1 +------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 2 | 5 | 2 | 3 | 6 | +------+---+---+---+---+---+
|
Make sure to verify that your counts variable contains the correct value. If you think your countUp() and updateCounts() methods are working properly, you can go ahead and remove the System.out.println() statement inside the updateCounts() method.
Scoring the Upper Section
The scoring of Yahtzee consists of two sections, the upper section and the lower section. We will look into each section separately. The upper section consists of six categories, Ones, Twos, Threes, Fours, Fives, and Sixes. For this scoring section, you simply total only the specified die face. For example, if you roll 5, 3, 1, 3, 1, the score of each category will be as follows:
Ones is 2, since there are two dice with value 1 (1 + 1 = 2).
Twos is 0 since there are no dice with value 2.
Threes is 6 since there are two dice with value 3 (3 + 3 = 6).
Fours is 0.
Fives is 5 since there is one die with value 5.
Sixes is 0.
With the variable counts that has been properly updated, scoring of each category in this upper section will be as simple as counts[value - 1] * value where value is the face value of each category. For example, from the above roll, counts[0] will be 2 (there are two dice with value 1), the score of Ones is simply counts[0] * 1. For this upper section, insert the following methods into your class Yahtzee (but do not remove any of the previous methods, fields, or constructors):
public class Yahtzee {
public int getScoreOnes()
{
}
public int getScoreTwos()
{
}
public int getScoreThrees()
{
}
public int getScoreFours()
{
}
public int getScoreFives()
{
}
public int getScoreSixes()
{
} }
|
where each method is associated with each scoring category.
Testing the Upper Section
After you implement the above methods, modify the main() method of your class YahtzeeGame to the following to verify these methods:
public class YahtzeeGame {
public static void main(String[] args)
{
Yahtzee yahtzee = new Yahtzee();
System.out.println(yahtzee.showDice());
System.out.println(" Ones: " + yahtzee.getScoreOnes());
System.out.println(" Twos: " + yahtzee.getScoreTwos());
System.out.println(" Threes: " + yahtzee.getScoreThrees());
System.out.println(" Fours: " + yahtzee.getScoreFours());
System.out.println(" Fives: " + yahtzee.getScoreFives());
System.out.println(" Sixes: " + yahtzee.getScoreSixes());
} }
|
When you are ready to test your code, you can do so as follows:
Open up either Terminal (Mac) or Command Prompt (Windows)
Navigate to the directory where you stored all of your files, using cd
Compile all your code: type javac *.java and then hit enter. If you have any compiler errors, fix those before you move to the next step.
Run your code: java YahtzeeGame
The output of the above program should look like the following:
+------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 3 | 5 | 3 | 1 | +------+---+---+---+---+---+
Ones: 1
Twos: 0
Threes: 6
Fours: 0
Fives: 5
Sixes: 6
|
Again, Run the above program a couple times to verify that all scoring of this upper section is associated with the dice rolls.
Lower Section
The lower section consists of seven categories. The scoring of each category are as follows:
Three of a Kind: If a roll contains at least three of the same die faces, simply total all the die faces and that is the score of this category. All literally means all - as in, add up each die, including the ones that are not part of the three of a kind. If there are no matching triplets, this method should return 0.
Four of a Kind: The scoring of this category is the same as the Three of a Kind (total all the die faces) except that you must have at least four of the same die faces. All literally means all - as in, add up each die, including the ones that are not part of the four of a kind. If there are no matching quadruplets, this method should return 0.
Full House: If you have three of a kind and a pair (e.g., 5, 3, 5, 5, 3), the score of this category is 25. If there is no four of a kind, this method should return 0.
Small Straight: If you have a sequence of 4 consecutive die faces (e.g., 2, 4, 3, 4, 5), 30 points
Large Straight: If you have a sequence of 5 consecutive die faces, 40 points.
Chance: For this category, simply total all the die faces values.
Yahtzee: This category is simply 5 of a kind. This score of this category is 50 points.
For this lower section, insert the following methods into your class Yahtzee (but do not remove any of the previous methods, fields, or constructors):
public class Yahtzee {
public int getScoreThreeOfAKind()
{
}
public int getScoreFourOfAKind()
{
}
public int getScoreFullHouse()
{
}
public int getScoreSmallStraight()
{
}
public int getScoreLargeStraight()
{
}
public int getScoreChance()
{
}
public int getScoreYahtzee()
{
} }
|
where each method is associated with each scoring category. As we mentioned earlier, using the variable counts will help you implement scoring of this lower section. For example, for the Three of a Kind, you just have to search whether a counts[i] where i is in between 0 to 5 (inclusive) is greater than or equal to 3.
Testing the Lower Scoring
Once these method are implemented, use the following program to verify the above methods:
public class YahtzeeGame {
public static void main(String[] args)
{
Yahtzee yahtzee = new Yahtzee();
System.out.println(yahtzee.showDice());
System.out.println("Three of a Kind: " + yahtzee.getScoreThreeOfAKind());
System.out.println(" Four of a Kind: " + yahtzee.getScoreFourOfAKind());
System.out.println(" Full House: " + yahtzee.getScoreFullHouse());
System.out.println(" Small Straight: " + yahtzee.getScoreSmallStraight());
System.out.println(" Large Straight: " + yahtzee.getScoreLargeStraight());
System.out.println(" Chance: " + yahtzee.getScoreChance());
System.out.println(" Yahtzee: " + yahtzee.getScoreYahtzee());
} }
|
When you are ready to test your code, you can do so as follows:
Open up either Terminal (Mac) or Command Prompt (Windows)
Navigate to the directory where you stored all of your files, using cd
Compile all your code: type javac *.java and then hit enter. If you have any compiler errors, fix those before you move to the next step.
Run your code: java YahtzeeGame
The output of the above program should look like the following:
+------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 5 | 6 | 6 | 3 | +------+---+---+---+---+---+ Three of a Kind: 26
Four of a Kind: 0
Full House: 0
Small Straight: 0
Large Straight: 0
Chance: 26
Yahtzee: 0
|
Displaying the Scorecard
The last method of this class Yahtzee is called getScoreCard() method. Add the following method to the Yahtzee class (but do not remove any of the previous methods)
public class Yahtzee {
public String getScoreCard()
{
} }
|
This method returns a score card of the current roll in the form of a String (similar to the showDice() method) which can be used with the statement System.out.println(). The format of the output string should look like the following:
+------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 1 | 2 | 4 | 5 | +------+---+---+---+---+---+
Ones: 1
Twos: 2
Threes: 0
Fours: 4
Fives: 5
Sixes: 6
Three of a Kind: 0
Four of a Kind: 0
Full House: 0
Small Straight: 0
Large Straight: 0
Chance: 18
Yahtzee: 0
|
You should use String concatenation, along with the methods you have already written, to construct the output string.
Testing the Scorecard
You can test this method with the following code
public class YahtzeeGame {
public static void main(String[] args)
{
Yahtzee yahtzee = new Yahtzee();
System.out.println(yahtzee.getScoreCard());
} }
|
When you are ready to test your code, you can do so as follows:
Open up either Terminal (Mac) or Command Prompt (Windows)
Navigate to the directory where you stored all of your files, using cd
Compile all your code: type javac *.java and then hit enter. If you have any compiler errors, fix those before you move to the next step.
Run your code: java YahtzeeGame
Submitting your Code for Part 2
Please submit the following files on Canvas;
Die.java - You do not need to change anything from the last part, but you still need to submit this file to make it easier on the grader
Yahtzee.java - Submit your final version that has everything, up to and including the getScoreCard method
YahtzeeGame.java - Submit your final version that you used to get the getScoreCard method
Part 2 Rubric
Code compiles and runs with no errors: 10 points
Programming Style: 10 points
Scoring Utility Methods: 5 points
Upper Scoring Methods: 10 points
Lower Scoring Methods: 15 points
Part III: Game Play (30 Points)
Due Sunday, 11/22/2020 at 11:59pm
For this part, we will create the class that allows the user to actually play a game of Yahtzee.
We will grade Part 3 for programming style. You should make sure you review the section on style guidelines before you start this section.
As we mentioned earlier, we will focus on only one turn of a player. In each turn, a player rolls five dice: he/she may elect to re-roll some (or all) of the dice, or to keep the roll. A player may re-roll at most twice in any turn.
Now we will implement the game play in the class YahtzeeGame (in the file YahtzeeGame.java). From the previous section, your main() method should look like the following:
public class YahtzeeGame {
public static void main(String[] args)
{
Yahtzee yahtzee = new Yahtzee();
System.out.println(yahtzee.getScoreCard());
} }
|
When you run the program, the output on the console screen should look like the following:
+------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 1 | 2 | 4 | 5 | +------+---+---+---+---+---+
Ones: 1
Twos: 2
Threes: 0
Fours: 4
Fives: 5
Sixes: 6
Three of a Kind: 0
Four of a Kind: 0
Full House: 0
Small Straight: 0
Large Straight: 0
Chance: 18
Yahtzee: 0
|
Now, we can start adding more code to our main() method. Then the game should ask the user to enter the die number(s) that they want to keep (separated) by a space. Start by printing out the following:
Enter die number(s) to keep (separated by a space):
|
You should use a Scanner object with the nextLine() method to read in a String. For example, in the above case, if a user wants to keep die numbers 1, 3, 4, and 5, user will enter 1 3 4 5 as shown below:
Enter die number(s) to keep (separated by a space): 1 3 4 5
|
Once the user presses the Enter key, your program should re-roll all dices that are not on the given list. This can be done by calling the contains() method of the String class. For example, suppose the string "1 3 4 5" is stored in the variable named userKeep of type String, then userKeep.contains("1") will return true, and userKeep.contains("2") will return false. Re-roll the die number(s) that are not on the given list. When done, simply show the current roll with the scorecard as shown below:
Enter die number(s) to keep (separated by a space): 1 3 4 5 +------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 3 | 2 | 4 | 5 | +------+---+---+---+---+---+
Ones: 0
Twos: 2
Threes: 3
Fours: 4
Fives: 5
Sixes: 6
Three of a Kind: 0
Four of a Kind: 0
Full House: 0
Small Straight: 30
Large Straight: 40
Chance: 20
Yahtzee: 0 Enter die number(s) to keep (separated by a space):
|
Note that a user can re-roll one more time. However, if a user decide to end his/her turn, he/she must simply enter 1 2 3 4 5 to keep all dice as shown below:
Enter die number(s) to keep (separated by a space): 1 2 3 4 5 +------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 3 | 2 | 4 | 5 | +------+---+---+---+---+---+
Ones: 0
Twos: 2
Threes: 3
Fours: 4
Fives: 5
Sixes: 6
Three of a Kind: 0
Four of a Kind: 0
Full House: 0
Small Straight: 30
Large Straight: 40
Chance: 20
Yahtzee: 0
|
Similarly, if a user wants to re-roll all dice, simply press Enter without entering a number when asked.
The following is the console output of the previous example of a turn of a user:
+------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 1 | 2 | 4 | 5 | +------+---+---+---+---+---+
Ones: 1
Twos: 2
Threes: 0
Fours: 4
Fives: 5
Sixes: 6
Three of a Kind: 0
Four of a Kind: 0
Full House: 0
Small Straight: 0
Large Straight: 0
Chance: 18
Yahtzee: 0 Enter die number(s) to keep (separated by a space): 1 3 4 5 +------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 3 | 2 | 4 | 5 | +------+---+---+---+---+---+
Ones: 0
Twos: 2
Threes: 3
Fours: 4
Fives: 5
Sixes: 6
Three of a Kind: 0
Four of a Kind: 0
Full House: 0
Small Straight: 30
Large Straight: 40
Chance: 20
Yahtzee: 0 Enter die number(s) to keep (separated by a space): 1 2 3 4 5 +------+---+---+---+---+---+ | Dice | 1 | 2 | 3 | 4 | 5 | +------+---+---+---+---+---+ | Face | 6 | 3 | 2 | 4 | 5 | +------+---+---+---+---+---+
Ones: 0
Twos: 2
Threes: 3
Fours: 4
Fives: 5
Sixes: 6
Three of a Kind: 0
Four of a Kind: 0
Full House: 0
Small Straight: 30
Large Straight: 40
Chance: 20
Yahtzee: 0
|
You may notice that you may have only one method which is the main() method in YahtzeeGame.java and it is pretty short. This is one of the advantages of Object-Oriented Programming.
When you are ready to test your code, you can do so as follows:
Open up either Terminal (Mac) or Command Prompt (Windows)
Navigate to the directory where you stored all of your files, using cd
Compile all your code: type javac *.java and then hit enter. If you have any compiler errors, fix those before you move to the next step.
Run your code: java YahtzeeGame
Submitting your Code for Part 3
Please submit the following files on Canvas
Die.java - You do not need to change anything from Part 1, but you still need to submit this to make it easier for the grader.
Yahtzee.java - You do not need to change anything from Part 2, but you still needto submit this to make it easier for the grader.
YahtzeeGame.java - All the code you write for Part 3 will be in the YahtzeeGame.java class. You should get rid of the code that you used to test all of your methods from Part 2, and only submit the code that you are using to play the game of Yahtzee.
Part 3 Rubric
Yahtzee Game plays correctly: 10 points