
/*
 * 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 nPlayers;
    private String[] playerNames;
    private YahtzeeDisplay display;
    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
        int currentPlayerNumber = 1;
        String currentPlayerName = playerNames[currentPlayerNumber - 1];
        display.printMessage(
                String.format("%s's turn! Click \"Roll Dice\" button to roll the dice.", currentPlayerName));
        display.waitForPlayerToClickRoll(currentPlayerNumber);
        rollDice();
        display.waitForPlayerToSelectDice();
    }

    private void rollDice() {
        int[] diceValues = new int[N_DICE];
        for (int i = 0; i < N_DICE; i++) {
            diceValues[i] = rgen.nextInt(1, 6);
        }
        display.displayDice(diceValues);
    }
}

@@ -270,16 +270,158 @@
 tants %7B%0A
+    private String currentPlayerName;%0A    private int currentPlayerNumber;%0A%0A    private int%5B%5D diceValues;%0A    private YahtzeeDisplay display;%0A
     priv
@@ -476,44 +476,8 @@
 es;%0A
-    private YahtzeeDisplay display;%0A
     
@@ -1168,20 +1168,16 @@
         
-int 
 currentP
@@ -1201,23 +1201,16 @@
         
-String 
 currentP
@@ -1259,16 +1259,202 @@
 r - 1%5D;%0A
+        handleFirstDiceRoll();%0A        handleSubsequentDiceRoll();%0A        handleSubsequentDiceRoll();%0A        handleCategorySelection();%0A    %7D%0A%0A    private void handleFirstDiceRoll() %7B%0A
         
@@ -1660,144 +1660,371 @@
     
-rollDice();%0A        display.waitForPlayerToSelectDice();%0A    %7D%0A%0A    private void rollDice() %7B%0A        int%5B%5D diceValues = new int%5BN_DICE%5D
+diceValues = new int%5BN_DICE%5D;%0A        for (int i = 0; i %3C N_DICE; i++) %7B%0A            diceValues%5Bi%5D = rgen.nextInt(1, 6);%0A        %7D%0A        display.displayDice(diceValues);%0A    %7D%0A%0A    private void handleSubsequentDiceRoll() %7B%0A        display.printMessage(%22Select the dice you wish to re-roll and click %5C%22Roll Again%5C%22.%22);%0A        display.waitForPlayerToSelectDice()
 ;%0A  
@@ -2056,32 +2056,80 @@
  N_DICE; i++) %7B%0A
+            if (display.isDieSelected(i)) %7B%0A    
             dice
@@ -2152,32 +2152,36 @@
 .nextInt(1, 6);%0A
+    
         %7D%0A      
@@ -2166,32 +2166,42 @@
 ;%0A            %7D%0A
+        %7D%0A
         display.
@@ -2230,11 +2230,589 @@
 );%0A    %7D
+%0A%0A    private void handleCategorySelection() %7B%0A        display.printMessage(%22Select a category for this roll.%22);%0A        int category;%0A        while (true) %7B%0A            category = display.waitForPlayerToSelectCategory();%0A            if (YahtzeeMagicStub.checkCategory(diceValues, category)) %7B%0A                display.printMessage(%22Test%22);%0A                break;%0A            %7D else %7B%0A                display.printMessage(%22Invalid category selected. Please select another.%22);%0A            %7D%0A        %7D%0A        // display.updateScorecard(category, currentPlayerNumber, score);%0A    %7D
 %0A%7D%0A

Handle subsequent dice rolls and start work on handling category selection