Browse Source

tictactoe: added method to switch player

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

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

@ -13,6 +13,7 @@ public class GameLogic implements ActionListener {
private static final char PLAYER_2 = 'o'; private static final char PLAYER_2 = 'o';
private char[][] board; private char[][] board;
private final char[] occupiedFields = { PLAYER_1, PLAYER_2 }; private final char[] occupiedFields = { PLAYER_1, PLAYER_2 };
private char currentPlayer = PLAYER_1;
private JButton[][] fields; private JButton[][] fields;
private JPanel contentPanel; private JPanel contentPanel;
@ -105,8 +106,13 @@ public class GameLogic implements ActionListener {
} }
public char getCurrentPlayer() { public char getCurrentPlayer() {
return 'x';
return this.currentPlayer;
} }
public void switchPlayer() {
this.currentPlayer = this.currentPlayer == PLAYER_1 ? PLAYER_2 : PLAYER_1;
}
public JPanel generateGUI() { public JPanel generateGUI() {
this.fields = new JButton[this.board.length][this.board.length]; this.fields = new JButton[this.board.length][this.board.length];
@ -137,5 +143,5 @@ public class GameLogic implements ActionListener {
} }
} }
} }
} }

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

@ -89,6 +89,16 @@ class GameLogicTest {
assertEquals(expectedResult, realResult); assertEquals(expectedResult, realResult);
} }
@Test
void switchPlayerTest() {
game.switchPlayer();
char expectedResult = game.getCurrentPlayer();
char realResult = 'o';
assertEquals(expectedResult, realResult);
}
@ParameterizedTest(name = "[{index}] {0} -> {2} fields") @ParameterizedTest(name = "[{index}] {0} -> {2} fields")
@MethodSource("testCasesForCountPlayfields") @MethodSource("testCasesForCountPlayfields")
void fieldCountTest(String testName, int size, int expectedResult) { void fieldCountTest(String testName, int size, int expectedResult) {

Loading…
Cancel
Save