
/*
 * File: Yahtzee.java
 * ------------------
 * This program will eventually play the Yahtzee game.
 */

import acm.io.IODialog;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;

public class Yahtzee extends GraphicsProgram implements YahtzeeConstants {
    private int category;
    private String currentPlayerName;
    private int currentPlayerNumber;

    private int[] diceValues;
    private YahtzeeDisplay display;
    private int nPlayers;
    private String[] playerNames;
    private RandomGenerator rgen = new RandomGenerator();

    public static void main(String[] args) {
        new Yahtzee().start(args);
    }

    @Override
    public void run() {
        IODialog dialog = getDialog();
        nPlayers = dialog.readInt("Enter number of players");
        playerNames = new String[nPlayers];
        for (int i = 1; i <= nPlayers; i++) {
            playerNames[i - 1] = dialog.readLine("Enter name for player " + i);
        }
        display = new YahtzeeDisplay(getGCanvas(), playerNames);
        playGame();
    }

    private void playGame() {
        playTurn();
    }

    private void playTurn() {
        // TODO: change currentPlayerNumber
        currentPlayerNumber = 1;
        currentPlayerName = playerNames[currentPlayerNumber - 1];
        handleFirstDiceRoll();
        handleSubsequentDiceRoll();
        handleSubsequentDiceRoll();
        handleCategorySelection();
        updateScore();
    }

    private void handleFirstDiceRoll() {
        display.printMessage(
                String.format("%s's turn! Click \"Roll Dice\" button to roll the dice.", currentPlayerName));
        display.waitForPlayerToClickRoll(currentPlayerNumber);
        diceValues = new int[N_DICE];
        for (int i = 0; i < diceValues.length; i++) {
            diceValues[i] = rgen.nextInt(1, 6);
        }
        display.displayDice(diceValues);
    }

    private void handleSubsequentDiceRoll() {
        display.printMessage("Select the dice you wish to re-roll and click \"Roll Again\".");
        display.waitForPlayerToSelectDice();
        for (int i = 0; i < diceValues.length; i++) {
            if (display.isDieSelected(i)) {
                diceValues[i] = rgen.nextInt(1, 6);
            }
        }
        display.displayDice(diceValues);
    }

    private void handleCategorySelection() {
        display.printMessage("Select a category for this roll.");
        while (true) {
            category = display.waitForPlayerToSelectCategory();
            if (YahtzeeMagicStub.checkCategory(diceValues, category)) {
                break;
            } else {
                display.printMessage("Invalid category selected. Please select another.");
            }
        }
    }

    private int calculateScore() {
        int score = 0;

        switch (category) {
        case ONES:
            for (int diceValue : diceValues) {
                if (diceValue == 1) {
                    score += 1;
                }
            }
            break;
        case TWOS:
            for (int diceValue : diceValues) {
                if (diceValue == 2) {
                    score += 2;
                }
            }
            break;
        case THREES:
            for (int diceValue : diceValues) {
                if (diceValue == 3) {
                    score += 3;
                }
            }
            break;
        case FOURS:
            // TODO
            break;
        case FIVES:
            // TODO
            break;
        case SIXES:
            // TODO
            break;
        case THREE_OF_A_KIND:
            // TODO
            break;
        case FOUR_OF_A_KIND:
            // TODO
            break;
        case FULL_HOUSE:
            score = 35;
            break;
        case SMALL_STRAIGHT:
            score = 30;
            break;
        case LARGE_STRAIGHT:
            score = 40;
            break;
        case YAHTZEE:
            score = 50;
            break;
        case CHANCE:
            // TODO
            break;
        default:
        }

        return score;
    }

    private void updateScore() {
        int score = calculateScore();
        // TODO
        display.printMessage("Score: " + score);
        // display.updateScorecard(category, currentPlayerNumber, score);
    }
}

@@ -101,16 +101,42 @@
 e.%0A */%0A%0A
+import java.util.Arrays;%0A%0A
 import a
@@ -2887,111 +2887,86 @@
     
-for (int diceValue : diceValues) %7B%0A                if (diceValue == 1) %7B%0A                    score += 1
+score = Arrays.stream(diceValues).filter(w -%3E w == 1).sum();%0A            break
 ;%0A  
@@ -2975,69 +2975,38 @@
     
- 
-       %7D%0A            %7D%0A            break;%0A        case TWOS:
+case TWOS:%0A            // TODO
 %0A   
@@ -3005,32 +3005,35 @@
 TODO%0A           
+ //
  for (int diceVa
@@ -3056,35 +3056,34 @@
 ) %7B%0A            
-   
+//
  if (diceValue =
@@ -3093,39 +3093,34 @@
 ) %7B%0A            
-      
- 
+//
  score += 2;%0A   
@@ -3120,35 +3120,34 @@
  2;%0A            
-   
+//
  %7D%0A            %7D
@@ -3136,32 +3136,35 @@
 // %7D%0A           
+ //
  %7D%0A            b
@@ -3197,24 +3197,47 @@
 %0A           
+ // TODO%0A            //
  for (int di
@@ -3268,27 +3268,26 @@
             
-   
+//
  if (diceVal
@@ -3305,31 +3305,26 @@
             
-   
-    
+//
  score += 3;
@@ -3328,35 +3328,34 @@
  3;%0A            
-   
+//
  %7D%0A            %7D
@@ -3344,32 +3344,35 @@
 // %7D%0A           
+ //
  %7D%0A            b
@@ -3993,39 +3993,72 @@
 CE:%0A            
-// TODO
+score = Arrays.stream(diceValues).sum();
 %0A            bre

Use java 8 stream API for calculating scores