From eaeb7bc48f1bdef90d4615baa8b277d857121185 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Mon, 14 Feb 2022 23:30:15 +0100 Subject: [PATCH] tictactoe: refactored code to update gui --- .../java/de/tims/tictactoe/GameLogic.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index a206cd7..66de3c2 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/src/main/java/de/tims/tictactoe/GameLogic.java @@ -133,29 +133,32 @@ public class GameLogic implements ActionListener { public JButton getGUIField(int column, int row) { return this.fields[column][row]; } + + private void updateGUI() { + if (this.checkEndOfGame()) { + for (int i = 0; i < this.fields.length; i++) { + for (int j = 0; j < this.fields.length; j++) { + this.fields[i][j].setEnabled(false); + } + } + JOptionPane.showMessageDialog(contentPanel, "Spieler " + this.currentPlayer + " hat gewonnen."); + System.exit(0); + } + this.switchPlayer(); + } @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < this.fields.length; i++) { for (int j = 0; j < this.fields[0].length; j++) { - if (e.getSource() == this.fields[i][j]) { + if (e.getSource() == this.fields[i][j]) { this.setField(i, j, currentPlayer); this.fields[i][j].setText("" + this.getCurrentPlayer()); - - if (this.checkEndOfGame()) { - JOptionPane.showMessageDialog(contentPanel, "Spieler " + this.currentPlayer + " hat gewonnen."); - for (int k = 0; k < this.fields.length; k++) { - for (int l = 0; l < this.fields.length; l++) { - this.fields[k][l].setEnabled(false); - } - } - System.exit(0); - } - this.switchPlayer(); - + updateGUI(); } } } } + }