From 6d235a9763d8b6212ef741403aad48d8d48c4e34 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Mon, 14 Feb 2022 23:18:56 +0100 Subject: [PATCH] tictactoe: added method to switch player --- src/main/java/de/tims/tictactoe/GameLogic.java | 10 ++++++++-- src/test/java/de/tims/tictactoe/GameLogicTest.java | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 4c464fa..1a74151 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/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 char[][] board; private final char[] occupiedFields = { PLAYER_1, PLAYER_2 }; + private char currentPlayer = PLAYER_1; private JButton[][] fields; private JPanel contentPanel; @@ -105,8 +106,13 @@ public class GameLogic implements ActionListener { } public char getCurrentPlayer() { - return 'x'; + return this.currentPlayer; } + + public void switchPlayer() { + this.currentPlayer = this.currentPlayer == PLAYER_1 ? PLAYER_2 : PLAYER_1; + } + public JPanel generateGUI() { this.fields = new JButton[this.board.length][this.board.length]; @@ -137,5 +143,5 @@ public class GameLogic implements ActionListener { } } } - + } diff --git a/src/test/java/de/tims/tictactoe/GameLogicTest.java b/src/test/java/de/tims/tictactoe/GameLogicTest.java index f939cff..de436d0 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/src/test/java/de/tims/tictactoe/GameLogicTest.java @@ -89,6 +89,16 @@ class GameLogicTest { assertEquals(expectedResult, realResult); } + @Test + void switchPlayerTest() { + game.switchPlayer(); + + char expectedResult = game.getCurrentPlayer(); + char realResult = 'o'; + + assertEquals(expectedResult, realResult); + } + @ParameterizedTest(name = "[{index}] {0} -> {2} fields") @MethodSource("testCasesForCountPlayfields") void fieldCountTest(String testName, int size, int expectedResult) {