Console Yahtzee Java Porgram with following requiemrnts -have several functions
ID: 3902149 • Letter: C
Question
Console Yahtzee Java Porgram with following requiemrnts
-have several functions
-create menu called in main
-at end ask user to start over or quit
-ask user to start rolling
-let user re-roll twice after first 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).
-let user select which value want to hold
Clear Array
-At the end of each round you will want to set the values in the array back to 0.
-has three rounds
Explanation / Answer
Hi Student,
Please find the code below :
import java.util.*;
import java.math.*;
class YahtzeeGame {
int num_of_rolls_remaining = 2;
final static int NUM_OF_DICE = 3;
public int rollDice() {
int diceRoll = (int) (Math.random() * 6) + 1;
num_of_rolls_remaining = num_of_rolls_remaining - 1;
return diceRoll;
}
public void testGame() {
for (int i = 0; i < NUM_OF_DICE; i++) {
System.out.println(rollDice());
}
}
}
public class ConsoleYahtzee {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int round = 1;
YahtzeeGame game = new YahtzeeGame();
do{
System.out.println("Round -> "+round);
game.testGame();
System.out.println("Do you want to play again ? Enter 0-Continue 1-Break ");
if(s.nextInt() == 1)
break;
round++;
}while(true);
}
}
Please find a screenshot of the output below :
Round -> 1
4
6
2
Do you want to play again ? Enter 0-Continue 1-Break
0
Round -> 2
2
3
4
Do you want to play again ? Enter 0-Continue 1-Break
1
If you like this answer, give a thumbs up! If you have any doubt feel free to revert back. Happy Learning :)