简体   繁体   中英

How do you do re-rolls in a yahtzee program in java?

I've been working at an assignment for my intro to Java class, and I can't figure out how to accomplish the two things I need to do.

What I have now: I have created an array that rolls five dice, and displays the results. The code I have is as follows...

package operation.pkg7;

import java.util.Random;
import java.util.Scanner;
public class Operation7 {
 public static void main(String[] args) 
{    


    int[] dice = new int[5];
 Random r = new Random();

 //This makes the dice 
 for (int i = 0; i < 5; i++) {
 dice[i] = r.nextInt(6) + 1;
 }
 //This displays the dice rolls
 for (int i = 0; i < 5; i++) {
 System.out.print("Roll " + (i+1) + ":" );
 System.out.print(" " + dice[i] + " ");
 }
 System.out.println();
 //..........................................
 int[] counts = new int[6];
 for (int i = 0; i < 6; i++){
 counts[i] = 0;
 }

 //count up the values
 for (int i = 0; i < 5; i++) {
 int diceIndex = dice[i] - 1;
 counts[diceIndex]++;
 }
System.out.println();
 for (int i = 0; i < 6; i++){
 System.out.println("The number of " + (i+1) + "s is : " +
counts[i]);
}
}
}

Now, I successfully gives me the results of the die rolls, but I'm having problems figuring out how to do re-rolls. This is an example of what I'm wanting the program to ask for after displaying the initial die roll...

Roll 1: 2 Roll 2: 6 Roll 3: 1 Roll 4: 4 Roll 5: 2

Would you like to re-roll any dice? y/n
y

Which dice would you like to re-roll? 1-5
2, 3, 4

Roll 1: 2 Roll 2: 3 Roll 3: 2 Roll 4: 3 Roll 5: 2

Would you like to re-roll any dice? y/n
n

Roll 1: 2 Roll 2: 3 Roll 3: 2 Roll 4: 3 Roll 5: 2

That's the goal. Once I do that, I need to make the program display the best possible score... For example, if there are two threes and three twos, it needs to say it's a full house, or display yahtzee if there are five of the same number etc.

Anyone have any advice?

将代码置于do-while循环中,并在do-while循环条件检查之前放置用户输入的条件。

You have the results of your rolls store in the dice array you simply need to create new random results for the dice in those positions. Eg if you re-roll #2 then put a new random in index 1.

As for determining the various types of rolls such as full house you will want to define generic logic for each. You might for example define a method like:

public static boolean isFullHouse(int[] diceRoll){
    //return true if the roll is a full house, false otherwise
}

However, I think what you really want is not just to determine if a roll is a full house but to also calculate the total score using the scoring rules for that scenario.

EDIT #1:

Before going any further please, at a minimum, read the section on this page titled "Asking about homework" .

I will try to illustrate how I think you could accomplish a re-roll without providing full code at this point. As I stated above, since you need to prompt the user for which dice they intend to re-roll you can process that input and use it as indicies into your dice array. So if the users 2,3,4 as in your example you use Scanner.getLine() to read that input and then use String methods to break it apart. Once you have the individual pieces you can convert them to int values using the Integer class. Finally, subtract 1 from them to adjust for the fact that your dice array indicies begin at 0.

Now you have an array containing indicies into your dice array. Each item in this new array is just used to refer to an index of the dice array. So let's call this second array userInput . Continuing the 2,3,4 example userInput[0]=1 , userInput[1]=2 , userInput[2]=3 .

All you need to do is loop through userInput and each time through the loop generate a new random number. This random number is then stored in the dice array at the index stored in the current position of your userInput array.

As for determining the best possible score you could, as I suggested, write individual methods for each to determine if they are present and then another method to invoke each of them and report the greatest score possible.

Let's take one of them as an example like 3 of a Kind. Perhaps the simplest way to think about it would be to loop through all the possible rolls of the die (0 to 6) so that each time through the loop you're counting how many of each roll appear in the current dice roll. So in loop 1 you're looking for three 1's and in loop 2 you're looking for three 2's and so on.

Inside of this loop you create another loop to iterate through all of the dice in the current dice roll (your dice array) and each time through the loop you check to see if the current die is the number you're currently looking for from your outer loop. If it is the number from the outer loop then you increment a counter (which is defined and initialized in the outer loop but outside the inner loop so that it resets each time the outer loop increments) to represent how many of that number you have found so far.

If the counter reaches 3 before the inner loop ends you know you have 3 of a Kind, so have the method return true since you have no reason to continue (can't possibly have 2 different 3 of a Kind). Otherwise your method should return false to indicate there was no match.

Note that if you want to be ambitious you could exercise good programming practice and reuse code by making the limit of your counter a parameter to your method so that you can use the same logic for both 3 of a Kind and 4 of a Kind.

Hint there is a more clever way of doing this but I thought this approach would be more consistent with the constructs you already know.

I hope this helps to get you started!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM