diff --git a/.idea/copyright/copyright.xml b/.idea/copyright/copyright.xml
new file mode 100644
index 0000000000000000000000000000000000000000..d5ba28999d3c2aa2a9253a219ae5d608336fdce5
--- /dev/null
+++ b/.idea/copyright/copyright.xml
@@ -0,0 +1,6 @@
+<component name="CopyrightManager">
+ <copyright>
+ <option name="notice" value="Copyright (c) &#36;today.year. Bradley M. Small All rights reserved." />
+ <option name="myName" value="copyright" />
+ </copyright>
+</component>
\ No newline at end of file
diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml
new file mode 100644
index 0000000000000000000000000000000000000000..91283a0044b75625b0e6342045e2adcc5369ced8
--- /dev/null
+++ b/.idea/copyright/profiles_settings.xml
@@ -0,0 +1,7 @@
+<component name="CopyrightManager">
+ <settings default="copyright">
+ <module2copyright>
+ <element module="All" copyright="copyright" />
+ </module2copyright>
+ </settings>
+</component>
\ No newline at end of file
diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000000000000000000000000000000000000..94a25f7f4cb416c083d265558da75d457237d671
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="VcsDirectoryMappings">
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
+ </component>
+</project>
\ No newline at end of file
diff --git a/src/main/java/com/small/dicegame/App.java b/src/main/java/com/small/dicegame/App.java
index dd70666b19e52717f241f7d58ca78c3b203bc90e..6b53e05f755c17475f8ed3a0a0225a025fef75ed 100644
--- a/src/main/java/com/small/dicegame/App.java
+++ b/src/main/java/com/small/dicegame/App.java
@@ -6,63 +6,45 @@
package com.small.dicegame;
import javax.swing.*;
-import java.awt.*;
import java.util.Arrays;
-import java.util.stream.IntStream;
public class App extends JFrame {
public static final int NUMBER_OF_DICE = 6;
- private final DiePanel[] diePanels = new DiePanel[NUMBER_OF_DICE];
- private final JButton[] holdButtons = new JButton[NUMBER_OF_DICE];
+ 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 transient DiceGame game = new DiceGame();
+
App() {
initGui();
}
private void initGui() {
+ setTitle("24 Dice Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
- JPanel panel = new JPanel();
- panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
-
- IntStream.range(0, NUMBER_OF_DICE).forEach(index -> {
- diePanels[index] = new DiePanel();
- diePanels[index].setLayout(new BoxLayout(diePanels[index], BoxLayout.PAGE_AXIS));
- diePanels[index].setCurrentValue(6);
- diePanels[index].add(Box.createRigidArea(new Dimension(0, 70)));
- holdButtons[index] = new JButton("Hold");
- holdButtons[index].setEnabled(false);
- holdButtons[index].addActionListener(e -> {
+ Box diceBox = Box.createHorizontalBox();
+ for (int index = 0, dieBoxesLength = dieBoxes.length; index < dieBoxesLength; index++) {
+ dieBoxes[index] = new DieBox(index);
+ dieBoxes[index].addActionListener(e -> {
JButton holdButton = (JButton) e.getSource();
holdButton.setEnabled(false);
- hold(getHoldButtonIndex(holdButton));
- if (areAllHeld()) {
- newGameButton.setEnabled(true);
- rollButton.setEnabled(false);
-
- scoreText.setText("Your score = " + score());
- scoreText.setEnabled(true);
- scoreText.setVisible(true);
- } else {
- rollButton.setEnabled(true);
- }
- });
- diePanels[index].add(holdButtons[index]);
- panel.add(diePanels[index]);
- });
+ DieBox dieBox = (DieBox) holdButton.getParent();
- add(panel);
+ hold(dieBox.getIndex());
+ checkScore();
+ });
+ diceBox.add(dieBoxes[index]);
+ }
+ add(diceBox);
JPanel buttonPanel = new JPanel();
-
rollButton.setEnabled(false);
rollButton.addActionListener(e -> {
if (areAllHeld()) {
- for (JButton b : holdButtons) b.setEnabled(true);
+ for (DieBox d : dieBoxes) d.setButtonEnabled(true);
}
rollButton.setEnabled(false);
updateDiePanelValues(roll());
@@ -77,34 +59,40 @@ 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()) {
+ newGameButton.setEnabled(true);
+ rollButton.setEnabled(false);
+
+ scoreText.setText("Your score = " + score());
+ scoreText.setEnabled(true);
+ scoreText.setVisible(true);
+ } else {
+ rollButton.setEnabled(true);
+ }
+ }
+
private boolean areAllHeld() {
- return Arrays.stream(holdButtons).noneMatch(JButton::isEnabled);
+ return Arrays.stream(dieBoxes).noneMatch(DieBox::isButtonEnabled);
+
}
private void updateDiePanelValues(int [] values) {
for (int index = 0; index < NUMBER_OF_DICE ; ++index) {
- diePanels[index].setCurrentValue(values[index]);
- }
- }
-
- private int getHoldButtonIndex (JButton b) {
- for (int holdIndex = 0; holdIndex < NUMBER_OF_DICE; ++holdIndex) {
- if (b == holdButtons[holdIndex]) {
- return holdIndex;
- }
+ dieBoxes[index].setCurrentValue(values[index]);
}
- return -1;
}
private void newGame() {
diff --git a/src/main/java/com/small/dicegame/DiePanel.java b/src/main/java/com/small/dicegame/DieBox.java
similarity index 71%
rename from src/main/java/com/small/dicegame/DiePanel.java
rename to src/main/java/com/small/dicegame/DieBox.java
index 40f1c45add9381e38040d70f12b7f54cd4ba91b2..dc5905c637444d454fba4347130bdbf3589b481d 100644
--- a/src/main/java/com/small/dicegame/DiePanel.java
+++ b/src/main/java/com/small/dicegame/DieBox.java
@@ -7,14 +7,33 @@ package com.small.dicegame;
import javax.swing.*;
import java.awt.*;
+import java.awt.event.ActionListener;
-public class DiePanel extends JPanel {
- private int currentValue = 0;
+public class DieBox extends Box {
+ private final int index;
+ private int currentValue = 6;
+ private JButton holdButton = new JButton("Hold");
- DiePanel() {
- super();
+ public DieBox(int index) {
+ super(BoxLayout.PAGE_AXIS);
+ add(Box.createRigidArea(new Dimension(0, 70)));
+ add(holdButton);
+ holdButton.setEnabled(false);
+ this.index = index;
}
+
+ public void setButtonEnabled(boolean setValue) {
+ holdButton.setEnabled(setValue);
+ }
+
+ public boolean isButtonEnabled() {
+ return holdButton.isEnabled();
+ }
+
+ public void addActionListener(ActionListener e) {
+ holdButton.addActionListener(e);
+ }
@Override
protected void paintComponent(Graphics g) {
@@ -68,4 +87,8 @@ public class DiePanel extends JPanel {
this.currentValue = currentValue;
repaint();
}
+
+ public int getIndex() {
+ return index;
+ }
}