Browse Source

tictactoe: added gui logic

tictactoe
Malte Schellhardt 3 years ago
committed by Lorenz Hohmann
parent
commit
9c2d219ccc
  1. 16
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 2
      src/test/java/de/tims/tictactoe/GameLogicTest.java

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

@ -5,6 +5,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class GameLogic implements ActionListener {
@ -138,7 +139,20 @@ public class GameLogic implements ActionListener {
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]) {
this.fields[i][j].setText("clicked");
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();
}
}
}

2
src/test/java/de/tims/tictactoe/GameLogicTest.java

@ -83,6 +83,7 @@ class GameLogicTest {
@Test
void getCurrentPlayerTest() {
GameLogic game = new GameLogic(SIZE);
char expectedResult = game.getCurrentPlayer();
char realResult = 'x';
@ -91,6 +92,7 @@ class GameLogicTest {
@Test
void switchPlayerTest() {
GameLogic game = new GameLogic(SIZE);
game.switchPlayer();
char expectedResult = game.getCurrentPlayer();

Loading…
Cancel
Save