java - Reroll dices in yahtzee game -


i´m trying create simple yahtzee game array filled 5 random numbers , player can choose roll specific dices again. cant figure out how write method supposed replace specific numbers in array new random numbers , return it. suggestions how approach this?

import java.util.arrays; import java.util.random; import java.util.scanner;  public class yatzygame {      private final integer nbrofdice = 5;     private final integer dicemaxvalue = 6;     random rnd = new random();     scanner keyboard = new scanner(system.in);      public void startgame() {         int[] dice = new int[nbrofdice];         rolldice(dice);         printdice(dice);          system.out.println("do want reroll dices? " + "y/n");         string answer = keyboard.nextline();         if (answer.equalsignorecase("y")) {             rerolldice(dice);          } else if (answer.equalsignorecase("n")) {             calculatesum(dice);          } else {             system.out.println("wrong command!");         }      }      public int[] rolldice(int[] dice) {         (int = 0; < dice.length; i++) {             dice[i] = rnd.nextint(dicemaxvalue) + 1;         }         return dice;     }      public int[] rerolldice(int[] dice) {         system.out.println("what dices want reroll? dices index 0-4");         int dicetoreroll = keyboard.nextint();          // replace numbers @ index user specifies new random numbers , return array.      }      public void printdice(int[] dices) {          system.out.println("your dices show: " + arrays.tostring(dices));     }      public void calculatesum(int[] dices) {         int sum = 0;         (int : dices) {             sum += i;         }         if (sum == 30) {             system.out.println("yahtzee! total score 50! congratulations!");         } else             system.out.println("your total score is: " + sum);     }      public static void main(string[] args) {         new yatzygame().startgame();     }  } 

public int[] rolldice(int[] dice) {               system.out.println("what dice want reroll? dices index 0-4");              int dicetoroll = keyboard.nextint();              dice[dicetoroll] = rnd.nextint(dicemaxvalue) + 1;     } 

like can make function:

public int[] reroll(int amountofrerolls, int[] dices){      for(int =0;i<dicestoreroll;i++){        this.rolldice(dices);     }   return dices;  } 

this makes programm bit more modal since can reuse rolldice() method wherever need it. expand bit handing in indexes allowed rerolled in case needed.

edit: style


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -