Browse Source

tictactoe: refactored code to update gui

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

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

@ -133,29 +133,32 @@ public class GameLogic implements ActionListener {
public JButton getGUIField(int column, int row) { public JButton getGUIField(int column, int row) {
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++) {
for (int j = 0; j < this.fields[0].length; j++) { 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.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