Question: Create yahtzee console program in Java Modify code below -add a re-roll method that allows user to re roll dice 2 more times after the

Create yahtzee console program in Java

Modify code below

-add a re-roll method that allows user to re roll dice 2 more times after the first roll.

-user should be able to hold which value they want to hold

-Clear Array -At the end of each round you will want to set the values in the array back to 0. Roll

-Generate X random numbers (0 to 5 numbers) with a value range of 1 to 6 and place them in the last slots of the array. X will be determined by how many numbers were held in the preceding roll. If everything was held, no numbers will be generated or if no values were held, then you generate an entirely new set of numbers).

-Note we are using the last slots of the array because our hold will place numbers up front first, so we start inserting after the slots we know are held. This function needs to be aware of what set of rolls it is on, if its the second roll, it needs to place values in the second row. Hold

-This will create a loop asking the user for which values to hold. It will only accept numbers 1 to 5

-

Clear Array

-At the end of each round you will want to set the values in the array back to 0. Roll

-Generate X random numbers (0 to 5 numbers) with a value range of 1 to 6 and place them in the last slots of the array. X will be determined by how many numbers were held in the preceding roll. If everything was held, no numbers will be generated or if no values were held, then you generate an entirely new set of numbers).

using the last slots of the array because our hold will place numbers up front first, so we start inserting after the slots we know are held. This function needs to be aware of what set of rolls it is on, if its the second roll, it needs to place values in the second row. Hold

-This will create a loop asking the user for which values to hold. It will only accept numbers 1 to 5 or the letter q. Once q is selected it will copy the selected values to the next row in the array it will place them sequentially in the slots starting at 0 Then it will return a value for the number of values held.

Play Round -This function will be called at the start of each round. It will start by clearnig the array of current values. Then it will let the user roll 3 times and hold after the first 2 rolls. What is left after the final roll is what the user is scored on. At the end of the round it will increment the round counter by 1. If 10 rounds have been completed, it will return falseotherwise it will return tru

Score Hand -we will allow the program to choose the best score and regardless of whether the user has scored it before, allow that score.

3 of a kind Sum of all the dice + 5 4 of a kind Sum of all the dice + 10 full house (2 matching dice and 3 matching dice) 25 pts small straight (4 dice in consecutive sequence) 30 pts large straight (all dice in consecutive sequence) 40 pts Yahtzee (all dice match) 50 pts Chance (anything) Sum of all the dice

-When game is over allow user to start again or quit program

code to add to below:

import java.util.*;

import java.util.Scanner;

import java.util.Random;

public class Yahtzee {

private static final Random randomNumber = new Random();

private static final Scanner input = new Scanner( System.in );

public static int s1, s2, s3, s4, s5; //declaring the variables that will be used in the

//selecting which dice to re-roll

public static int rollPerTurn; //declaring variable to keep track of rolls per turn

private static int [] dice = new int [5]; //declaring an int array representing the 5 dice

public static void main( String args[] )

{

while (true){

//creating a menu

System.out.println("Welcome to the Yahtzee Game! ");

System.out.println("select 1 to start your roll.");

System.out.println("select 2 to quit the game ");

int selection = input.nextInt();

if ( selection == 1){

firstRoll();

break; //if 1 is selected start game and end the menu

}

else if ( selection == 2 ){

}

else{

System.out.println("Please enter one of the options below. ");

}

}

while ( rollPerTurn < 3 ){

System.out.println();

s1 = input.nextInt();//obtaining input to determine which dice to re-roll

s2 = input.nextInt();

s3 = input.nextInt();

s4 = input.nextInt();

s5 = input.nextInt();

reRoll();

}

}

public static int firstRoll() //method to roll 5 dice

{

for ( int i = 0; i < dice.length; i++){

dice[i] = rollDice();

}

System.out.println("Select which dice to reroll.");

for ( int a = 0; a < dice.length; a++ ){

System.out.print(dice[a] + " " );

}

rollPerTurn = 1; //indicating this is the first roll

return rollPerTurn;

}

public static int [] reRoll() //method to re-roll certain dice

{

if ( s1 == 1 ){

dice[0] = rollDice(); //re-roll Die 1

}if ( s2 == 1 ){

dice[1] = rollDice(); //re-roll Die 2

}if ( s3 == 1 ){

dice[2] = rollDice(); //re-roll Die 3

}if ( s4 == 1 ){

dice[3] = rollDice(); //re-roll Die 4

}if ( s5 == 1 ){

dice[4] = rollDice(); //re-roll Die 5

}

rollPerTurn++;

for ( int b = 0; b < dice.length; b++ ){

System.out.print(dice[b] + " " ); //displaying the dice after re-roll

}

return dice;

}

public static int rollDice() //method to roll a die

{

int die = 1 + randomNumber.nextInt( 6 );

return die;

}

public static void yahtzeeHelp() //the "help" screen

{

System.out.println("Your dice will show up and you will input a 1 or a 0");

System.out.println("under each die. A 1 indicates you wish to re-roll");

System.out.println("that particular die, and a 0 means you wish to keep it.");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!