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

25
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.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
@ -35,8 +36,10 @@ class GameLogicTest {
@Test @Test
void getBoardTest() { 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); assertArrayEquals(expectedResult, realResult);
} }
@ -50,6 +53,15 @@ class GameLogicTest {
assertEquals(expectedResult, realResult); 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() { private static Stream<Arguments> testCasesForCountPlayfields() {
return Stream.of( return Stream.of(
Arguments.of("1x1 board with too few fields", 1, 9), Arguments.of("1x1 board with too few fields", 1, 9),
@ -60,4 +72,13 @@ class GameLogicTest {
); );
} }
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