From f9bb2054dbc1b260c41a9ea2a521678a5a018a7c Mon Sep 17 00:00:00 2001
From: "Bradley M. Small" <bradley_small@hotamil.com>
Date: Mon, 16 Aug 2021 17:16:57 -0400
Subject: [PATCH] reduced code in init by moving inits to separate methods

---
 README.md                                     | 20 ++++--
 pom.xml                                       |  4 +-
 src/main/java/com/small/dicegame/App.java     | 71 +++++++++++--------
 src/main/java/com/small/dicegame/Die.java     |  4 +-
 src/main/java/com/small/dicegame/DieBox.java  |  9 ++-
 .../java/com/small/dicegame/DiceGameTest.java |  3 +-
 6 files changed, 67 insertions(+), 44 deletions(-)

diff --git a/README.md b/README.md
index e6ae62c..ead025b 100644
--- a/README.md
+++ b/README.md
@@ -1,20 +1,30 @@
 # DiceGame will play a game called 24.
+
 **Copyright © 2021 Bradley M. Small**
 
 ## **Rules**
 
-The rules are simple enough. Simply roll the dice and hold at least one die per roll. When you hold the last die, the game is complete. In order to score you must retain at least one die with a value of 2 and one die with a value of 4. Your final score is the sum of the other four dice. For example 2,4,6,6,6,6 is a perfect score of 24. While 6,6,6,6,6,6 is zero because you have not held both a 2 and a 4.
+The rules are simple enough. Simply roll the dice and hold at least one die per roll. When you hold the last die, the
+game is complete. In order to score you must retain at least one die with a value of 2 and one die with a value of 4.
+Your final score is the sum of the other four dice. For example 2,4,6,6,6,6 is a perfect score of 24. While 6,6,6,6,6,6
+is zero because you have not held both a 2 and a 4.
 
 ## **Code**
 
-The source code is Java. App and DiePanel contain the code for the GUI Application. DiceGame, Die, and Hand contain the code for the game logic itself. Development of the game used TDD, and the tests are in DiceGameTest. Once GUI development began, it was a goal to keep the game logic separate from the GUI logic as much as possible. It is my goal to create a WebUI and a TextUI to this game that will work without modification to the game code itself.
+The source code is Java. App and DiePanel contain the code for the GUI Application. DiceGame, Die, and Hand contain the
+code for the game logic itself. Development of the game used TDD, and the tests are in DiceGameTest. Once GUI
+development began, it was a goal to keep the game logic separate from the GUI logic as much as possible. It is my goal
+to create a WebUI and a TextUI to this game that will work without modification to the game code itself.
 
 ## **Suggestions**
 
-This code is intended as an exercise for me to apply my learning of Java. I am quite open to suggestions and recommendations for improvement. 
+This code is intended as an exercise for me to apply my learning of Java. I am quite open to suggestions and
+recommendations for improvement.
 
 ## **License**
 
-If you wish to use any of this code for personal and/or educational purposes you may freely do so as long as you do not claim it as your own. Give a guy a little credit if you use his work. 
+If you wish to use any of this code for personal and/or educational purposes you may freely do so as long as you do not
+claim it as your own. Give a guy a little credit if you use his work.
 
