diff --git a/dicegame/META-INF/MANIFEST.MF b/dicegame/META-INF/MANIFEST.MF new file mode 100644 index 00000000..f961c940 --- /dev/null +++ b/dicegame/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Class-Path: . +Main-Class: WuerfelspielGUI + diff --git a/dicegame/src/dicegame/logic/DiceInteraction.java b/dicegame/src/dicegame/logic/DiceInteraction.java new file mode 100644 index 00000000..0f57d7a4 --- /dev/null +++ b/dicegame/src/dicegame/logic/DiceInteraction.java @@ -0,0 +1,5 @@ +package dicegame.logic; + +public interface DiceInteraction { + public int changeDiceState(int diceIndex) throws SelectionException; +} diff --git a/dicegame/src/dicegame/logic/Logic.java b/dicegame/src/dicegame/logic/Logic.java new file mode 100644 index 00000000..cbd41089 --- /dev/null +++ b/dicegame/src/dicegame/logic/Logic.java @@ -0,0 +1,276 @@ +package dicegame.logic; + +import java.util.Random; + +public class Logic implements DiceInteraction { + public static final int DICE_NUMBER = 6; + public static final int DEFAULT_PLAYER_NUMBER = 2; + public static final int DEFAULT_ROUNDS = 10; + private static final int DIE_UNFIXED = 0; + private static final int DIE_TEMP_FIXED = 1; + private static final int DIE_FIXED = 2; + private boolean gameIsRunning; + private boolean playerCanThrowDice; + private int currentPlayer; + private int roundNumber; + private int roundPoints; + private int maxRounds = DEFAULT_ROUNDS; + private int[] points = new int[2]; + private String[] names = new String[2]; + private int[] dicePips = new int[6]; + private int[] diceFixed = new int[6]; + private Random random; + + public Logic(Random random) { + this.random = random; + } + + public void startGame() { + this.resetGame(); + this.gameIsRunning = true; + this.playerCanThrowDice = true; + this.roundNumber = 1; + this.currentPlayer = 0; + } + + public String rollDice() { + if (!this.gameIsRunning) { + return "Das Spiel läuft aktuell nicht!"; + } else { + if (this.isAllDiceFixed()) { + this.resetDice(); + } + + for(int i = 0; i < DICE_NUMBER; ++i) { + if (this.diceFixed[i] == DIE_TEMP_FIXED) { + this.diceFixed[i] = DIE_FIXED; + } else if (this.diceFixed[i] == DIE_UNFIXED) { + this.dicePips[i] = this.random.nextInt(6) + 1; + } + } + + if (this.isScoringPipsAvailable()) { + this.playerCanThrowDice = true; + } else { + this.playerCanThrowDice = false; + this.roundPoints = 0; + } + + return null; + } + } + + public boolean isScoringPipsAvailable() { + for(int i = 0; i < DICE_NUMBER; ++i) { + if (this.diceFixed[i] != DIE_FIXED && (this.dicePips[i] == 5 || this.dicePips[i] == 1)) { + return true; + } + } + + return false; + } + + private boolean isAllDiceFixed() { + for(int i = 0; i < DICE_NUMBER; ++i) { + if (this.diceFixed[i] == DIE_UNFIXED) { + return false; + } + } + + return true; + } + + public String checkIfGameCanStart() { + if (this.gameIsRunning) { + return "Das Spiel läuft bereits!"; + } else { + for(int i = 0; i < DEFAULT_PLAYER_NUMBER; ++i) { + if (this.names[i] == null || this.names[i].trim().length() < 2) { + return "Spieler " + (i + 1) + " hat noch keinen Namen!"; + } + } + + return null; + } + } + + public void resetGame() { + this.roundNumber = 0; + this.roundPoints = 0; + this.resetDice(); + this.resetPoints(); + } + + public void finishRound() { + this.resetDice(); + int[] var10000 = this.points; + int var10001 = this.currentPlayer; + var10000[var10001] += this.roundPoints; + this.roundPoints = 0; + this.currentPlayer = ++this.currentPlayer % DEFAULT_PLAYER_NUMBER; + if (this.currentPlayer == 0) { + if (this.roundNumber == this.maxRounds) { + this.gameIsRunning = false; + } else { + ++this.roundNumber; + } + } + + } + + private void resetDice() { + for(int i = 0; i < DICE_NUMBER; ++i) { + this.diceFixed[i] = DIE_UNFIXED; + } + + } + + private void resetPoints() { + for(int i = 0; i < DEFAULT_PLAYER_NUMBER; ++i) { + this.points[i] = 0; + } + + } + + public int getRoundNumber() { + return this.roundNumber; + } + + public int getRoundPoints() { + return this.roundPoints; + } + + public boolean isDieFixed(int dieIndex) { + return this.diceFixed[dieIndex] != DIE_UNFIXED; + } + + public boolean isAtLeastOneDieFixedInCurrentThrow() { + for(int i = 0; i < DICE_NUMBER; ++i) { + if (this.diceFixed[i] == DIE_TEMP_FIXED) { + return true; + } + } + + return false; + } + + private int countTempFixedByPips(int pips) { + int count = 0; + + for(int i = 0; i < DICE_NUMBER; ++i) { + if (this.diceFixed[i] == DIE_TEMP_FIXED && this.dicePips[i] == pips) { + ++count; + } + } + + return count; + } + + public int getDiePips(int dieIndex) { + return this.dicePips[dieIndex]; + } + + public int getMaxRounds() { + return this.maxRounds; + } + + public String setMaxRounds(int maxRounds) { + if (this.gameIsRunning) { + return "Rundenzahl kann nicht während eines Spiels verändert werden!"; + } else { + this.maxRounds = maxRounds; + return null; + } + } + + public int getPoints(int playerIndex) { + return this.points[playerIndex]; + } + + public String getName(int playerIndex) { + return this.names[playerIndex]; + } + + public String setName(int playerIndex, String name) { + if (this.gameIsRunning) { + return "Ein Spielername kann nicht während eines Spiels verändert werden!"; + } else { + this.names[playerIndex] = name; + return null; + } + } + + public boolean isPlayerCanThrowDice() { + return this.playerCanThrowDice; + } + + public boolean isGameIsRunning() { + return this.gameIsRunning; + } + + public int getCurrentPlayer() { + return this.currentPlayer; + } + + @Override + public int changeDiceState(int diceIndex) throws SelectionException { + if (this.diceFixed[diceIndex] == DIE_UNFIXED) { + setDieFixed(diceIndex); + } else { + setDieUnfixed(diceIndex); + } + return roundPoints; + } + + private void setDieFixed(int dieIndex) throws SelectionException { + int c; + if (this.dicePips[dieIndex] == 1) { + c = this.countTempFixedByPips(1); + if (c != 2 && c != 5) { + this.roundPoints += 100; + } else { + this.roundPoints -= 200; + this.roundPoints += 1000; + } + + this.diceFixed[dieIndex] = DIE_TEMP_FIXED; + } else if (this.dicePips[dieIndex] == 5) { + c = this.countTempFixedByPips(5); + if (c != 2 && c != 5) { + this.roundPoints += 50; + } else { + this.roundPoints -= 100; + this.roundPoints += 500; + } + + this.diceFixed[dieIndex] = DIE_TEMP_FIXED; + } else { + throw new SelectionException("Es können nur 1'en und 5'en fixiert werden!"); + } + } + + private void setDieUnfixed(int dieIndex) throws SelectionException { + int c; + if (this.dicePips[dieIndex] == 1) { + c = this.countTempFixedByPips(1); + if (c != 3 && c != 6) { + this.roundPoints -= 100; + } else { + this.roundPoints -= 1000; + this.roundPoints += 200; + } + + this.diceFixed[dieIndex] = DIE_UNFIXED; + } else if (this.dicePips[dieIndex] == 5) { + c = this.countTempFixedByPips(5); + if (c != 3 && c != 6) { + this.roundPoints -= 50; + } else { + this.roundPoints -= 500; + this.roundPoints += 100; + } + + this.diceFixed[dieIndex] = DIE_UNFIXED; + } + } +} diff --git a/dicegame/src/dicegame/logic/SelectionException.java b/dicegame/src/dicegame/logic/SelectionException.java new file mode 100644 index 00000000..3497b259 --- /dev/null +++ b/dicegame/src/dicegame/logic/SelectionException.java @@ -0,0 +1,7 @@ +package dicegame.logic; + +public class SelectionException extends Exception { + SelectionException(String msg) { + super(msg); + } +} diff --git a/dicegame/src/dicegame/view/GUI.java b/dicegame/src/dicegame/view/GUI.java new file mode 100644 index 00000000..ec7aeb9b --- /dev/null +++ b/dicegame/src/dicegame/view/GUI.java @@ -0,0 +1,608 @@ +package dicegame.view; + +import dicegame.logic.SelectionException; +import dicegame.logic.Logic; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.util.Random; +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.JToggleButton; +import javax.swing.SwingUtilities; + +public final class GUI extends JFrame implements ActionListener { + private final Logic logic; + private static final long serialVersionUID = 1L; + private JPanel jContentPane = null; + private JPanel panelHead = null; + private JPanel panelMain = null; + private JTextField textfieldS1Name = null; + private JLabel labelS1Name = null; + private JLabel labelS2Name = null; + private JTextField textfieldS2Name = null; + private JPanel panelSpieler1 = null; + private JPanel panelSpieler2 = null; + private JLabel labelS1Points = null; + private JLabel labelS1PointsValue = null; + private JLabel labelS2Points = null; + private JLabel labelS2PointsValue = null; + private JPanel panelGoButton = null; + private JButton buttonGo = null; + private JPanel panelRound = null; + private JLabel labelRound = null; + private JLabel labelRoundValue = null; + private JPanel panelDice = null; + private JToggleButton toggleDie1 = null; + private JToggleButton toggleDie2 = null; + private JToggleButton toggleDie3 = null; + private JToggleButton toggleDie4 = null; + private JToggleButton toggleDie5 = null; + private JToggleButton toggleDie6 = null; + private JPanel panelActions = null; + private JPanel panelRoundPoints = null; + private JLabel labelRoundPoints = null; + private JLabel labelRoundPointsValue = null; + private JPanel panelActionsButtons = null; + private JButton buttonRoll = null; + private JButton buttonReady = null; + private JPanel panelDiceActions = null; + private JPanel panelStatus = null; + private JLabel labelStatus = null; + + private JPanel getPanelHead() { + if (this.panelHead == null) { + BorderLayout borderLayout3 = new BorderLayout(); + borderLayout3.setHgap(3); + borderLayout3.setVgap(3); + this.labelS2Name = new JLabel(); + this.labelS2Name.setText("Spieler 2 :"); + this.labelS2Name.setFont(new Font("Dialog", 0, 12)); + this.labelS2Name.setToolTipText(""); + this.labelS1Name = new JLabel(); + this.labelS1Name.setText("Spieler 1 :"); + this.labelS1Name.setFont(new Font("Dialog", 0, 12)); + this.panelHead = new JPanel(); + this.panelHead.setLayout(borderLayout3); + this.panelHead.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); + this.panelHead.add(this.getPanelSpieler1(), "West"); + this.panelHead.add(this.getPanelSpieler2(), "East"); + this.panelHead.add(this.getPanelGoButton(), "South"); + } + + return this.panelHead; + } + + private JPanel getPanelMain() { + if (this.panelMain == null) { + BorderLayout borderLayout1 = new BorderLayout(); + borderLayout1.setVgap(5); + GridLayout gridLayout3 = new GridLayout(); + gridLayout3.setRows(3); + gridLayout3.setColumns(1); + this.panelMain = new JPanel(); + this.panelMain.setEnabled(true); + this.panelMain.setBorder(BorderFactory.createEtchedBorder(0)); + this.panelMain.setLayout(borderLayout1); + this.panelMain.add(this.getPanelRound(), "North"); + this.panelMain.add(this.getPanelDiceActions(), "Center"); + } + + return this.panelMain; + } + + private JTextField getTextfieldS1Name() { + if (this.textfieldS1Name == null) { + this.textfieldS1Name = new JTextField(); + this.textfieldS1Name.setColumns(10); + this.textfieldS1Name.setName(""); + this.textfieldS1Name.setToolTipText("Name des ersten Spielers, bestehend aus mindestens 2 Zeichen"); + this.textfieldS1Name.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + GUI.this.textfieldS2Name.grabFocus(); + } + }); + this.textfieldS1Name.addKeyListener(new KeyAdapter() { + public void keyReleased(KeyEvent e) { + GUI.this.logic.setName(0, GUI.this.textfieldS1Name.getText()); + String s = GUI.this.logic.checkIfGameCanStart(); + if (s != null) { + GUI.this.labelStatus.setText(s); + GUI.this.buttonGo.setEnabled(false); + } else { + GUI.this.buttonGo.setEnabled(true); + GUI.this.labelStatus.setText(" "); + } + + } + }); + } + + return this.textfieldS1Name; + } + + private JTextField getTextfieldS2Name() { + if (this.textfieldS2Name == null) { + this.textfieldS2Name = new JTextField(); + this.textfieldS2Name.setColumns(10); + this.textfieldS2Name.setName(""); + this.textfieldS2Name.setToolTipText("Name des zweiten Spielers, bestehend aus mindestens 2 Zeichen"); + this.textfieldS2Name.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + GUI.this.buttonGo.grabFocus(); + } + }); + this.textfieldS2Name.addKeyListener(new KeyAdapter() { + public void keyReleased(KeyEvent e) { + GUI.this.logic.setName(1, GUI.this.textfieldS2Name.getText()); + String s = GUI.this.logic.checkIfGameCanStart(); + if (s != null) { + GUI.this.labelStatus.setText(s); + GUI.this.buttonGo.setEnabled(false); + } else { + GUI.this.buttonGo.setEnabled(true); + GUI.this.labelStatus.setText(" "); + } + + } + }); + } + + return this.textfieldS2Name; + } + + private JPanel getPanelSpieler1() { + if (this.panelSpieler1 == null) { + GridLayout gridLayout21 = new GridLayout(); + gridLayout21.setRows(2); + this.labelS1PointsValue = new JLabel(); + this.labelS1PointsValue.setText("0"); + this.labelS1PointsValue.setFont(new Font("Dialog", 1, 14)); + GridLayout gridLayout1 = new GridLayout(); + gridLayout1.setRows(2); + gridLayout1.setColumns(2); + this.labelS1Points = new JLabel(); + this.labelS1Points.setText("Punkte :"); + this.labelS1Points.setFont(new Font("Dialog", 0, 12)); + GridLayout gridLayout = new GridLayout(); + gridLayout.setRows(2); + gridLayout.setColumns(2); + this.panelSpieler1 = new JPanel(); + this.panelSpieler1.setLayout(gridLayout21); + this.panelSpieler1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); + this.panelSpieler1.add(this.labelS1Name, (Object)null); + this.panelSpieler1.add(this.getTextfieldS1Name(), (Object)null); + this.panelSpieler1.add(this.labelS1Points, (Object)null); + this.panelSpieler1.add(this.labelS1PointsValue, (Object)null); + } + + return this.panelSpieler1; + } + + private JPanel getPanelSpieler2() { + if (this.panelSpieler2 == null) { + this.labelS2PointsValue = new JLabel(); + this.labelS2PointsValue.setText("0"); + this.labelS2PointsValue.setFont(new Font("Dialog", 1, 14)); + this.labelS2Points = new JLabel(); + this.labelS2Points.setText("Punkte :"); + this.labelS2Points.setFont(new Font("Dialog", 0, 12)); + GridLayout gridLayout2 = new GridLayout(); + gridLayout2.setRows(2); + gridLayout2.setColumns(2); + this.panelSpieler2 = new JPanel(); + this.panelSpieler2.setLayout(gridLayout2); + this.panelSpieler2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); + this.panelSpieler2.add(this.labelS2Name, (Object)null); + this.panelSpieler2.add(this.getTextfieldS2Name(), (Object)null); + this.panelSpieler2.add(this.labelS2Points, (Object)null); + this.panelSpieler2.add(this.labelS2PointsValue, (Object)null); + } + + return this.panelSpieler2; + } + + private JPanel getPanelGoButton() { + if (this.panelGoButton == null) { + this.panelGoButton = new JPanel(); + this.panelGoButton.setLayout(new GridBagLayout()); + this.panelGoButton.setEnabled(true); + this.panelGoButton.add(this.getButtonGo(), new GridBagConstraints()); + } + + return this.panelGoButton; + } + + private JButton getButtonGo() { + if (this.buttonGo == null) { + this.buttonGo = new JButton(); + this.buttonGo.setName(""); + this.buttonGo.setEnabled(false); + this.buttonGo.setText(" Spiel starten "); + this.buttonGo.addActionListener(this); + } + + return this.buttonGo; + } + + private JPanel getPanelRound() { + if (this.panelRound == null) { + this.labelRoundValue = new JLabel(); + this.labelRoundValue.setText("0"); + this.labelRoundValue.setFont(new Font("Dialog", 0, 12)); + this.labelRound = new JLabel(); + this.labelRound.setText("Runde "); + this.labelRound.setFont(new Font("Dialog", 0, 12)); + this.panelRound = new JPanel(); + this.panelRound.setLayout(new FlowLayout()); + this.panelRound.add(this.labelRound, (Object)null); + this.panelRound.add(this.labelRoundValue, (Object)null); + } + + return this.panelRound; + } + + private JPanel getPanelDice() { + if (this.panelDice == null) { + GridLayout gridLayout5 = new GridLayout(); + gridLayout5.setRows(1); + gridLayout5.setHgap(20); + gridLayout5.setVgap(1); + gridLayout5.setColumns(6); + this.panelDice = new JPanel(); + this.panelDice.setPreferredSize(new Dimension(346, 26)); + this.panelDice.setBorder(BorderFactory.createEmptyBorder(5, 3, 5, 0)); + this.panelDice.setLayout(gridLayout5); + this.panelDice.add(this.getToggleDie1(), (Object)null); + this.panelDice.add(this.getToggleDie2(), (Object)null); + this.panelDice.add(this.getToggleDie3(), (Object)null); + this.panelDice.add(this.getToggleDie4(), (Object)null); + this.panelDice.add(this.getToggleDie5(), (Object)null); + this.panelDice.add(this.getToggleDie6(), (Object)null); + } + + return this.panelDice; + } + + private JToggleButton getToggleDie1() { + if (this.toggleDie1 == null) { + this.toggleDie1 = new JToggleButton(); + this.toggleDie1.setText("1"); + this.toggleDie1.setEnabled(false); + this.toggleDie1.addActionListener(this); + } + + return this.toggleDie1; + } + + private JToggleButton getToggleDie2() { + if (this.toggleDie2 == null) { + this.toggleDie2 = new JToggleButton(); + this.toggleDie2.setText("2"); + this.toggleDie2.setEnabled(false); + this.toggleDie2.addActionListener(this); + } + + return this.toggleDie2; + } + + private JToggleButton getToggleDie3() { + if (this.toggleDie3 == null) { + this.toggleDie3 = new JToggleButton(); + this.toggleDie3.setText("3"); + this.toggleDie3.setEnabled(false); + this.toggleDie3.addActionListener(this); + } + + return this.toggleDie3; + } + + private JToggleButton getToggleDie4() { + if (this.toggleDie4 == null) { + this.toggleDie4 = new JToggleButton(); + this.toggleDie4.setText("4"); + this.toggleDie4.setEnabled(false); + this.toggleDie4.addActionListener(this); + } + + return this.toggleDie4; + } + + private JToggleButton getToggleDie5() { + if (this.toggleDie5 == null) { + this.toggleDie5 = new JToggleButton(); + this.toggleDie5.setText("5"); + this.toggleDie5.setEnabled(false); + this.toggleDie5.addActionListener(this); + } + + return this.toggleDie5; + } + + private JToggleButton getToggleDie6() { + if (this.toggleDie6 == null) { + this.toggleDie6 = new JToggleButton(); + this.toggleDie6.setText("6"); + this.toggleDie6.setEnabled(false); + this.toggleDie6.addActionListener(this); + } + + return this.toggleDie6; + } + + private JPanel getPanelActions() { + if (this.panelActions == null) { + BorderLayout borderLayout2 = new BorderLayout(); + borderLayout2.setVgap(0); + borderLayout2.setHgap(0); + this.panelActions = new JPanel(); + this.panelActions.setLayout(borderLayout2); + this.panelActions.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); + this.panelActions.add(this.getPanelRoundPoints(), "West"); + this.panelActions.add(this.getPanelActionsButtons(), "East"); + } + + return this.panelActions; + } + + private JPanel getPanelRoundPoints() { + if (this.panelRoundPoints == null) { + this.labelRoundPointsValue = new JLabel(); + this.labelRoundPointsValue.setText("0"); + this.labelRoundPoints = new JLabel(); + this.labelRoundPoints.setText("Punkte des aktuellen Zuges : "); + this.labelRoundPoints.setFont(new Font("Dialog", 0, 12)); + this.panelRoundPoints = new JPanel(); + this.panelRoundPoints.setLayout(new GridBagLayout()); + this.panelRoundPoints.add(this.labelRoundPoints, new GridBagConstraints()); + this.panelRoundPoints.add(this.labelRoundPointsValue, new GridBagConstraints()); + } + + return this.panelRoundPoints; + } + + private JPanel getPanelActionsButtons() { + if (this.panelActionsButtons == null) { + GridLayout gridLayout6 = new GridLayout(); + gridLayout6.setRows(2); + gridLayout6.setHgap(0); + gridLayout6.setVgap(6); + gridLayout6.setColumns(1); + this.panelActionsButtons = new JPanel(); + this.panelActionsButtons.setLayout(gridLayout6); + this.panelActionsButtons.add(this.getButtonRoll(), (Object)null); + this.panelActionsButtons.add(this.getButtonReady(), (Object)null); + } + + return this.panelActionsButtons; + } + + private JButton getButtonRoll() { + if (this.buttonRoll == null) { + this.buttonRoll = new JButton(); + this.buttonRoll.setText("Würfeln"); + this.buttonRoll.setEnabled(false); + this.buttonRoll.addActionListener(this); + } + + return this.buttonRoll; + } + + private JButton getButtonReady() { + if (this.buttonReady == null) { + this.buttonReady = new JButton(); + this.buttonReady.setText("Zug beenden"); + this.buttonReady.setEnabled(false); + this.buttonReady.addActionListener(this); + } + + return this.buttonReady; + } + + private JPanel getPanelDiceActions() { + if (this.panelDiceActions == null) { + BorderLayout borderLayout = new BorderLayout(); + borderLayout.setHgap(5); + borderLayout.setVgap(5); + this.panelDiceActions = new JPanel(); + this.panelDiceActions.setLayout(borderLayout); + this.panelDiceActions.add(this.getPanelDice(), "Center"); + this.panelDiceActions.add(this.getPanelActions(), "South"); + } + + return this.panelDiceActions; + } + + private JPanel getPanelStatus() { + if (this.panelStatus == null) { + this.labelStatus = new JLabel(); + this.labelStatus.setText(" "); + this.labelStatus.setToolTipText(""); + this.labelStatus.setFont(new Font("Arial", 0, 12)); + this.panelStatus = new JPanel(); + this.panelStatus.setLayout(new BorderLayout()); + this.panelStatus.setBorder(BorderFactory.createBevelBorder(1)); + this.panelStatus.add(this.labelStatus, "North"); + } + + return this.panelStatus; + } + + public GUI() { + this.initialize(); + this.logic = new Logic(new Random()); + this.logic.resetGame(); + } + + private void initialize() { + this.setSize(478, 311); + this.setResizable(false); + this.setDefaultCloseOperation(3); + this.setName("mainFrame"); + this.setContentPane(this.getJContentPane()); + this.setTitle("Dings"); + } + + private JPanel getJContentPane() { + if (this.jContentPane == null) { + BorderLayout borderLayout4 = new BorderLayout(); + borderLayout4.setHgap(3); + borderLayout4.setVgap(2); + this.jContentPane = new JPanel(); + this.jContentPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2)); + this.jContentPane.setLayout(borderLayout4); + this.jContentPane.add(this.getPanelHead(), "North"); + this.jContentPane.add(this.getPanelMain(), "Center"); + this.jContentPane.add(this.getPanelStatus(), "South"); + } + + return this.jContentPane; + } + + public void actionPerformed(ActionEvent e) { + Object source = e.getSource(); + if (source != this.textfieldS1Name && source != this.textfieldS2Name) { + if (source == this.buttonGo) { + this.logic.startGame(); + this.labelStatus.setText(" "); + this.buttonGo.setEnabled(false); + this.textfieldS1Name.setEditable(false); + this.textfieldS2Name.setEditable(false); + this.buttonReady.setEnabled(true); + this.labelS2PointsValue.setText(String.valueOf(this.logic.getPoints(1))); + this.labelS1PointsValue.setText(String.valueOf(this.logic.getPoints(0))); + this.syncRoundInfoToGui(); + this.logic.rollDice(); + this.syncLogicDiceToGui(); + this.toggleDie1.setEnabled(true); + this.toggleDie2.setEnabled(true); + this.toggleDie3.setEnabled(true); + this.toggleDie4.setEnabled(true); + this.toggleDie5.setEnabled(true); + this.toggleDie6.setEnabled(true); + } else if (source == this.buttonReady) { + this.logic.finishRound(); + this.syncRoundInfoToGui(); + if (this.logic.isGameIsRunning()) { + this.logic.rollDice(); + this.syncLogicDiceToGui(); + } + } else if (source == this.buttonRoll) { + if (!this.logic.isAtLeastOneDieFixedInCurrentThrow()) { + this.labelStatus.setText("Wenigstens ein Würfel muss fixiert sein."); + } else { + String s = this.logic.rollDice(); + if (s != null) { + this.labelStatus.setText(s); + } else { + this.syncLogicDiceToGui(); + } + } + } else if (source == this.toggleDie1) { + this.toggleDie(this.toggleDie1, 0); + } else if (source == this.toggleDie2) { + this.toggleDie(this.toggleDie2, 1); + } else if (source == this.toggleDie3) { + this.toggleDie(this.toggleDie3, 2); + } else if (source == this.toggleDie4) { + this.toggleDie(this.toggleDie4, 3); + } else if (source == this.toggleDie5) { + this.toggleDie(this.toggleDie5, 4); + } else if (source == this.toggleDie6) { + this.toggleDie(this.toggleDie6, 5); + } + } + + } + + private void toggleDie(JToggleButton die, int dieIndex) { + String result = null; + this.labelStatus.setText(" "); + try { + int resultingPoints = this.logic.changeDiceState(dieIndex); + this.labelRoundPointsValue.setText(String.valueOf(resultingPoints)); + } catch (SelectionException e) { + die.setSelected(!die.isSelected()); + this.labelStatus.setText(e.getMessage()); + } + } + + private void syncRoundInfoToGui() { + if (this.logic.isGameIsRunning()) { + this.labelRoundValue.setText(String.valueOf(this.logic.getRoundNumber())); + this.labelRoundPointsValue.setText(String.valueOf(this.logic.getRoundPoints())); + if (this.logic.getCurrentPlayer() == 0) { + this.labelS1Name.setForeground(Color.GREEN); + this.labelS2Name.setForeground(Color.BLACK); + this.labelS2PointsValue.setText(String.valueOf(this.logic.getPoints(1))); + } else { + this.labelS2Name.setForeground(Color.GREEN); + this.labelS1Name.setForeground(Color.BLACK); + this.labelS1PointsValue.setText(String.valueOf(this.logic.getPoints(0))); + } + } else { + this.labelS2Name.setForeground(Color.BLACK); + this.labelS1Name.setForeground(Color.BLACK); + this.textfieldS1Name.setEditable(true); + this.textfieldS2Name.setEditable(true); + this.labelStatus.setText("Das Spiel ist beendet!"); + this.buttonReady.setEnabled(false); + this.buttonRoll.setEnabled(false); + this.buttonGo.setEnabled(true); + this.toggleDie1.setEnabled(false); + this.toggleDie2.setEnabled(false); + this.toggleDie3.setEnabled(false); + this.toggleDie4.setEnabled(false); + this.toggleDie5.setEnabled(false); + this.toggleDie6.setEnabled(false); + } + + } + + private void syncLogicDiceToGui() { + this.syncLogicDieToGui(this.toggleDie1, 0); + this.syncLogicDieToGui(this.toggleDie2, 1); + this.syncLogicDieToGui(this.toggleDie3, 2); + this.syncLogicDieToGui(this.toggleDie4, 3); + this.syncLogicDieToGui(this.toggleDie5, 4); + this.syncLogicDieToGui(this.toggleDie6, 5); + this.buttonRoll.setEnabled(this.logic.isPlayerCanThrowDice()); + this.labelRoundPointsValue.setText(String.valueOf(this.logic.getRoundPoints())); + } + + private void syncLogicDieToGui(JToggleButton die, int dieIndex) { + die.setText(String.valueOf(this.logic.getDiePips(dieIndex))); + if (this.logic.isDieFixed(dieIndex)) { + die.setForeground(Color.BLUE); + die.setSelected(true); + } else { + die.setForeground(Color.BLACK); + die.setSelected(false); + } + + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + GUI thisClass = new GUI(); + thisClass.setDefaultCloseOperation(3); + thisClass.setVisible(true); + } + }); + } +}