Yahtzee Lab In this lab we will be simulating Yahtzee games
Yahtzee Lab
In this lab we will be simulating Yahtzee games! You will have two java classes: Dice and Yahtzee. Dice will be the domain class and Yahtzee will be the tester class. We will display a menu to our user and allow them to play a single game or multiple games then display the results of their games.
Domain Class:
Create Mutator (Setter) and Accessor (Getter) methods.
Create toString method.
Tester Class:
Step 1: main()
Correct any syntax issues in main()
Step 2: displayMenu()
Fix the loop used to display the menu. (Loop #1)
Step 3: playGame()
Create a counter for twoPairs and threePairs. Increment these when the user gets the corresponding result.
Create three Dice objects: die1, die2, and die3.
Fix the loop to figure out how many Yahtzee games the user would like to simulate. (Loop #2)
Fix the loop that will simulate the number of Yahtzee games the user specified in the variable numGames. (Loop #3)
You will need to roll the dice in each loop iteration.
You will need to increment the correct counters every time a pair is rolled.
Display the contents of die1, die2, and die3.
Create a decision structure to check dice for any pairs (You will need to check for pairs if the user is playing either 1 game or multiple games).
Two Pair
Three Pair
Display results for the users Yahtzee game or games.
import java.util.Scanner;
/**
*
* @author mwoma001
*/
public class YahtzeeTester
{
public static void main(String[] args)
{
int choice = 0;
choice = displayMenu();
playGame();
}
public static int displayMenu()
{
int choice = 0;
Scanner keyboard = new Scanner(System.in);
//Loop until the user picks a valid choice (1 - 3). LOOP #1
{
System.out.println(\"*******************************\ Welcome to the Yahtzee Simulator!\ *******************************\ \" +
\"Please choose from the following options:\ 1. Play a Single Game\ 2. Play Multiple Games\ 3. Exit\");
choice = keyboard.nextInt();
}
//End Loop
return choice;
}
public static void playGame(int aChoice)
{
Scanner keyboard = new Scanner(System.in);
int numGames = 0;
if(aChoice == 1)
{
//Roll Dice
//Create Switch or if Statement here and display results.
}
else if(aChoice == 2)
{
//Loop Until The User Picks a Positive Number for numGames. LOOP #2
{
System.out.println(\"Please pick a positive number - This will determine how many times you play Yahtzee.\");
numGames = keyboard.nextInt();
}
//End Loop
//Create a Loop that will run as many times as the user specified in numGames. LOOP #3
{
//Create Counters
//Roll the Dice
//Create a Switch or if Statement Here.
//Display the Contents of die1, die2, and die3 after each iteration of the loop.
}
//End Loop
//Display how many times the user played, and the contents of your counters that are keeping track of pairs.
}
else
{
System.out.println(\"Good-Bye!\");
}
}
}
package Yahtzee;
import java.util.Random;
/**
*
* @author mwoma001
*/
public class Dice
{
int value;
public Dice()
{
value = 0;
}
public void rollDice()
{
Random myRand = new Random();
this.value = myRand.nextInt(6) + 1;
}
}
Solution
Mistake in Yahtzee Tester: choice not passed when playgames called from main().
YahtzeeTester.java
import java.util.Scanner;
public class YahtzeeTester
{
public static void main(String[] args)
{
int choice = 0;
choice = displayMenu();
playGame(choice);
}
public static int displayMenu()
{
int choice = 0;
Scanner keyboard = new Scanner(System.in);
//Loop until the user picks a valid choice (1 - 3). LOOP #1
while (choice>3 || choice<=0){
System.out.println(\"*******************************\ Welcome to the Yahtzee Simulator!\ *******************************\ \" +
\"Please choose from the following options:\ 1. Play a Single Game\ 2. Play Multiple Games\ 3. Exit\");
choice = keyboard.nextInt();
}
//End Loop
return choice;
}
public static void playGame(int aChoice)
{
int twoPairs = 0;
int threePairs = 0;
Scanner keyboard = new Scanner(System.in);
int numGames = 0;
Dice die1 = new Dice();
Dice die2 = new Dice();
Dice die3 = new Dice();
if(aChoice == 1)
{
//Roll Dice
die1.rollDice();
die2.rollDice();
die3.rollDice();
System.out.println(\"die1: \"+die1.get()+\"\ \");
System.out.println(\"die2: \"+die2.get()+\"\ \");
System.out.println(\"die3: \"+die3.get()+\"\ \");
//Create Switch or if Statement here and display results.
if(die1.get()==die2.get() && die2.get()==die3.get()){
//threePairs++;
System.out.println(\"Three pairs\");
}
else if(die1.get()==die2.get() || die3.get()==die2.get() || die1.get()==die3.get()){
//twoPairs++;
System.out.println(\"Two Pairs\");
}
}
else if(aChoice == 2)
{
int numPlayed = 0;
//Loop Until The User Picks a Positive Number for numGames. LOOP #2
while(numGames<=0){
System.out.println(\"Please pick a positive number - This will determine how many times you play Yahtzee.\");
numGames = keyboard.nextInt();
}
//End Loop
//Create a Loop that will run as many times as the user specified in numGames. LOOP #3
while(numGames>numPlayed){
//Create Counters
numPlayed++;
//Roll the Dice
die1.rollDice();
die2.rollDice();
die3.rollDice();
//Create a Switch or if Statement Here.
if(die1.get()==die2.get() && die2.get()==die3.get()){
threePairs++;
}
else if(die1.get()==die2.get() || die3.get()==die2.get() || die1.get()==die3.get()){
twoPairs++;
}
//Display the Contents of die1, die2, and die3 after each iteration of the loop.
System.out.println(\"die1: \"+die1.get()+\"\ \");
System.out.println(\"die2: \"+die2.get()+\"\ \");
System.out.println(\"die3: \"+die3.get()+\"\ \");
}
//End Loop
//Display how many times the user played, and the contents of your counters that are keeping track of pairs.
System.out.println(\"Number of games played: \"+numGames+\"\ \");
System.out.println(\"Number of two pairs: \"+twoPairs+\"\ \");
System.out.println(\"Number of three pairs: \"+threePairs+\"\ \");
}
else
{
System.out.println(\"Good-Bye!\");
}
}
}
Dice.java
import java.util.Random;
/**
*
* @author mwoma001
*/
public class Dice
{
int value;
public Dice()
{
value = 0;
}
public void rollDice()
{
Random myRand = new Random();
this.value = myRand.nextInt(6) + 1;
}
public void set(int value){
this.value = value;
}
public int get(){
return this.value;
}
}




