Browse Source

tictactoe: refactored code to update gui

tictactoe
Malte Schellhardt 3 years ago
committed by Lorenz Hohmann
parent
commit
eaeb7bc48f
  1. 27
      src/main/java/de/tims/tictactoe/GameLogic.java

27
src/main/java/de/tims/tictactoe/GameLogic.java

@ -134,6 +134,19 @@ public class GameLogic implements ActionListener {
return this.fields[column][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 @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
for (int i = 0; i < this.fields.length; i++) { for (int i = 0; i < this.fields.length; i++) {
@ -141,21 +154,11 @@ public class GameLogic implements ActionListener {
if (e.getSource() == this.fields[i][j]) { if (e.getSource() == this.fields[i][j]) {
this.setField(i, j, currentPlayer); this.setField(i, j, currentPlayer);
this.fields[i][j].setText("" + this.getCurrentPlayer()); 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();
} }
} }
} }
} }
} }
Loading…
Cancel
Save