Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//import scanner class import java.util.Scanner; //import random class import ja

ID: 3736540 • Letter: #

Question

//import scanner class
import java.util.Scanner;
//import random class
import java.util.Random;
//import Array class
import java.util.Arrays;


//public class Yahtzee
{
//main method
public static void main(String[] args){
//initialize int variable for the dice
int dice = 5;
//set it equal to 5 (there are 5 dice)
  
//create an instance of the Random class (random numbers are from 1-6)
Random randomNum = new Random();
//create an instance of the Scanner class
Scanner sc = new Scanner(System.in);
//create an array holding 5 values
int [] new_Array=new int[dice];
  
//--First Roll--
//for loop
for (int i=0; i<new_Array.length; i++)
//populate each index in array with random number
{
new_Array[i] = randomNum.nextInt(6)+1;
}
//close loop
  
  
//--Displaying the first Roll--
//call the toString method of the Arrays class
System.out.print(KGArrays.toString(new_array)+ " ");
  
  
//create a reroll variable, set it equal to zero
//ask the user if they would like to re-roll
// if they say yes and re-roll variable <3
//add one to the reroll variable
//ask which die to re roll
//create a variable and set it equal to what the user said
int reroll = 0;
System.out.print("Will you like to roll again");
String input =sc.nextLine();
  

  
//re-populate the array with a new random number at the postion the user said
  
  
  
  
  
  
  
  
  
  

}


}

Exercise 2 of 2: Create a program that lets the player play a simplified game of Yahtzee. The game starts with the user throwing five 6-sided dice. The user can choose which dice to keep and which dice to re-roll. The user can re-roll a maximum of 2 times. After two times, the user has 5 dice with a certain value. The player then has to choose where they want their final combination of dice values applied. The following list is a simplified Yahtzee card, which we will use for this game: Ones, Twos, Threes, Fours, Fives, Sixes, 3 of a kind, 4 of a kind, Full House, Straight, Yahtzee For a Full House, the player needs 3 of a kind AND 2 of a kind. For a Straight, the player needs to have all 5 dice in consecutive order, i.e. either 1 ,2, 3, 4, 5, or 2, 3, 4, 5, 6. Yahtzee means 5 of a kind. If the player applies his dice score to something inapplicable, e.g. the player only has 2 of a kind, but applies it to 3 of a kind, or the player has no Ones, but applies his dice result to the Ones category, he scores zero in this category. Implementing the Bonus, Small Straight, and Chance (as usually available in most Yahtzee games) is not required for this homework exercise. Requirement: You wil need a random number generator for the dice roll. Make use of objects, which means either create a Dice class for the 5 dice, or implement a class that represents the 5 dice together (the choice is up to you.) Also, as mentioned, the player can re-throw his dice up to two times. The player can chose which dice to re-throw and which to keep.

Explanation / Answer

import java.util.Scanner; import java.util.Random; import java.util.Arrays; { //main method public class Game { private Player p1; private Player p2; private Dice dice; private int scoreToWin; void displayGameMenu() { System.out.println(); System.out.println("(1) Start a new game"); System.out.println("(2) Play one round"); System.out.println("(3) Who is leading now?"); System.out.println("(4) Display game help"); System.out.println("(5) Exit game"); System.out.print("Choose an option: "); } void selectGameOption(int optionSelected) { switch (optionSelected) { case 1: this.startNewGame(); break; case 2: this.playOneRound(p1); this.playOneRound(p2); break; case 3: this.whoIsLeading(); break; case 4: this.displayGameInstruction(); break; default: break; } } void startNewGame() { String p1Name; String p2Name; Scanner sc = new Scanner(System.in); System.out.print("Please enter player one name: "); p1Name = sc.nextLine(); System.out.print("Please enter player two name: "); p2Name = sc.nextLine(); System.out.print("Please enter the maximum score required to win: "); scoreToWin = sc.nextInt(); p1 = new Player(p1Name); p2 = new Player(p2Name); dice = new Dice(); } void playOneRound(Player p) { int result; int firstDiceRoll = dice.rollDice(); int secondDiceRoll = dice.rollDice(); if (firstDiceRoll == secondDiceRoll) { result = (firstDiceRoll + secondDiceRoll) * 2; p.setTotalScore(result); System.out.printf("%s rolled %d and %d, " + "and scored %d points(BONUS DOUBLE POINTS), " + "for a total of %d points", p.getName(), firstDiceRoll, secondDiceRoll, result, p.getTotalScore() ); } else { result = (firstDiceRoll + secondDiceRoll); p.setTotalScore(result); System.out.printf("%s rolled %d and %d, " + "and scored %d points, " + "for a total of %d points", p.getName(), firstDiceRoll, secondDiceRoll, result, p.getTotalScore() ); } System.out.println(); } void whoIsLeading() { if (p1.getTotalScore() == p2.getTotalScore()) { System.out.format("Its currently a draw, " + "%s has %d, %s has %d", p1.getName(), p1.getTotalScore(), p2.getName(), p2.getTotalScore() ); } else if (p1.getTotalScore() > p2.getTotalScore()) { System.out.printf("%s is leading, %s has %d points, " + "%s has %d points", p1.getName(), p1.getName(), p1.getTotalScore(), p2.getName(), p2.getTotalScore()); } else if (p1.getTotalScore() = scoreToWin && p2.getTotalScore() >= scoreToWin) { System.out.println("Its a draw! Both players have exceeded the score limit"); return true; } else if (p1.getTotalScore() >= scoreToWin && p2.getTotalScore() 5 || optionSelected < 0) { System.out.print("Option entered invalid, please enter a number from 1 to 5: "); optionSelected = sc.nextInt(); } if (optionSelected == 5) { System.out.println("Exiting game"); break; } game.selectGameOption(optionSelected); boolean anyoneWin = game.checkIfAnyoneHasWon(); if (anyoneWin) { System.out.println(); System.out.println("Game ended."); break; } } } }