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

Create yahtzee console program in Java Modify code below -add a re-roll method t

ID: 3902127 • Letter: C

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 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.");

}

}

Explanation / Answer

import java.util.*;

/**

* Lets the user play Yahtzee with five dice and keep score.

*/

public class MiniYahtzee {

public static void main(String[] args) {

int play = 1, scorea = 0, sum = 0;

int[] wins = new int[15];

while ((play == 1) && (sum < 15)) {

sum = 0;

int[] aDice = new int[] { 0, 0, 0, 0, 0 };// creates an array

int roll = 0;

int x, y, w, z;

int rerolla = 0, rerollb = 03;

Die die = new Die();

for (x = 0; x < 5; x++) {

die.roll();

aDice[x] = die.get();// sets the dice values

}

System.out.println("Die 1: " + aDice[0]);

System.out.println("Die 2: " + aDice[1]);

System.out.println("Die 3: " + aDice[2]);

System.out.println("Die 4: " + aDice[3]);

System.out.println("Die 5: " + aDice[4]);

do {

rerolla = inputInt("How many dice do you want to reroll? (0-5)");

if (rerolla > 0) {

int[] reroll = new int[rerolla];

for (y = 0; y < rerolla; y++) {

rerollb = inputInt("Which ones?");

reroll[y] = rerollb;

}

for (w = 0; w < rerolla; w++) {

if (reroll[w] == 1) {

die.roll();

aDice[0] = die.get();

}

if (reroll[w] == 2) {

die.roll();

aDice[1] = die.get();

}

if (reroll[w] == 3) {

die.roll();

aDice[2] = die.get();

}

if (reroll[w] == 4) {

die.roll();

aDice[3] = die.get();

}

if (reroll[w] == 5) {

die.roll();

aDice[4] = die.get();

}

}

roll++;

System.out.println("Die 1: " + aDice[0]);

System.out.println("Die 2: " + aDice[1]);

System.out.println("Die 3: " + aDice[2]);

System.out.println("Die 4: " + aDice[3]);

System.out.println("Die 5: " + aDice[4]);

}

} while ((roll < 2) && (rerolla > 0));

Winnings prize = new Winnings();

prize.checkWinnings(aDice, wins);

wins[prize.choice() - 1] = 1;

for (z = 0; z < 15; z++) {

sum += wins[z];

}

scorea += prize.score();

System.out.println("Your total score is: " + scorea);

if (sum < 15) {

play = inputInt("do you want to play again?(1=yes, 2=no)");

} else {

System.out.println("GAME OVER!");

}

}

}

static int inputInt(String Prompt) {

int result = 0;

try {

result = Integer.parseInt(input(Prompt).trim());

} catch (Exception e) {

result = 0;

}

return result;

}

static String input(String prompt) {

String inputLine = "";

System.out.print(prompt);

try {

java.io.InputStreamReader sys = new java.io.InputStreamReader(

System.in);

java.io.BufferedReader inBuffer = new java.io.BufferedReader(sys);

inputLine = inBuffer.readLine();

} catch (Exception e) {

String err = e.toString();

System.out.println(err);

}

return inputLine;

}

}

/* The class for handling each of the dice */

class Die {

private int value;

private Random rand;

public Die() {

value = 0;

rand = new Random();

}

public void roll() {

value = 1 + rand.nextInt(6);

}

public int get() {

return (value);

}

}

/* The class for determining what you have (i.e. a pair, straight, etc) */

