# Thread: Selecting which dice to re-roll

1. Member
Join Date
Oct 2013
Posts
2
Rep Power
0

## Selecting which dice to re-roll

I am trying to teach myself how to program in Java, and I am writing a Yahtzee program, which I thought would be considerably simpler than it actually is. I am having difficulty figuring out how to select a certain number of dice to re-roll, for example say the first roll produces: 2, 4, 2, 1, 2, and the player wants to re-roll the 4 and 1. The way I am doing it now is by asking how many dice the player wants to re-roll, and then having different methods for each answer 0 to 5, so with the example roll above the player would input 2, then would be prompted to input the places of the two dice he wants to re-roll, so he would input 2 and 4, which would then cause these two dice to be re-rolled, but I know there has to be a much easier way to do this. If someone could point me in the right direction, it would be greatly appreciated. As you can see I haven't wrote the code for the methods yet, because I realized there has to be an easier way.

Java Code:
```import java.util.Scanner;
import java.util.Random;

public class Yahtzee
{
private static final Random randomNumber = new Random();
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
int die1 = rollDice();
int die2 = rollDice();
int die3 = rollDice();
int die4 = rollDice();
int die5 = rollDice();
int [] dice = { die1 , die2 , die3 , die4 , die5 };
System.out.println( die1 + " " + die2 + " " + die3 + " " + die4 + " " + die5 );
System.out.println( "Do you want to reroll any dice?" );
System.out.println( "Enter number of dice you wish to reroll: " );
int selection = input.nextInt();
if ( selection == 1 )
rerollOne();
else if ( selection == 2 )
rerollTwo();
else if ( selection == 3 )
rerollThree();
else if ( selection == 4 )
rerollFour();
else if ( selection == 5 )
rerollFive();
else
System.out.println( "No such selection." );
}
public static int rollDice()
{
int die = 1 + randomNumber.nextInt( 6 );
return die;
}
public static String rerollOne()
{
String rerollOne = "You chose 1.";
System.out.println( "You chose one." );
return rerollOne;
}
public static String rerollTwo()
{
String rerollTwo = "You chose 2.";
System.out.println( "You chose two." );
return rerollTwo;
}
public static String rerollThree()
{
String rerollThree = "You chose 3.";
System.out.println( "You chose three." );
return rerollThree;
}
public static String rerollFour()
{
String rerollFour = " You chose 4.";
System.out.println( "You chose four." );
return rerollFour;
}
public static String rerollFive()
{
String rerollFive = "You chose 5.";
System.out.println( "You chose five." );
return rerollFive;
}
}```

2. ## Re: Selecting which dice to re-roll

The best way to do this would be to have a Die class to represent the 5 dice. In that class you would have an instance variable of type boolean to keep track if it should be rolled or not. Then iterate over your 5 Die objects and if the boolean is set to true* roll it again.

* Or it could be false depending how you set up the logic.

3. Member
Join Date
Oct 2013
Posts
2
Rep Power
0

## Re: Selecting which dice to re-roll

Thank you very much for your feedback. I actually decided to do it a different way. I think it might make it a little easier if later I decide to attempt to convert it to a GUI program. If you could please take a look at it and give me any critiques it would be appreciated. Specifically if you could give me advice for methods I am really struggling with this, looking at my code am I doing it correctly (most efficiently). Thank You.
Java Code:
```/*
Author: Jon Davis
File: Yahtzee.java
Date: 2013 Oct 27
Description: This program is simple yahtzee program
that runs in the console
*/
import java.util.Scanner;
import java.util.Random;

public class Yahtzee
{
private static final Random randomNumber = new Random();
private static final Scanner input = new Scanner( System.in );
public static int s1, s2, s3, s4, s5; //declaring the variables that will be used in the
//selecting which dice to re-roll
public static int rollPerTurn; //declaring variable to keep track of rolls per turn
private static int [] dice = new int [5]; //declaring an int array representing the 5 dice
public static void main( String args[] )
{
while ( rollPerTurn < 3 ){
System.out.println();
s1 = input.nextInt();//obtaining input to determine which dice to re-roll
s2 = input.nextInt();
s3 = input.nextInt();
s4 = input.nextInt();
s5 = input.nextInt();
reRoll();
}
}
public static void menu()
{
System.out.println("1. Play Yahtzee");
System.out.println("2. Help");
System.out.println("3. Quit");
System.out.println("********************");
int selection = input.nextInt();
switch ( selection )
{
case 1:
firstRoll();
break;
case 2:
yahtzeeHelp();
break;
case 3:
exit();
break;
default:
System.out.println( "No such selection." );
}
}
public static int firstRoll() //method to roll 5 dice
{
for ( int i = 0; i < dice.length; i++){
dice[i] = rollDice();
}
System.out.println("Select which dice to reroll.");
for ( int a = 0; a < dice.length; a++ ){
System.out.print(dice[a] + " " );
}
rollPerTurn = 1; //indicating this is the first roll
return rollPerTurn;
}
public static int [] reRoll() //method to re-roll certain dice
{
if ( s1 == 1 ){
dice[0] = rollDice(); //re-roll Die 1
}
if ( s2 == 1 ){
dice[1] = rollDice(); //re-roll Die 2
}
if ( s3 == 1 ){
dice[2] = rollDice(); //re-roll Die 3
}
if ( s4 == 1 ){
dice[3] = rollDice(); //re-roll Die 4
}
if ( s5 == 1 ){
dice[4] = rollDice(); //re-roll Die 5
}
rollPerTurn++;
for ( int b = 0; b < dice.length; b++ ){
System.out.print(dice[b] + " " ); //displaying the dice after re-roll
}
return dice;
}
public static int rollDice() //method to roll a die
{
int die = 1 + randomNumber.nextInt( 6 );
return die;
}
public static void yahtzeeHelp() //the "help" screen
{
System.out.println("Your dice will show up and you will input a 1 or a 0");
System.out.println("under each die.  A 1 indicates you wish to re-roll");
System.out.println("that particular die, and a 0 means you wish to keep it.");
System.out.println();
}
public static void exit()
{
System.out.println( "Thank you for playing. Good bye." );
System.exit(0);
}
}```

4. ## Re: Selecting which dice to re-roll

Don't worry about efficiency. If you program performs like crap, then is a good time. For learning to code, "most efficient" is the least of your concearns.

I think it might make it a little easier if later I decide to attempt to convert it to a GUI program.
That's where you're wrong. It will be harder. Imagine you having all this spaghetti code, and then have to write a GUI program around it. It surely doesn't help. Being able to move responsibilities to the right classes is very important. Even for little command line games.

I have to agree with Junky about the Die class. It makes your coding life much easier:
Java Code:
```public class Die {
private int number;
private boolean shouldRoll;

public void roll() {
number =  1 + randomNumber.nextInt( 6 );
}

public isShouldRoll() {
return shouldRoll;
}
...
}

List<Die> dice = new ArrayList<>();
for ( int i = 0; i < 6; i++)  {
}```
(randomNumber and getters/setters omitted as an excercise for the reader.)

Imagine the ease of rolling like this. This replaces your 15 lines (70-84) of code:
Java Code:
```for ( Die d : dice) {
if ( d.isShouldRoll() ) {
d.roll();
}
}```
That's how a Java coder rolls... *badumm tsssss*
Last edited by SurfMan; 10-28-2013 at 08:44 AM. Reason: Added quote and comment

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•