Browse Source

tictactoe: added setter for playfield and changed internal board datatype to char

tictactoe
Malte Schellhardt 3 years ago
committed by Lorenz Hohmann
parent
commit
972fcd97a6
  1. 16
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 27
      src/test/java/de/tims/tictactoe/GameLogicTest.java

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

@ -2,16 +2,22 @@ package de.tims.tictactoe;
public class GameLogic {
private int[][] board;
private char[][] board;
public GameLogic(int size) {
if (size < 3) {
size = 3;
}
this.board = new int[size][size];
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, '-');
}
}
}
public int[][] getBoard() {
public char[][] getBoard() {
return this.board;
}
@ -19,4 +25,8 @@ public class GameLogic {
return this.board[0].length * this.board.length;
}
public void setField(int row, int column, char player) {
this.board[row][column] = player;
}
}

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

@ -3,6 +3,7 @@ package de.tims.tictactoe;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll;
@ -35,9 +36,11 @@ class GameLogicTest {
@Test
void getBoardTest() {
int[][] expectedResult = new int[3][3];
int[][] realResult = this.game.getBoard();
char[][] expectedResult = new char[][]{{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}};
char[][] realResult = this.game.getBoard();
assertArrayEquals(expectedResult, realResult);
}
@ -50,6 +53,15 @@ class GameLogicTest {
assertEquals(expectedResult, realResult);
}
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("testCasesForSetField")
void setFieldTest(String testName, int row, int column, char player, char[][] expectedResult) {
this.game.setField(row, column, player);
char[][] realResult = this.game.getBoard();
assertArrayEquals(expectedResult, realResult);
}
private static Stream<Arguments> testCasesForCountPlayfields() {
return Stream.of(
Arguments.of("1x1 board with too few fields", 1, 9),
@ -59,5 +71,14 @@ class GameLogicTest {
Arguments.of("5x5 board with 25 playfields", 5,25)
);
}
private static Stream<Arguments> testCasesForSetField() {
return Stream.of(
Arguments.of("set field [0][0] for player 1", 0, 0, 'x', new char[][]
{{'x', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}})
);
}
}
Loading…
Cancel
Save