Question

Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is...

Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is as follows: InputOrGenerateDiceRolls program should ask the user whether the user prefers 1) to roll his/her own dice, 2) use computer-generated dice rolls, or 3) quit the program. If the user prefers to roll his/her own dice, the program should prompt the user to enter 5 digits in the range from 1-6 separated by spaces in any order with duplicates allowed. However, if the user prefers to use computer-generated dice rolls, generate 5 random digits in the range from 1-6 with duplicates allowed. Next, regardless of the user’s preference, display the 5 digits (representing 5 dice rolls) in non-decreasing order.) 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.

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: 4 2 6 1 1
The five rolls in non-decreasing order are: 1 1 2 4 6
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 1 2 4 6.
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!

Coding Style: In java, use descriptive variable names. Use consistent indentation. Only import java.util.Scanner to solve the problem. Use standard Java naming conventions forvariableAndMethodNames, ClassNames, CONSTANT_NAMES. Include a reasonable amount of comments. Create a class by a specific name. The class should have a main method. You are also encouraged (but not required) to write “helper” methods for each class which “help” the main method by breaking the problem into smaller pieces. Please do not call any methods in the Java class library specifically intended for sorting.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
       Random t = new Random();
       String str2="";
       do{
           sc = new Scanner(System.in);
       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 nice day!");
           break;
       }
       else if(choice ==1){
           Scanner sc1 = new Scanner(System.in);
           System.out.println();
           System.out.print("Please enter the 5 dice rolls: ");
           String str = sc1.nextLine();
          
           String str1[] = str.split(" ");
          
           arr1 = new int[str1.length];
          
           for(int i=0;i<str1.length;i++){
               arr1[i] = Integer.parseInt(str1[i]);
           }
           System.out.println();
           System.out.print("The five rolls in non decreasing order are");
           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("----------");
       String choiceStr = sc.next();
       int choice2 = Integer.parseInt(choiceStr);
       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[arr1[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[arr1[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[arr1[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[arr1[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);
   }

}

workspace- Java - HomeworkLib pracrise/src/pa File Edit Source Refactor Navigate Search Project Run Window Help el/OneRoundOneRollY

Add a comment
Know the answer?
Add Answer to:
Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In...

    Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make certain combinations. For example, the Yahtzee combination is formed by all five dice having the same value. A large straight occurs when all of the dice are in consecutive order (e.g., 1,2,3,4,5 or 2,3,4,5,6). If you haven’t played Yahtzee, you can find out more from various online sources such as Wikipedia. Once...

  • I need java code for this . The assignment requires using objects Exercise 2 of 2:...

    I need java code for this . The assignment requires using objects Exercise 2 of 2: Create a program that lets the player play a simplified game of Yahtzee. The game starts with the user throwing five 6-sided dice. The user can choose which dice to keep and which dice to re-roll. The user can re-roll a maximum of 2 times. After two times, the user has 5 dice with a certain value. The player then has to choose where...

  • Can anyone help me with this java program? We are still very early with lessons so...

    Can anyone help me with this java program? We are still very early with lessons so no advanced code please. And if you comment throughout the code that would be incredibly helpful. Thank you Create 2 Java files for this assignment: Die.java and TestDie.java Part 1 - The Die Class             The program Design a Die class that contains the following attributes: sides: represents how many sides a dice has. A dice usually has 6 sides but sometimes could have...

  • Hi, I need help with this question to my coding assignment. I completed letter A in...

    Hi, I need help with this question to my coding assignment. I completed letter A in the remaining program I just need help with letter "B" and by the way this is C programming. If you can't do it, just explain to me in very simple terms what I would do. void updateScores(int scoreCard[CATEGORIES][COLS], int category,int dice [5]) { switch(category) { case ONE: printf("Scoring Ones...\n"); break; case TWO: printf("Scoring Twos...\n"); break; case THREE: printf("Scoring Threes...\n"); break; case FOUR: printf("Scoring Fours...\n");...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

  • can someone help me with this C++ problem write your game() function and 3+ functions that...

    can someone help me with this C++ problem write your game() function and 3+ functions that simulate the game of Jeopardy Dice according to the following game mechanics and specifications. Game Mechanics There are two players: the user and the computer, who alternate turns until one of them reaches 80 points or higher. The user is always the first player and starts the game. If any player reaches 80 points or more at the end of their turn, the game...

  • Write a program which implements a dice game. Name your program file 'YourUsernameA105.py, e.g. afer023A1Q5.py. The...

    Write a program which implements a dice game. Name your program file 'YourUsernameA105.py, e.g. afer023A1Q5.py. The aim of the game is to reach a score as close as possible to 100 (but not over 100) in three rounds. Each round consists of throwing five random dice, the user then chooses two of the dice values where the two dice values chosen form a two digit score which is added to the user's current total, e.g., if the user first chooses...

  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

  • USE PYTHON ONLY Please write a Python program to let you or the user play the...

    USE PYTHON ONLY Please write a Python program to let you or the user play the game of rolling 2 dice and win/lose money. Initially, you have 100 dollars in your account, and the dealer also has 100 dollars in his/her account. You would be asked to enter a bet to start the game. If your bet is zero, the game would be stopped immediately. Otherwise, dealer would roll 2 dice and get a number from ( random.randint(1, 6) +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT