From f92df8a1cb6a4695dcfb3761fe8b69efd12d7400 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 13:38:22 +0100 Subject: [PATCH 1/6] matchfield: first test - creation not empty --- .../matchfield/MatchfieldCreation.java | 9 +++++++++ .../matchfield/MatchfieldCreationTest.java | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java create mode 100644 src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java diff --git a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java new file mode 100644 index 0000000..f735fd9 --- /dev/null +++ b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java @@ -0,0 +1,9 @@ +package de.tims.fleetstorm.matchfield; + +public class MatchfieldCreation { + + public int[][] createMatchfield() { + return new int[][] {}; + } + +} diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java new file mode 100644 index 0000000..65e47fa --- /dev/null +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -0,0 +1,16 @@ +package de.tims.fleetstorm.matchfield; + +import static org.junit.Assert.assertNotNull; + +import org.junit.jupiter.api.Test; + +class MatchfieldCreationTest { + + MatchfieldCreation matchfield = new MatchfieldCreation(); + + @Test + void testMatchfieldCreateNotEmpty() { + int[][] calcResult = matchfield.createMatchfield(); + assertNotNull(calcResult); + } +} From 510b3ac953565f94294f30f031555688f0394e3b Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:22:43 +0100 Subject: [PATCH 2/6] matchfield: added field size --- .../matchfield/MatchfieldCreation.java | 16 +++++++++++++-- .../matchfield/MatchfieldCreationTest.java | 20 ++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java index f735fd9..c101ca2 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java @@ -2,8 +2,20 @@ package de.tims.fleetstorm.matchfield; public class MatchfieldCreation { - public int[][] createMatchfield() { - return new int[][] {}; + public int[][] createMatchfield(int size) { + int[][] matchfield = new int[size][size]; + + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + matchfield[i][j] = 1; + } + } + + return matchfield; + } + + public int getSize() { + return 10; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 65e47fa..e285b46 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -1,8 +1,14 @@ package de.tims.fleetstorm.matchfield; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertNotNull; +import java.util.stream.Stream; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class MatchfieldCreationTest { @@ -10,7 +16,19 @@ class MatchfieldCreationTest { @Test void testMatchfieldCreateNotEmpty() { - int[][] calcResult = matchfield.createMatchfield(); + int[][] calcResult = matchfield.createMatchfield(0); assertNotNull(calcResult); } + + @ParameterizedTest(name = "matchfield creation has correct size") + @MethodSource("testMatchfieldSize") + void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) { + matchfield.createMatchfield(size); + int calcResult = matchfield.getSize(); + assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream testMatchfieldSize() { + return Stream.of(Arguments.of("field size 10x10", 10, 10)); + } } From 96939307ae426a5de08190993c5a4ac260133f80 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:28:14 +0100 Subject: [PATCH 3/6] matchfield: added dynamic field size with constructor --- .../matchfield/MatchfieldCreation.java | 17 ++++++++++++----- .../matchfield/MatchfieldCreationTest.java | 11 ++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java index c101ca2..ed3da8c 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java @@ -2,20 +2,27 @@ package de.tims.fleetstorm.matchfield; public class MatchfieldCreation { - public int[][] createMatchfield(int size) { - int[][] matchfield = new int[size][size]; + private int[][] matchfield; + private int size; + + public MatchfieldCreation(int size) { + this.size = size; + } + + public int[][] createMatchfield() { + this.matchfield = new int[this.size][this.size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { - matchfield[i][j] = 1; + this.matchfield[i][j] = 1; } } - return matchfield; + return this.matchfield; } public int getSize() { - return 10; + return this.matchfield.length * this.matchfield[0].length; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index e285b46..79b0047 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -12,23 +12,24 @@ import org.junit.jupiter.params.provider.MethodSource; class MatchfieldCreationTest { - MatchfieldCreation matchfield = new MatchfieldCreation(); - @Test void testMatchfieldCreateNotEmpty() { - int[][] calcResult = matchfield.createMatchfield(0); + MatchfieldCreation matchfield = new MatchfieldCreation(0); + int[][] calcResult = matchfield.createMatchfield(); assertNotNull(calcResult); } @ParameterizedTest(name = "matchfield creation has correct size") @MethodSource("testMatchfieldSize") void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) { - matchfield.createMatchfield(size); + MatchfieldCreation matchfield = new MatchfieldCreation(size); + matchfield.createMatchfield(); + int calcResult = matchfield.getSize(); assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); } static Stream testMatchfieldSize() { - return Stream.of(Arguments.of("field size 10x10", 10, 10)); + return Stream.of(Arguments.of("field size 10x10", 10, 100), Arguments.of("field size 15x15", 15, 225)); } } From 37318e912c6c02d41fca5255c44eb3ecfbd4c367 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:34:16 +0100 Subject: [PATCH 4/6] matchfield: added getState() to get state from one field --- .../fleetstorm/matchfield/MatchfieldCreation.java | 4 ++++ .../matchfield/MatchfieldCreationTest.java | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java index ed3da8c..6ad69b3 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java @@ -25,4 +25,8 @@ public class MatchfieldCreation { return this.matchfield.length * this.matchfield[0].length; } + public int getState(int x, int y) { + return 1; + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 79b0047..37c1ede 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -32,4 +32,18 @@ class MatchfieldCreationTest { static Stream testMatchfieldSize() { return Stream.of(Arguments.of("field size 10x10", 10, 100), Arguments.of("field size 15x15", 15, 225)); } + + @ParameterizedTest(name = "matchfield change field is correct") + @MethodSource("testMatchfieldGetFieldState") + void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) { + MatchfieldCreation matchfield = new MatchfieldCreation(10); + matchfield.createMatchfield(); + + int calcResult = matchfield.getState(x, y); + assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream testMatchfieldGetFieldState() { + return Stream.of(Arguments.of("field x:0 y:0 has initial state", 0, 0, 1)); + } } From 6b8a749c3249dd22e45249ce99525195118ac762 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:37:33 +0100 Subject: [PATCH 5/6] matchfield: added setState() function --- .../matchfield/MatchfieldCreation.java | 6 +++++- .../matchfield/MatchfieldCreationTest.java | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java index 6ad69b3..2f604a6 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java @@ -26,7 +26,11 @@ public class MatchfieldCreation { } public int getState(int x, int y) { - return 1; + return this.matchfield[x][y]; + } + + public void setState(int x, int y, int state) { + this.matchfield[x][y] = state; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 37c1ede..ee790cb 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -33,7 +33,7 @@ class MatchfieldCreationTest { return Stream.of(Arguments.of("field size 10x10", 10, 100), Arguments.of("field size 15x15", 15, 225)); } - @ParameterizedTest(name = "matchfield change field is correct") + @ParameterizedTest(name = "matchfield get field is correct") @MethodSource("testMatchfieldGetFieldState") void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) { MatchfieldCreation matchfield = new MatchfieldCreation(10); @@ -46,4 +46,20 @@ class MatchfieldCreationTest { static Stream testMatchfieldGetFieldState() { return Stream.of(Arguments.of("field x:0 y:0 has initial state", 0, 0, 1)); } + + @ParameterizedTest(name = "matchfield change field is correct") + @MethodSource("testMatchfieldSetStateIsCorrect") + void testMatchfieldGetCorrectState(String testName, int x, int y, int state, int expectedResult) { + MatchfieldCreation matchfield = new MatchfieldCreation(10); + matchfield.createMatchfield(); + + matchfield.setState(x, y, state); + + int calcResult = matchfield.getState(x, y); + assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream testMatchfieldSetStateIsCorrect() { + return Stream.of(Arguments.of("field x:0 y:0 has state 2 after setState()", 0, 0, 2, 2)); + } } From a3c2a467967ee95fd3b36683ec9607b37fb76b88 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:42:15 +0100 Subject: [PATCH 6/6] matchfield: refactored Matchfield class structure --- .../{MatchfieldCreation.java => Matchfield.java} | 13 ++++++++----- .../matchfield/MatchfieldCreationTest.java | 13 +++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) rename src/main/java/de/tims/fleetstorm/matchfield/{MatchfieldCreation.java => Matchfield.java} (72%) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java similarity index 72% rename from src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java rename to src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 2f604a6..45912eb 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -1,20 +1,23 @@ package de.tims.fleetstorm.matchfield; -public class MatchfieldCreation { +public class Matchfield { private int[][] matchfield; private int size; - public MatchfieldCreation(int size) { + public static final int EMPTY = 0; + public static final int SHIP = 1; + public static final int SHOT = 2; + + public Matchfield(int size) { this.size = size; + this.matchfield = new int[this.size][this.size]; } public int[][] createMatchfield() { - this.matchfield = new int[this.size][this.size]; - for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { - this.matchfield[i][j] = 1; + this.matchfield[i][j] = Matchfield.EMPTY; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index ee790cb..168dade 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -14,7 +14,7 @@ class MatchfieldCreationTest { @Test void testMatchfieldCreateNotEmpty() { - MatchfieldCreation matchfield = new MatchfieldCreation(0); + Matchfield matchfield = new Matchfield(0); int[][] calcResult = matchfield.createMatchfield(); assertNotNull(calcResult); } @@ -22,7 +22,7 @@ class MatchfieldCreationTest { @ParameterizedTest(name = "matchfield creation has correct size") @MethodSource("testMatchfieldSize") void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) { - MatchfieldCreation matchfield = new MatchfieldCreation(size); + Matchfield matchfield = new Matchfield(size); matchfield.createMatchfield(); int calcResult = matchfield.getSize(); @@ -36,7 +36,7 @@ class MatchfieldCreationTest { @ParameterizedTest(name = "matchfield get field is correct") @MethodSource("testMatchfieldGetFieldState") void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) { - MatchfieldCreation matchfield = new MatchfieldCreation(10); + Matchfield matchfield = new Matchfield(10); matchfield.createMatchfield(); int calcResult = matchfield.getState(x, y); @@ -44,13 +44,13 @@ class MatchfieldCreationTest { } static Stream testMatchfieldGetFieldState() { - return Stream.of(Arguments.of("field x:0 y:0 has initial state", 0, 0, 1)); + return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Matchfield.EMPTY)); } @ParameterizedTest(name = "matchfield change field is correct") @MethodSource("testMatchfieldSetStateIsCorrect") void testMatchfieldGetCorrectState(String testName, int x, int y, int state, int expectedResult) { - MatchfieldCreation matchfield = new MatchfieldCreation(10); + Matchfield matchfield = new Matchfield(10); matchfield.createMatchfield(); matchfield.setState(x, y, state); @@ -60,6 +60,7 @@ class MatchfieldCreationTest { } static Stream testMatchfieldSetStateIsCorrect() { - return Stream.of(Arguments.of("field x:0 y:0 has state 2 after setState()", 0, 0, 2, 2)); + return Stream.of( + Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Matchfield.SHIP, Matchfield.SHIP)); } }