//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
}
}
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; } } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.