Yahtzee Array Метод для суммы костей - PullRequest
0 голосов
/ 10 декабря 2018

Я пытаюсь выяснить, как отформатировать код для метода массива, который легко отображает общий счет за игру Yahtzee.Я не могу на всю жизнь понять, как его ввести. Любая помощь с тем, что добавить, будет принята с благодарностью.Будь проще.Спасибо!

(sumDice - это то, что должно быть методом)

int[] dice = new int[5];             // the array of 5 dice
Scanner scnr= new Scanner(System.in);
Random randGen = new Random();      // new random number generator

public void playGame() {
    dice[0] = randGen.nextInt(6) + 1;
    dice[1] = randGen.nextInt(6) + 1;   
    dice[2] = randGen.nextInt(6) + 1;   
    dice[3] = randGen.nextInt(6) + 1;   
    dice[4] = randGen.nextInt(6) + 1;  

    displayDice();
    reRoll();
    displayDice();
    reRoll();
    displayDice();

    System.out.println("Which value do you want to count?");
    int valueToCount = scnr.nextInt();


    System.out.println("You have " + countDie(valueToCount) + " dice of value " + valueToCount);

                //sum of the dice
    System.out.println("The sum of the dice is " + sumDice() );

}



public int countDie(int valueToCount) {
 int count = 0;
    int i;
    for (i=0; i<dice.length; i++) {
        if (dice[i] == valueToCount) {
            count = count + 1;
        }
    }  
    return count;
}



public void displayDice() {
    int index;                          // counter for loop
    for (index=0; index<dice.length; index++) {
        System.out.print(dice[index] + " ");

    } 
    System.out.println();
}

public void reRoll() {
    // re-rolling
    String answer;
    int i;
    for (i=0; i<dice.length; i++ ) {
        System.out.println("Re-roll die " + i + " which is a " + dice[i] + " (y/n)?");
        answer = scnr.next();
        if (answer.equalsIgnoreCase("y")) {
            System.out.println("REROLLING THIS DIE!!!");
            dice[i] = randGen.nextInt(6) + 1;
        }
    }          
}

}

...