From 972fcd97a6067093cded11ebd3c376eba56db13c Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Sun, 30 Jan 2022 14:36:15 +0100 Subject: [PATCH] tictactoe: added setter for playfield and changed internal board datatype to char --- .../java/de/tims/tictactoe/GameLogic.java | 16 ++++++++--- .../java/de/tims/tictactoe/GameLogicTest.java | 27 ++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index f63da53..91d3e36 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/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; + } + } diff --git a/src/test/java/de/tims/tictactoe/GameLogicTest.java b/src/test/java/de/tims/tictactoe/GameLogicTest.java index 1c4cf18..6810b9a 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/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 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 testCasesForSetField() { + return Stream.of( + Arguments.of("set field [0][0] for player 1", 0, 0, 'x', new char[][] + {{'x', '-', '-'}, + {'-', '-', '-'}, + {'-', '-', '-'}}) + ); + } }