As part of an assignment for my intro Java class (please bear with my beginner skill set), I'm working on a program for which a Die object class is used to play a Yahtzee game. An overview of the directions are as follows:
The program should give an introduction of what it is going to do, ask the user how many dice the he/she wants to roll (number must be positive), and create an array of Die objects of the specified size. The program should then repeatedly roll all the dice until a Yahtzee is rolled occurs when all the dice have the same face value and report how many rolls it took to obtain the Yahtzee, and what face value made the Yahtzee. The program should give the user the option to run it again.
Also, it should print out a single period each time the dice are rolled, so as to indicate to the user that the program is working. These periods will be printed on the same line and a newline character should be added after every 50 rolls. The use of helper methods is encouraged and most methods should be around 12 lines or less. There should be, at minimum, a method that accepts an array of Die objects as a parameter and rolls each Die object once and another method that accepts the same array as a parameter and tests to see if a Yahtzee has been rolled.
I have completed the program and it seems to work alright, but I was hoping to get some advice/suggestions on how to better optimize my code, as I feel it is a little messy. Any feedback would be much appreciated!
Below is the Die
class:
Note: The methods' modifiers, return types, and parameters were defined in the assignment directions and must be exactly as specified. There must be a single instance variable int
faceValue
, a void
method roll with no parameters, a method getValue
that simply returns the value of faceValue
, and a method toString
that returns a String
representation of faceValue
.
public class Die {
int faceValue;
public void roll(){
faceValue = (int)(Math.random() * 6 + 1);
}
public int getValue(){
return faceValue;
}
public String toString(){
return Integer.toString(faceValue);
}
}
The Die
class is to be used in the main program called Yahtzee, which I have written as follows:
import java.util.*;
public class Yahtzee {
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
introduction();
boolean runAgain = true;
while (runAgain){
boolean rolledYahtzee = false;
int numRolls = 0;
Die [] dieArray = dieArray(keyboard);
while(!rolledYahtzee){
dieArray = rollDice (dieArray);
numRolls++;
printDots(numRolls);
rolledYahtzee = checkForYahtzee(dieArray, numRolls);
}
printResults (numRolls, dieArray);
runAgain = runAgain(keyboard);
}
}
public static void introduction(){
System.out.println("This program will");
}
public static Die [] dieArray(Scanner keyboard){
int numDice = getInt(keyboard, "How many dice do you want to throw? ");
while (numDice <= 0){
System.out.println("Sorry, you must enter a positive number.");
numDice = getInt(keyboard, "How many dice do you want to throw? ");
}
Die [] dieArray = new Die [numDice];
return dieArray;
}
public static int getInt (Scanner keyboard, String prompt){
System.out.print(prompt);
while (!keyboard.hasNextInt()){
keyboard.next();
System.out.println("Sorry, you must enter an integer.");
System.out.print(prompt);
}
return keyboard.nextInt();
}
public static Die [] rollDice(Die [] dieArray){
for (int i = 0; i < dieArray.length; i++){
dieArray[i] = new Die ();
dieArray[i].roll();
dieArray[i].faceValue = dieArray[i].getValue();
}
return dieArray;
}
public static void printDots(int numRolls){
System.out.print(".");
if ((numRolls%50) == 0){
System.out.println();
}
}
public static boolean checkForYahtzee(Die[] dieArray, int numRolls) {
for(int i = 0; i < dieArray.length; i++) {
for(int j = i+1; j < dieArray.length; j++) {
if(dieArray[i].getValue() != dieArray[j].getValue()) {
return false;
}
}
}
return true;
}
public static void printResults (int numRolls, Die[] dieArray){
System.out.println();
System.out.println("Yahtzee!!");
System.out.print("After " + numRolls + " rolls, I finally rolled ");
System.out.println(dieArray.length + " " + dieArray[0].getValue() + "'s");
System.out.println();
}
public static boolean runAgain (Scanner keyboard){
System.out.print("Do you want to run another experiment? (y|n)): ");
String answer = keyboard.next().trim().toLowerCase();
keyboard.nextLine();
System.out.println();
return (answer.charAt(0) == 'y');
}
}
Here is a sample execution given by the instructor to demonstrate how the program should behave:
This program will... [your job to fill in]
How many dice do you want to throw? -2
Sorry, you must enter a positive number.
How many dice do you want to throw? 0
Sorry, you must enter a positive number.
How many dice do you want to throw? 2
........
Yahtzee!!
After 8 rolls, I finally rolled 2 1's
Do you want to run another experiment? (y|n)): y
How many dice do you want to throw? 3
..
Yahtzee!!
After 2 rolls, I finally rolled 3 2's
Do you want to run another experiment? (y|n)): y
How many dice do you want to throw? 4
..................................................
..............................
Yahtzee!!
After 80 rolls, I finally rolled 4 6's
Do you want to run another experiment? (y|n)): y
How many dice do you want to throw? 5
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
................................................
Yahtzee!!
After 448 rolls, I finally rolled 5 4's
Do you want to run another experiment? (y|n)): y
How many dice do you want to throw? 6
..................................................
..................................................
..................................................
[a bunch of dots removed to save space]
..................................................
..................................................
..........................
Yahtzee!!
After 15926 rolls, I finally rolled 6 5's
Do you want to run another experiment? (y|n)): n