class Winnings {

private int score;

private int choice;

public Winnings() {

score = 0;

}

public void checkWinnings(int[] aDice, int[] wins) {

System.out.println("Which do you want to see if you have?");

if (wins[0] == 0) {

System.out.println("1 - yahtzee");

}

if (wins[1] == 0) {

System.out.println("2 - full house");

}

if (wins[2] == 0) {

System.out.println("3 - large straigt");

}

if (wins[3] == 0) {

System.out.println("4 - small straigt");

}

if (wins[4] == 0) {

System.out.println("5 - four of a kind");

}

if (wins[5] == 0) {

System.out.println("6 - three of a kind");

}

if (wins[6] == 0) {

System.out.println("7 - pair");

}

if (wins[7] == 0) {

System.out.println("8 - two pair");

}

if (wins[8] == 0) {

System.out.println("9 - number of 1's");

}

if (wins[9] == 0) {

System.out.println("10 - number of 2's");

}

if (wins[10] == 0) {

System.out.println("11 - number of 3's");

}

if (wins[11] == 0) {

System.out.println("12 - number of 4's");

}

if (wins[12] == 0) {

System.out.println("13 - number of 5's");

}

if (wins[13] == 0) {

System.out.println("14 - number of 6's");

}

if (wins[14] == 0) {

System.out.println("15 - chance");

}

choice = MiniYahtzee.inputInt("");

int x = 0, y = 0, winings = 0, winingsa = 0;

int twos = 0, threes = 0, fours = 0, fives = 0, sixes = 0;

Arrays.sort(aDice);

//Numbers

for (y = 0; y < 5; y++) {

if (aDice[y] == 1) {

ones++;

}

if (aDice[y] == 2) {

twos++;

}

if (aDice[y] == 3) {

threes++;

}

if (aDice[y] == 4) {

fours++;

}

if (aDice[y] == 5) {

fives++;

}

if (aDice[y] == 6) {

sixes++;

}

}

//Straights

if ((aDice[0] == aDice[1] - 1) && (aDice[1] == aDice[2] - 1)

&& (aDice[2] == aDice[3] - 1) && (aDice[3] == aDice[4] - 1)

&& (choice == 3)) {

winingsa = 1;

} else if ((ones > 0) && (twos > 0) && (threes > 0) && (fours > 0)) {

winingsa = 2;

} else if ((threes > 0) && (fours > 0) && (fives > 0) && (sixes > 0)) {

winingsa = 2;

} else if ((twos > 0) && (threes > 0) && (fours > 0) && (fives > 0)) {

winingsa = 2;

}

//Pairs

for (x = 0; x < 5; x++) {

if (x != 0) {

if ((aDice[0] == aDice[x])) {

winings++;

}

}

if ((x != 0) && (x != 1)) {

if ((aDice[1] == aDice[x])) {

winings++;

}

}

if ((x != 0) && (x != 1) && (x != 2)) {

if ((aDice[2] == aDice[x])) {

winings++;

}

}

if ((x != 0) && (x != 1) && (x != 2) && (x != 3)) {

if ((aDice[3] == aDice[x])) {

winings++;

}

}

}

//Winnings

if ((winingsa == 1) && (choice == 3)) {

System.out.println("You have a straight.");

score = 40;

} else if ((winingsa == 2) && (choice == 4)) {

System.out.println("You have a small straight.");

score = 30;

} else if ((winings == 10) && (choice == 1)) {

System.out.println("Yatzee!");

score = 50;

} else if ((choice == 6) && (winings >= 3)) {

System.out.println("You have three of a kind.");

score = aDice[0] + aDice[1] + aDice[2] + aDice[3] + aDice[4];

} else if ((choice == 7) && (winings > 0)) {

System.out.println("You have a pair.");

score = 5;

} else if ((winings == 2) && (choice == 8)) {

System.out.println("You have two pairs.");

score = 10;

} else if ((winings == 4) && (choice == 2)) {

System.out.println("You have a full house.");

score = 25;

} else if ((winings >= 6) && (choice == 5)) {

System.out.println("You have four of a kind.");

score = aDice[0] + aDice[1] + aDice[2] + aDice[3] + aDice[4];

} else if (choice == 9) {

System.out.println("You have " + ones + " ones.");

score = ones;

} else if (choice == 10) {

System.out.println("You have " + twos + " twos.");

score = twos * 2;

} else if (choice == 11) {

System.out.println("You have " + threes + " threes.");

score = threes * 3;

} else if (choice == 12) {

System.out.println("You have " + fours + " fours.");

score = fours * 4;

} else if (choice == 13) {

System.out.println("You have " + fives + " fives.");

score = fives * 5;

} else if (choice == 14) {

System.out.println("You have " + sixes + " sixes.");

score = sixes * 6;

} else if (choice == 15) {

score = aDice[0] + aDice[1] + aDice[2] + aDice[3] + aDice[4];

System.out.println("Your get " + score + " points.");

} else {

System.out.println("You got nothin'.");

score = 0;

}

}

public int score() {

return (score);

}

public int choice() {

return (choice);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote