Browse Source

tictactoe: refactored and formatted whole code

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

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

@ -10,11 +10,13 @@ import javax.swing.JPanel;
public class GameLogic implements ActionListener {
private static final char EMPTY_FIELD = '-';
private static final char PLAYER_1 = 'x';
private static final char PLAYER_2 = 'o';
private char[][] board;
private final char[] occupiedFields = { PLAYER_1, PLAYER_2 };
private char currentPlayer = PLAYER_1;
private boolean gui = false;
private JButton[][] fields;
private JPanel contentPanel;
@ -24,12 +26,7 @@ public class GameLogic implements ActionListener {
size = 3;
}
this.board = new char[size][size];
for (int i = 0; i < this.board.length; i++) {
for (int j = 0; j < this.board[0].length; j++) {
this.setField(i, j, '-');
}
}
this.resetBoard();
}
public GameLogic(char[][] board) {
@ -45,8 +42,13 @@ public class GameLogic implements ActionListener {
}
public void setField(int column, int row, char player) {
if (fieldIsEmpty(column, row))
if (this.fieldIsEmpty(column, row)) {
this.board[column][row] = player;
if (gui) {
this.fields[column][row].setText("" + this.getCurrentPlayer());
this.updateGUI();
}
}
}
public boolean fieldIsEmpty(int column, int row) {
@ -103,7 +105,7 @@ public class GameLogic implements ActionListener {
}
public boolean checkEndOfGame() {
return checkForWin(PLAYER_1) || checkForWin(PLAYER_2);
return this.checkForWin(PLAYER_1) || this.checkForWin(PLAYER_2);
}
public char getCurrentPlayer() {
@ -117,7 +119,7 @@ public class GameLogic implements ActionListener {
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] = '-';
this.board[i][j] = EMPTY_FIELD;
}
}
}
@ -134,6 +136,7 @@ public class GameLogic implements ActionListener {
this.contentPanel.add(this.fields[i][j]);
}
}
this.gui = true;
return this.contentPanel;
}
@ -171,12 +174,10 @@ public class GameLogic implements ActionListener {
for (int j = 0; j < this.fields[0].length; j++) {
if (e.getSource() == this.fields[i][j]) {
this.setField(i, j, currentPlayer);
this.fields[i][j].setText("" + this.getCurrentPlayer());
updateGUI();
this.fields[i][j].getText();
}
}
}
}
}

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

@ -7,7 +7,6 @@ import java.awt.Component;
import java.util.stream.Stream;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.junit.jupiter.api.BeforeAll;
@ -39,9 +38,11 @@ class GameLogicTest {
@Test
void getBoardTest() {
// @formatter:off
char[][] expectedResult = new char[][]{{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}};
// @formatter:on
char[][] realResult = this.game.getBoard();
assertArrayEquals(expectedResult, realResult);
@ -49,9 +50,11 @@ class GameLogicTest {
@Test
void createGameLogicWithGivenBoardTest() {
// @formatter:off
char[][] expectedResult = new char[][]{{'x', '-', '-'},
{'-', 'o', '-'},
{'x', '-', '-'}};
// @formatter:on
char[][] givenBoard = expectedResult;
char[][] realResult = new GameLogic(givenBoard).getBoard();
@ -61,21 +64,22 @@ class GameLogicTest {
@Test
void generateGUITest() {
JPanel expectedResult = new JPanel();
JPanel realResult = game.generateGUI();
JPanel realResult = this.game.generateGUI();
assertEquals(expectedResult.getClass(), realResult.getClass());
}
@Test
void numberOfGUIFieldsTest() {
int realResult = 0;
int expectedResult = (int) Math.pow(SIZE, 2);
int realResult = 0;
JPanel gui = game.generateGUI();
JPanel gui = this.game.generateGUI();
Component[] components = gui.getComponents();
for (Component component : components) {
if (component instanceof JButton) realResult++;
if (component instanceof JButton)
realResult++;
}
assertEquals(expectedResult, realResult);
@ -84,8 +88,8 @@ class GameLogicTest {
@Test
void getCurrentPlayerTest() {
GameLogic game = new GameLogic(SIZE);
char expectedResult = game.getCurrentPlayer();
char realResult = 'x';
char expectedResult = 'x';
char realResult = game.getCurrentPlayer();
assertEquals(expectedResult, realResult);
}
@ -95,8 +99,8 @@ class GameLogicTest {
GameLogic game = new GameLogic(SIZE);
game.switchPlayer();
char expectedResult = game.getCurrentPlayer();
char realResult = 'o';
char expectedResult = 'o';
char realResult = game.getCurrentPlayer();
assertEquals(expectedResult, realResult);
}
@ -105,10 +109,11 @@ class GameLogicTest {
void resetBoardTest() {
GameLogic game = new GameLogic(SIZE);
game.setField(1, 2, 'x');
// @formatter:off
char[][] expectedResult = new char[][]{{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}};;
{'-', '-', '-'}};
// @formatter:on
game.resetBoard();
char[][] realResult = game.getBoard();
@ -160,24 +165,25 @@ class GameLogicTest {
@ParameterizedTest(name = "[{index}] {0}: should be {1}")
@MethodSource("testCasesForCheckButtonState")
void buttonStateTest(String testName, boolean expectedResult, boolean doClick, int column, int row) {
void buttonStateTest(String testName, boolean expectedResult, boolean doClick, int column, int row) throws InterruptedException {
GameLogic game = new GameLogic(SIZE);
game.generateGUI();
JButton currentField = game.getGUIField(column, row);
JButton currentField = game.getGUIField(0, 0);
if (doClick) currentField.doClick();
if (doClick)
currentField.doClick();
boolean realResult = !currentField.getText().isEmpty();
assertEquals(expectedResult, realResult);
}
// @formatter:off
private static Stream<Arguments> testCasesForCountPlayfields() {
return Stream.of(
Arguments.of("1x1 board with too few fields", 1, 9),
return Stream.of(Arguments.of("1x1 board with too few fields", 1, 9),
Arguments.of("2x2 board with too few fields", 2, 9),
Arguments.of("3x3 board with 9 playfields", 3, 9),
Arguments.of("4x4 board with 16 playfields", 4, 16),
Arguments.of("5x5 board with 25 playfields", 5,25)
);
Arguments.of("5x5 board with 25 playfields", 5, 25));
}
private static Stream<Arguments> testCasesForSetField() {
@ -326,5 +332,6 @@ class GameLogicTest {
Arguments.of("dont't trigger gui field [1][1]", false, false, 1, 1)
);
}
// @formatter:on
}
Loading…
Cancel
Save