Question

i need to make a java program to play Yahtzee. it needs to use arrays. can...

i need to make a java program to play Yahtzee. it needs to use arrays. can you help me?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Description

  • Ask the user how many dice the he/she wants to roll
  • create an array of Die objects of the specified size
  • Program then repeatedly roll all the dice until a Yahtzee is rolled occurs when all the dice have the same face value.
  • Report how many rolls it took to obtain the Yahtzee.
  • Program asks the user the option to run it again.

Program

import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in); //create an object and reading input from the user
introduction(); //calling the introduction method
boolean runAgain = true; // declaring runAgain to true
while (runAgain){ // while statement runs until runagain is false
boolean rolledYahtzee = false; // declaring rolledYahtzee to false
int numRolls = 0; // initializing num of rolls as 0
Die [] dieArray = dieArray(keyboard); // creating an object diearray
while(!rolledYahtzee){ // while statement runs until rolledYahtzee is true
dieArray = rollDice (dieArray);
numRolls++;// incrementing num of rolls
printDots(numRolls);
rolledYahtzee = checkForYahtzee(dieArray, numRolls);// calling the method checkForYahtzee
}
printResults (numRolls, dieArray); // calling print method to print the results
runAgain = runAgain(keyboard);
}
}

public static void introduction(){ // introduction method
System.out.println("This program will");
}


public static Die [] dieArray(Scanner keyboard){ // method to check weather the given number is postive or not
int numDice = getInt(keyboard, "How many dice do you want to throw? ");
while (numDice <= 0){
System.out.println("Sorry, you must enter a positive number.");
numDice = getInt(keyboard, "How many dice do you want to throw? ");
}
Die [] dieArray = new Die [numDice];
return dieArray;
}

public static int getInt (Scanner keyboard, String prompt){ // method to check weather the given number is integer or a string
System.out.print(prompt);
while (!keyboard.hasNextInt()){
keyboard.next();
System.out.println("Sorry, you must enter an integer.");
System.out.print(prompt);
}
return keyboard.nextInt();
}

public static Die [] rollDice(Die [] dieArray){
for (int i = 0; i < dieArray.length; i++){
dieArray[i] = new Die ();
dieArray[i].roll();
dieArray[i].faceValue = dieArray[i].getValue();
}
return dieArray;
}

public static void printDots(int numRolls){
System.out.print(".");
if ((numRolls%50) == 0){
System.out.println();
}
}

public static boolean checkForYahtzee(Die[] dieArray, int numRolls) {
for(int i = 0; i < dieArray.length; i++) {
for(int j = i+1; j < dieArray.length; j++) {
if(dieArray[i].getValue() != dieArray[j].getValue()) {
return false;
}
}
}
return true;
}

public static void printResults (int numRolls, Die[] dieArray){
System.out.println();
System.out.println("Yahtzee!!");
System.out.print("After " + numRolls + " rolls, I finally rolled ");
System.out.println(dieArray.length + " " + dieArray[0].getValue() + "'s");
System.out.println();   
}

public static boolean runAgain (Scanner keyboard){
System.out.print("Do you want to run another experiment? (y|n)): ");
String answer = keyboard.next().trim().toLowerCase();
keyboard.nextLine();
System.out.println();
return (answer.charAt(0) == 'y');
}
}
class Die {
int faceValue;
public void roll(){
faceValue = (int)(Math.random() * 6 + 1);
}

public int getValue(){
return faceValue;   
}

public String toString(){
return Integer.toString(faceValue);
}
}

Note: For any query leave a comment.

Add a comment
Know the answer?
Add Answer to:
i need to make a java program to play Yahtzee. it needs to use arrays. can...
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
  • I need help with my assignment. It is a java program. Please make it as simple...

    I need help with my assignment. It is a java program. Please make it as simple as you can. Create an ArrayList with elements the objects of previous class. Using a simple loop populate the ArrayList with data from some arrays with data, or from console. Use a loop with iterator and write the information in a File using PrintWriter. Use this address for the file (c:\\result\\MyData.txt). Use the Scanner class to display the content of the file on console.

  • 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...

  • 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 help with the following. I need to write a program code in Java using...

    I need help with the following. I need to write a program code in Java using NetBeans. It is from How to Program Java book Chapter 2. This program should calculate the packing of Candles. First read: The number of candles The number of candles that fit in each case Then calculate and print: The number of full cases The number of candles left over (partial case) If this order is large (more than 5 full cases needed) An extra...

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

  • Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that...

    Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values

  • I was wondering if I could get some help on the last program I need to...

    I was wondering if I could get some help on the last program I need to complete for an engineering C++ class. It is a very simple program however there is one part of the problem stumping me. Here is the problem statement . Write a C++ program that reads in from a file two month names, followed by the rainfall amounts for each month in that span of months. These rain amounts are totaled, and then an average for...

  • need help writing a translator program in java. the program needs to be able to preform...

    need help writing a translator program in java. the program needs to be able to preform basic math calculations of addition,subtraction,division,multiplication. I also need to create my own syntax for them so for example if a user would be prompted to enter the syntax for addition they could type in ":/a 2 3" and the output would be 5 subtraction "//s 5 3 and the ouput would be 2, division "!!d 6 3" the output would be 2, multiplication "**m...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • Intro to Java. Can someone please help me answer this question. Create a java program playing...

    Intro to Java. Can someone please help me answer this question. Create a java program playing the game of craps with a random number generator. Write a method craps that plays the game of craps and it should return a 1 representing a win, a 2 representing a loss, and a 0 representing the need to toss the pair of dice again. The main method should ask how many games you wish to play then call the method craps. Main...

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