From c4dcd9e4f1bedb1ebbe1aeadae891e5245cb966c Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Mon, 14 Feb 2022 23:37:53 +0100 Subject: [PATCH] tictactoe: added methods to reset internal board and gui after game over --- .../java/de/tims/tictactoe/GameLogic.java | 20 ++++++++++++++++++- .../java/de/tims/tictactoe/GameLogicTest.java | 14 +++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 66de3c2..7f3099c 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/src/main/java/de/tims/tictactoe/GameLogic.java @@ -114,6 +114,13 @@ public class GameLogic implements ActionListener { this.currentPlayer = this.currentPlayer == PLAYER_1 ? PLAYER_2 : PLAYER_1; } + public void resetBoard() { + for (int i = 0; i < this.board.length; i++) { + for (int j = 0; j < this.board.length; j++) { + this.board[i][j] = '-'; + } + } + } public JPanel generateGUI() { this.fields = new JButton[this.board.length][this.board.length]; @@ -142,7 +149,18 @@ public class GameLogic implements ActionListener { } } JOptionPane.showMessageDialog(contentPanel, "Spieler " + this.currentPlayer + " hat gewonnen."); - System.exit(0); + this.resetGUI(); + } + this.switchPlayer(); + } + + private void resetGUI() { + for (int i = 0; i < this.fields.length; i++) { + for (int j = 0; j < this.fields.length; j++) { + this.resetBoard(); + this.fields[i][j].setText(""); + this.fields[i][j].setEnabled(true); + } } this.switchPlayer(); } diff --git a/src/test/java/de/tims/tictactoe/GameLogicTest.java b/src/test/java/de/tims/tictactoe/GameLogicTest.java index 27575ce..46c8e3c 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/src/test/java/de/tims/tictactoe/GameLogicTest.java @@ -101,6 +101,20 @@ class GameLogicTest { assertEquals(expectedResult, realResult); } + @Test + void resetBoardTest() { + GameLogic game = new GameLogic(SIZE); + game.setField(1, 2, 'x'); + + char[][] expectedResult = new char[][]{{'-', '-', '-'}, + {'-', '-', '-'}, + {'-', '-', '-'}};; + game.resetBoard(); + char[][] realResult = game.getBoard(); + + assertArrayEquals(expectedResult, realResult); + } + @ParameterizedTest(name = "[{index}] {0} -> {2} fields") @MethodSource("testCasesForCountPlayfields") void fieldCountTest(String testName, int size, int expectedResult) {