5. Write a class called OneRoundOneRollYahtzee. The program should behave the sa
ID: 3861541 • Letter: 5
Question
5. Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputorGenerateDiceRolls program with the following additional feature. After each set of five dice rolls has been displayed in non-decreasing order, the set of five dice rolls will be scored according to the rules of the dice game Yahtzee. Before the score for the roll can be determined, the user must be prompted for a category to be used for scoring the dice roll. The categories are listed on the next two pages which come from: Note that the first six categories (collectively called the "upper section") and category 13 (called "Chance") can be chosen for any set of five dice rolls. However, categories 7-12 are only legal for certain dice rolls. If the user chooses a legal category for the dice roll, your program should compute the score as specified in the Scores column of the tables. However, if the user chooses an illegal category for the dice roll, your program should notify the user that his/her choice is illegal and allow him/her to select again. Note that we are not simulating the full game of Yahtzee. We are only simulating one round of the game and the five dice are only being rolled once during this round. If the user chooses to play again he/she will be starting a new one- round game with all of the scoring categories available in the sense that they are un-used. Please enter 1) to roll your own dice, 2) to let the computer roll the dice, or 3) to quit: 1 Please enter the five dice rolls: 42611 The five rolls in non-decreasing order are: 11246 You may select from the following scoring categories: Upper Section: 1. Aces 2. Twos 3, Threes 4. Fours 5. Fives 6. Sixes Lower Section: 7. Three of A Kind 8. Four of A Kind 9. Full House 10, Small straight 11. Large Straight 12... Yahtzee 13, Chance Please enter the number corresponding to your chosen category: 12 The Yahtzee category is not legal for this roll: 1 1246. Please enter the number corresponding to your chosen category: 13 Your score for the Chance category is: 14. Please enter 1) to roll your own dice, 2) to let the computer roll the dice, or 3) to quit: 3 Have a nice day!Explanation / Answer
package package1;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class OneRoundOneRollYahtZee {
public static void main(String args[]){
int max =6;
int arr1[] = new int[5];
int min=1;
Scanner sc = new Scanner(System.in);
Random t = new Random();
String str2="";
do{
System.out.println();
System.out.println();
System.out.print("Please enter 1) to roll your own dice 2) to let the computer roll dice, or 3) to quit: ");
int choice = sc.nextInt();
if(choice==3){
System.out.println("Have a noce day!");
break;
}
else if(choice ==1){
System.out.print("Please enter the 5 dice rolls: ");
String str = sc.next();
String str1[] = str.split(" ");
arr1 = new int[str1.length];
for(int i=0;i<str1.length;i++){
arr1[i] = Integer.parseInt(str1[i]);
}
Arrays.sort(arr1);
for(int i=0;i<arr1.length;i++){
System.out.print(arr1[i]+" ");
}
}
else if(choice ==2){
for(int i=0;i<5;i++){
int a =t.nextInt(max - min + 1) + min;
str2 = str2+a+" ";
}
String str1[] = str2.split(" ");
arr1 =new int[str1.length];
for(int i=0;i<str1.length;i++)
arr1[i]= Integer.parseInt(str1[i]);
Arrays.sort(arr1);
System.out.println();
System.out.print("five rolls in non decreasing order are ");
for(int i=0;i<arr1.length;i++)
System.out.print(arr1[i]+" ");
}
else
{
}
System.out.println();
System.out.println("You may select from the following scoring categories ");
System.out.println("Upper Section: ");
System.out.println("1. Aces");
System.out.println("2. Twos");
System.out.println("3. Thress");
System.out.println("4. Fours");
System.out.println("5. Fives");
System.out.println("6. Sixes");
System.out.println();
System.out.println("Lower Section:");
System.out.println("7. Three of a Kind");
System.out.println("8. Four of a Kind");
System.out.println("9. Full House ");
System.out.println("10. Small Straight");
System.out.println("11. Large Straight");
System.out.println("12. Yahtzee");
System.out.println("13. Chance");
System.out.println("----------");
int choice2 = sc.nextInt();
int count=0;
if(choice2==1){
count=0;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==1)
count++;
}
System.out.println("Your score for chance category is :"+count*1);
}
else if(choice2==2){
count=0;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==2)
count++;
}
System.out.println("Your score for chance category is :"+count*2);
}
else if(choice2==3){
count=0;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==3)
count++;
}
System.out.println("Your score for chance category is :"+count*3);
}
else if(choice2==4){
count=0;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==4)
count++;
}
System.out.println("Your score for chance category is :"+count*4);
}
else if(choice2==5){
count=0;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==5)
count++;
}
System.out.println("Your score for chance category is :"+count*5);
}
else if(choice2==6){
count=0;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==6)
count++;
}
System.out.println("Your score for chance category is :"+count*6);
}
else if(choice2==7){
count=0;
int arr[] ={0,0,0,0,0,0};
for(int i=0;i<arr1.length;i++)
{
arr[arr[i]-1]++;
}
boolean flag = false;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==3)
{
flag = true;
break;
}
}
if(flag==false)
{
System.out.println();
System.out.print("The Yahtzee category is not legal for this role:");
for(int i=0;i< arr1.length;i++){
System.out.print(arr1[i]+" ");
}
}
else{
System.out.println("Your score for chance category is :"+17);
}
}
else if(choice2==8){
count=0;
int arr[] ={0,0,0,0,0,0};
for(int i=0;i<arr1.length;i++)
{
arr[arr[i]-1]++;
}
boolean flag = false;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==4)
{
flag = true;
break;
}
}
if(flag==false)
{
System.out.println();
System.out.print("The Yahtzee category is not legal for this role:");
for(int i=0;i< arr1.length;i++){
System.out.print(arr1[i]+" ");
}
}
else{
System.out.println("Your score for chance category is :"+24);
}
}
else if(choice2==9){
count=0;
int arr[] ={0,0,0,0,0,0};
boolean flag2 = false;
for(int i=0;i<arr1.length;i++)
{
arr[arr[i]-1]++;
}
boolean flag = false;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==3)
{
flag = true;
break;
}
}
for(int i=0;i<arr.length;i++)
{
if(arr[i]==2)
{
flag2 = true;
break;
}
}
if(!(flag && flag2))
{
System.out.println();
System.out.print("The Yahtzee category is not legal for this role:");
for(int i=0;i< arr1.length;i++){
System.out.print(arr1[i]+" ");
}
}
else{
System.out.println("Your score for chance category is :"+25);
}
}
else if(choice2==10){
boolean arr[] =new boolean[5];
for(int i=0;i<arr1.length;i++)
{
arr[arr1[i]-1] = true;
}
int c =0 ;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==true)
{
c++;
}
else
{
c=0;
}
}
if(c!=4)
{
System.out.println();
System.out.print("The Yahtzee category is not legal for this role:");
for(int i=0;i< arr1.length;i++){
System.out.print(arr1[i]+" ");
}
}
else{
System.out.println("Your score for chance category is :"+30);
}
}
else if(choice2==11){
boolean arr[] =new boolean[5];
for(int i=0;i<arr1.length;i++)
{
arr[arr1[i]-1] = true;
}
int c =0 ;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==true)
{
c++;
}
else
{
c=0;
}
}
if(c!=5)
{
System.out.println();
System.out.print("The Yahtzee category is not legal for this role:");
for(int i=0;i< arr1.length;i++){
System.out.print(arr1[i]+" ");
}
}
else{
System.out.println("Your score for chance category is :"+40);
}
}
else if(choice2==12){
count=0;
int arr[] ={0,0,0,0,0,0};
for(int i=0;i<arr1.length;i++)
{
arr[arr[i]-1]++;
}
boolean flag = false;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==5)
{
flag = true;
break;
}
}
if(flag ==false)
{
System.out.println();
System.out.print("The Yahtzee category is not legal for this role:");
for(int i=0;i< arr1.length;i++){
System.out.print(arr1[i]+" ");
}
}
else{
System.out.println("Your score for chance category is :"+50);
}
}
else if(choice2==13){
int sum=0;
System.out.println();
System.out.print("Your score for chance category is :"+25);
for(int i=0;i<arr1.length;i++){
sum = sum+arr1[i];
}
System.out.print(sum);
}
}while(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.