-If you wish to make money with this code, such as publishing it in a tutorial or making a salable product I want a cut of the action. As unlikely as this may be, simply contact me, so we can work out the details. 
+If you wish to make money with this code, such as publishing it in a tutorial or making a salable product I want a cut
+of the action. As unlikely as this may be, simply contact me, so we can work out the details. 
diff --git a/pom.xml b/pom.xml
index 6d79a35..4f203ff 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,8 +4,8 @@
   ~ All rights reserved.
   -->
 
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns="http://maven.apache.org/POM/4.0.0"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
diff --git a/src/main/java/com/small/dicegame/App.java b/src/main/java/com/small/dicegame/App.java
index c23e9b6..022f9cf 100644
--- a/src/main/java/com/small/dicegame/App.java
+++ b/src/main/java/com/small/dicegame/App.java
@@ -9,25 +9,48 @@ import javax.swing.*;
 import java.util.Arrays;
 
 public class App extends JFrame {
-    public static final int NUMBER_OF_DICE = 6;
+    private static final int NUMBER_OF_DICE = 6;
     private final DieBox[] dieBoxes = new DieBox[NUMBER_OF_DICE];
     private final JButton rollButton = new JButton("Roll");
     private final JButton newGameButton = new JButton("New Game");
     private final JTextArea scoreText = new JTextArea();
+    private final Box diceBox = Box.createHorizontalBox();
+    private final JPanel buttonPanel = new JPanel();
+
     private final transient DiceGame game = new DiceGame();
 
     App() {
         initGui();
     }
 
-    //TODO Try to keep initialization methods reasonably short, could move construction parts to additional methods
+    public static void main(String[] args) {
+        new App();
+    }
+
     private void initGui() {
         setTitle("24 Dice Game");
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
 
-        Box diceBox = Box.createHorizontalBox();
-        //TODO Could move to separate method: buildBoxDiceGUI(diceBox)
+        buildDiceBoxGUI();
+        add(diceBox);
+
+        initRollButton();
+        initGameNewButton();
+        buttonPanel.add(rollButton);
+        buttonPanel.add(newGameButton);
+        add(buttonPanel);
+
+        initScoreText();
+        add(scoreText);
+
+        pack();
+        setSize(500, 300);
+        setLocationRelativeTo(null);
+        setVisible(true);
+    }
+
+    private void buildDiceBoxGUI() {
         for (int index = 0; index < NUMBER_OF_DICE; index++) {
             dieBoxes[index] = new DieBox(index);
             dieBoxes[index].addActionListener(e -> {
@@ -40,22 +63,25 @@ public class App extends JFrame {
             });
             diceBox.add(dieBoxes[index]);
         }
-        add(diceBox);
+    }
 
-        JPanel buttonPanel = new JPanel();
-        //TODO Could move to separate method: initRollButton()
+    private void initScoreText() {
+        scoreText.setText("Score = 0");
+        scoreText.setEnabled(false);
+    }
+
+    private void initRollButton() {
         rollButton.setEnabled(false);
         rollButton.addActionListener(e -> {
-            if (areAllHeld()) {
+            if (isAllHeld()) {
                 for (DieBox d : dieBoxes) d.setButtonEnabled(true);
             }
             rollButton.setEnabled(false);
             updateDiePanelValues(roll());
         });
-        buttonPanel.add(rollButton);
+    }
 
-        buttonPanel.add(newGameButton);
-        //TODO Could move to separate method: initGameButton()
+    private void initGameNewButton() {
         newGameButton.addActionListener(e -> {
             newGameButton.setEnabled(false);
             rollButton.setEnabled(true);
@@ -63,20 +89,10 @@ public class App extends JFrame {
             scoreText.setVisible(false);
             newGame();
         });
-        add(buttonPanel);
-
-        scoreText.setText("Score = 0");
-        scoreText.setEnabled(false);
-        add(scoreText);
-
-        pack();
-        setSize(500, 300);
-        setLocationRelativeTo(null);
-        setVisible(true);
     }
 
     private void checkScore() {
-        if (areAllHeld()) {
+        if (isAllHeld()) {
             newGameButton.setEnabled(true);
             rollButton.setEnabled(false);
 
@@ -88,14 +104,13 @@ public class App extends JFrame {
         }
     }
 
-    //TODO Java naming style for methods that return Boolean to use prefix 'is' or 'has', example: hasHeldAllDice()
-    private boolean areAllHeld() {
+    private boolean isAllHeld() {
         return Arrays.stream(dieBoxes).noneMatch(DieBox::isButtonEnabled);
 
     }
 
-    private void updateDiePanelValues(int [] values) {
-        for (int index = 0; index < NUMBER_OF_DICE ; ++index) {
+    private void updateDiePanelValues(int[] values) {
+        for (int index = 0; index < NUMBER_OF_DICE; ++index) {
             dieBoxes[index].setCurrentValue(values[index]);
         }
     }
@@ -116,8 +131,4 @@ public class App extends JFrame {
         game.roll();
         return game.getValues();
     }
-
-    public static void main(String[] args) {
-        new App();
-    }
 }
diff --git a/src/main/java/com/small/dicegame/Die.java b/src/main/java/com/small/dicegame/Die.java
index f055c3e..7db6f55 100644
--- a/src/main/java/com/small/dicegame/Die.java
+++ b/src/main/java/com/small/dicegame/Die.java
@@ -8,16 +8,18 @@ package com.small.dicegame;
 import java.util.Random;
 
 public class Die {
+    private static final Random rnd = new Random();
     private final int numSides;
     private int value;
-    private static final Random rnd = new Random();
 
     public Die() {
         this(6, 6);
     }
+
     public Die(int sides) {
         this(sides, sides);
     }
+
     public Die(int sides, int initValue) {
         numSides = sides;
         value = initValue;
diff --git a/src/main/java/com/small/dicegame/DieBox.java b/src/main/java/com/small/dicegame/DieBox.java
index 39e9387..5d7a534 100644
--- a/src/main/java/com/small/dicegame/DieBox.java
+++ b/src/main/java/com/small/dicegame/DieBox.java
@@ -11,8 +11,8 @@ import java.awt.event.ActionListener;
 
 public class DieBox extends Box {
     private final int index;
-    private int currentValue = 6;
     private final JButton holdButton = new JButton("Hold");
+    private int currentValue = 6;
 
     public DieBox(int index) {
         super(BoxLayout.PAGE_AXIS);
@@ -22,15 +22,14 @@ public class DieBox extends Box {
         this.index = index;
     }
 
+    public boolean isButtonEnabled() {
+        return holdButton.isEnabled();
+    }
 
     public void setButtonEnabled(boolean setValue) {
         holdButton.setEnabled(setValue);
     }
 
-    public boolean isButtonEnabled() {
-        return holdButton.isEnabled();
-    }
-
     public void addActionListener(ActionListener e) {
         holdButton.addActionListener(e);
     }
diff --git a/src/test/java/com/small/dicegame/DiceGameTest.java b/src/test/java/com/small/dicegame/DiceGameTest.java
index fe6de78..b10438d 100644
--- a/src/test/java/com/small/dicegame/DiceGameTest.java
+++ b/src/test/java/com/small/dicegame/DiceGameTest.java
@@ -115,6 +115,7 @@ public class DiceGameTest {
         assertArrayEquals(new Boolean[]{true, true, true, true, true, true}, game.getHolds());
         assertNotNull(game.getScore());
     }
+
     @Test
     public void shouldPlay() {
         DiceGame game = new DiceGame();
@@ -132,7 +133,7 @@ public class DiceGameTest {
         System.out.println(Arrays.toString(game.getValues()));
         System.out.println(game.getScore());
 
-        game.setHolds(0,1,2,3,4,5);
+        game.setHolds(0, 1, 2, 3, 4, 5);
         System.out.println(Arrays.toString(game.getHolds()));
         System.out.println(Arrays.toString(game.getValues()));
         System.out.println(game.getScore());
-- 
GitLab