From 9c2d219ccc1ba20c6fddc22c1c602d19d213951d Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Mon, 14 Feb 2022 23:26:24 +0100 Subject: [PATCH] tictactoe: added gui logic --- src/main/java/de/tims/tictactoe/GameLogic.java | 16 +++++++++++++++- .../java/de/tims/tictactoe/GameLogicTest.java | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 1a74151..a206cd7 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/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(); + } } } diff --git a/src/test/java/de/tims/tictactoe/GameLogicTest.java b/src/test/java/de/tims/tictactoe/GameLogicTest.java index de436d0..27575ce 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/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();