Browse Source

tictactoe: added methods to reset internal board and gui after game over

tictactoe
Malte Schellhardt 2 years ago
committed by Lorenz Hohmann
parent
commit
c4dcd9e4f1
  1. 20
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 14
      src/test/java/de/tims/tictactoe/GameLogicTest.java

20
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();
}

14
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) {

Loading…
Cancel
Save