From bdb135116c43c004e0456909cd38a12ad3f1577f Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 13:38:22 +0100 Subject: [PATCH 01/93] 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 08ba5092ce911432afe9f0238f2381dd90b5c5c4 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:22:43 +0100 Subject: [PATCH 02/93] 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 2d36b5d7b157b206e37bda3be71f1b3f218dbdb9 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:28:14 +0100 Subject: [PATCH 03/93] 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 b060f385aa8ed9a010caf4ab60122a2ff133f0d2 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:34:16 +0100 Subject: [PATCH 04/93] 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 6e5e212c3e6c299cb2a09a8f65f4b7db2724b519 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:37:33 +0100 Subject: [PATCH 05/93] 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 f6d4e8a182a9b2b3585cf460a922c4c024e88896 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:42:15 +0100 Subject: [PATCH 06/93] 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)); } } From 1a2bd513943c409c0976ce90d08fa28737a9e264 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 11:14:41 +0100 Subject: [PATCH 07/93] choosenField returns only Fields with state != shot --- .../java/de/tims/fleetstorm/ai/Logic.java | 19 +++++++++++++-- .../java/de/tims/fleetstorm/ai/LogicTest.java | 23 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 359fcbd..d918ed5 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -1,9 +1,24 @@ package de.tims.fleetstorm.ai; +import java.util.ArrayList; +import java.util.Random; + +import de.tims.fleetstorm.matchfield.Matchfield; + public class Logic { - public int[] chooseField() { - return new int[] {}; + public int[] chooseField(Matchfield matchfield) { + ArrayList possibleFields = new ArrayList(); + for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { + for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { + if (matchfield.getState(x, y) == Matchfield.EMPTY || matchfield.getState(x, y) == Matchfield.SHIP) { + possibleFields.add(new int[] { x, y }); + } + } + } + Random randy = new Random(); + + return possibleFields.get(randy.nextInt(possibleFields.size() - 1)); } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 76fe7f2..d8ebcf5 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -1,16 +1,35 @@ package de.tims.fleetstorm.ai; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; +import de.tims.fleetstorm.matchfield.Matchfield; + class LogicTest { Logic logic = new Logic(); + int size = 3; + private Matchfield matchfield = new Matchfield(size); @Test - void testFielIsNotNull() { - int[] calcResult = logic.chooseField(); + void testFieldIsNotNull() { + int[] calcResult = logic.chooseField(matchfield); assertNotNull(calcResult); } + @Test + void testChoosenFieldHasNotStateShot() { + matchfield.createMatchfield(); + for (int x = 0; x < size; x++) { + for (int y = 0; y < size; y++) { + // matchfield.setState(x, y, Matchfield.SHOT); + } + } + // matchfield.setState(0, 0, Matchfield.EMPTY); + + int[] choosenField = logic.chooseField(matchfield); + int calcState = matchfield.getState(choosenField[0], choosenField[1]); + assertNotEquals(calcState, Matchfield.SHOT); + } } From b69624400045c9f5f7a5bc8799a30890fd3aaf4c Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 13:13:32 +0100 Subject: [PATCH 08/93] Coodrdinates returns correct Values for X and Y --- .../fleetstorm/matchfield/Coordinate.java | 20 +++++++++++++++++++ .../fleetstorm/matchfield/CoordinateTest.java | 20 +++++++++++++++++++ .../matchfield/MatchfieldCreationTest.java | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java create mode 100644 src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java new file mode 100644 index 0000000..a084376 --- /dev/null +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -0,0 +1,20 @@ +package de.tims.fleetstorm.matchfield; + +public class Coordinate { + private int x; + private int y; + + public Coordinate(int x, int y) { + this.x = x; + this.y = y; + } + + public Integer getX() { + return x; + } + + public Integer getY() { + return y; + } + +} diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java new file mode 100644 index 0000000..75b87af --- /dev/null +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -0,0 +1,20 @@ +package de.tims.fleetstorm.matchfield; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class CoordinateTest { + + int x = 1; + int y = 3; + + Coordinate coordinate = new Coordinate(x, y); + + @Test + void testGetCorrectValuesForXY() { + assertEquals(coordinate.getX(), x); + assertEquals(coordinate.getY(), y); + } + +} diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 168dade..d885970 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -1,7 +1,7 @@ package de.tims.fleetstorm.matchfield; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.stream.Stream; From 295ecd2bb157c98302db2ff84add0fb9354dfb57 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 13:16:55 +0100 Subject: [PATCH 09/93] add DeafaultState(EMPTY) to Coordinate --- .../java/de/tims/fleetstorm/matchfield/Coordinate.java | 10 ++++++++++ .../de/tims/fleetstorm/matchfield/CoordinateTest.java | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index a084376..dcace9e 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -3,10 +3,17 @@ package de.tims.fleetstorm.matchfield; public class Coordinate { private int x; private int y; + private int state; + + public static final int EMPTY = 0; + public static final int SHIP = 1; + public static final int SHOT = 2; + public static final int HIT = 3; public Coordinate(int x, int y) { this.x = x; this.y = y; + this.state = 0; } public Integer getX() { @@ -17,4 +24,7 @@ public class Coordinate { return y; } + public int getState() { + return this.state; + } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 75b87af..d37a398 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -17,4 +17,9 @@ class CoordinateTest { assertEquals(coordinate.getY(), y); } + @Test + void testCoordinatehasDefaultStates() { + assertEquals(coordinate.getState(), Coordinate.EMPTY); + assertEquals(coordinate.getState(), 0); + } } From 10c4518ed0951af816fd46c24d65908ed9ee8d05 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 13:40:34 +0100 Subject: [PATCH 10/93] add setState --- .../fleetstorm/matchfield/Coordinate.java | 4 ++++ .../fleetstorm/matchfield/CoordinateTest.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index dcace9e..2f7ec5a 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -27,4 +27,8 @@ public class Coordinate { public int getState() { return this.state; } + + public void setState(int state) { + this.state = state; + } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index d37a398..f7bb7f1 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -22,4 +22,23 @@ class CoordinateTest { assertEquals(coordinate.getState(), Coordinate.EMPTY); assertEquals(coordinate.getState(), 0); } + + @Test + void testGetAndSetForAllStates() { + coordinate.setState(Coordinate.SHIP); + assertEquals(coordinate.getState(), Coordinate.SHIP); + assertEquals(coordinate.getState(), 1); + + coordinate.setState(Coordinate.SHOT); + assertEquals(coordinate.getState(), Coordinate.SHOT); + assertEquals(coordinate.getState(), 2); + + coordinate.setState(Coordinate.HIT); + assertEquals(coordinate.getState(), Coordinate.HIT); + assertEquals(coordinate.getState(), 3); + + coordinate.setState(Coordinate.EMPTY); + assertEquals(coordinate.getState(), Coordinate.EMPTY); + assertEquals(coordinate.getState(), 0); + } } From f8a8179650b936b54acd815a6969dcea52751aa7 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 14:02:20 +0100 Subject: [PATCH 11/93] Refactoring: Implements Coordinate into Matchfield --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 5 +++-- .../tims/fleetstorm/matchfield/Matchfield.java | 16 ++++++---------- .../java/de/tims/fleetstorm/ai/LogicTest.java | 4 +++- .../matchfield/MatchfieldCreationTest.java | 6 +++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index d918ed5..a76512c 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -3,6 +3,7 @@ package de.tims.fleetstorm.ai; import java.util.ArrayList; import java.util.Random; +import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; public class Logic { @@ -11,14 +12,14 @@ public class Logic { ArrayList possibleFields = new ArrayList(); for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { - if (matchfield.getState(x, y) == Matchfield.EMPTY || matchfield.getState(x, y) == Matchfield.SHIP) { + if (matchfield.getState(x, y) == Coordinate.EMPTY || matchfield.getState(x, y) == Coordinate.SHIP) { possibleFields.add(new int[] { x, y }); } } } Random randy = new Random(); - return possibleFields.get(randy.nextInt(possibleFields.size() - 1)); + return possibleFields.get(randy.nextInt(possibleFields.size())); } } diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 45912eb..7a30858 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -2,22 +2,18 @@ package de.tims.fleetstorm.matchfield; public class Matchfield { - private int[][] matchfield; + private Coordinate[][] matchfield; private 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]; + this.matchfield = new Coordinate[this.size][this.size]; } - public int[][] createMatchfield() { + public Coordinate[][] createMatchfield() { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { - this.matchfield[i][j] = Matchfield.EMPTY; + this.matchfield[i][j] = new Coordinate(i, j); } } @@ -29,11 +25,11 @@ public class Matchfield { } public int getState(int x, int y) { - return this.matchfield[x][y]; + return this.matchfield[x][y].getState(); } public void setState(int x, int y, int state) { - this.matchfield[x][y] = state; + this.matchfield[x][y].setState(state); } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index d8ebcf5..4fbb52c 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; +import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; class LogicTest { @@ -14,6 +15,7 @@ class LogicTest { @Test void testFieldIsNotNull() { + matchfield.createMatchfield(); int[] calcResult = logic.chooseField(matchfield); assertNotNull(calcResult); } @@ -30,6 +32,6 @@ class LogicTest { int[] choosenField = logic.chooseField(matchfield); int calcState = matchfield.getState(choosenField[0], choosenField[1]); - assertNotEquals(calcState, Matchfield.SHOT); + assertNotEquals(calcState, Coordinate.SHOT); } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index d885970..ce47268 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -15,7 +15,7 @@ class MatchfieldCreationTest { @Test void testMatchfieldCreateNotEmpty() { Matchfield matchfield = new Matchfield(0); - int[][] calcResult = matchfield.createMatchfield(); + Coordinate[][] calcResult = matchfield.createMatchfield(); assertNotNull(calcResult); } @@ -44,7 +44,7 @@ class MatchfieldCreationTest { } static Stream testMatchfieldGetFieldState() { - return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Matchfield.EMPTY)); + return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Coordinate.EMPTY)); } @ParameterizedTest(name = "matchfield change field is correct") @@ -61,6 +61,6 @@ class MatchfieldCreationTest { static Stream testMatchfieldSetStateIsCorrect() { return Stream.of( - Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Matchfield.SHIP, Matchfield.SHIP)); + Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Coordinate.SHIP, Coordinate.SHIP)); } } From 78e804dff9595700d174d903360710d3bb5d648e Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 14:07:01 +0100 Subject: [PATCH 12/93] Refactoring: Implements Coordinate into ai-logic --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 6 +++--- src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index a76512c..3fefa1b 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -8,12 +8,12 @@ import de.tims.fleetstorm.matchfield.Matchfield; public class Logic { - public int[] chooseField(Matchfield matchfield) { - ArrayList possibleFields = new ArrayList(); + public Coordinate chooseField(Matchfield matchfield) { + ArrayList possibleFields = new ArrayList(); for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { if (matchfield.getState(x, y) == Coordinate.EMPTY || matchfield.getState(x, y) == Coordinate.SHIP) { - possibleFields.add(new int[] { x, y }); + possibleFields.add(new Coordinate(x, y)); } } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 4fbb52c..7c472aa 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -16,7 +16,7 @@ class LogicTest { @Test void testFieldIsNotNull() { matchfield.createMatchfield(); - int[] calcResult = logic.chooseField(matchfield); + Coordinate calcResult = logic.chooseField(matchfield); assertNotNull(calcResult); } @@ -30,8 +30,8 @@ class LogicTest { } // matchfield.setState(0, 0, Matchfield.EMPTY); - int[] choosenField = logic.chooseField(matchfield); - int calcState = matchfield.getState(choosenField[0], choosenField[1]); + Coordinate choosenField = logic.chooseField(matchfield); + int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); } } From 0106ea257708fffe635c0dd00b9e906a160f3730 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 14:41:15 +0100 Subject: [PATCH 13/93] Refactoring: Use ParameterizedTest in CoordinateTest --- .../fleetstorm/matchfield/Coordinate.java | 1 + .../fleetstorm/matchfield/CoordinateTest.java | 34 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index 2f7ec5a..3e16694 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -31,4 +31,5 @@ public class Coordinate { public void setState(int state) { this.state = state; } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index f7bb7f1..488aeda 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -1,8 +1,14 @@ package de.tims.fleetstorm.matchfield; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +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 CoordinateTest { @@ -23,22 +29,18 @@ class CoordinateTest { assertEquals(coordinate.getState(), 0); } - @Test - void testGetAndSetForAllStates() { - coordinate.setState(Coordinate.SHIP); - assertEquals(coordinate.getState(), Coordinate.SHIP); - assertEquals(coordinate.getState(), 1); - - coordinate.setState(Coordinate.SHOT); - assertEquals(coordinate.getState(), Coordinate.SHOT); - assertEquals(coordinate.getState(), 2); - - coordinate.setState(Coordinate.HIT); - assertEquals(coordinate.getState(), Coordinate.HIT); - assertEquals(coordinate.getState(), 3); + @ParameterizedTest(name = "All Getters/Setters are working") + @MethodSource("AllGettersAreWorking") + void testGetAndSetForAllStates(String testName, int state, int stateCode, int expectedResult) { + coordinate.setState(state); + int result = coordinate.getState(); + assertThat(result).describedAs(testName).isEqualTo(expectedResult); + } - coordinate.setState(Coordinate.EMPTY); - assertEquals(coordinate.getState(), Coordinate.EMPTY); - assertEquals(coordinate.getState(), 0); + static Stream AllGettersAreWorking() { + return Stream.of(Arguments.of("State = SHIP", 1, Coordinate.SHIP, Coordinate.SHIP), + Arguments.of("State = SHOT", 2, Coordinate.SHOT, Coordinate.SHOT), + Arguments.of("State = HIT", 3, Coordinate.HIT, Coordinate.HIT), + Arguments.of("State = EMPTY", 0, Coordinate.EMPTY, Coordinate.EMPTY)); } } From 958fcf71660d9450e233e37175f669c7f9d5a2f7 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 15:21:24 +0100 Subject: [PATCH 14/93] add getAbove() in Matchfield - DefaultCase --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 12 ++++++++++++ .../tims/fleetstorm/matchfield/CoordinateTest.java | 1 + .../matchfield/MatchfieldCreationTest.java | 13 +++++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 7a30858..1716226 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -32,4 +32,16 @@ public class Matchfield { this.matchfield[x][y].setState(state); } + public Coordinate getAbove(Coordinate center) { + return this.matchfield[center.getX() + 1][center.getY()]; + } + + public Coordinate getField(Coordinate coordinate) { + return matchfield[coordinate.getX()][coordinate.getY()]; + } + + public Coordinate getField(int x, int y) { + return matchfield[x][y]; + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 488aeda..3de5fc8 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -43,4 +43,5 @@ class CoordinateTest { Arguments.of("State = HIT", 3, Coordinate.HIT, Coordinate.HIT), Arguments.of("State = EMPTY", 0, Coordinate.EMPTY, Coordinate.EMPTY)); } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index ce47268..d4c5f3a 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -63,4 +63,17 @@ class MatchfieldCreationTest { return Stream.of( Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Coordinate.SHIP, Coordinate.SHIP)); } + + @ParameterizedTest(name = "Get the Coordinate above") + @MethodSource("getCoordinateAbove") + void testGetAbove(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { + + Coordinate result = matchfield.getAbove(center); + assertThat(result).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream getCoordinateAbove() { + Matchfield matchfield = new Matchfield(10); + return Stream.of(Arguments.of("above from (5/5)", matchfield, new Coordinate(5, 5), matchfield.getField(6, 5))); + } } From d703a7b1c6fa91280b1cd2c1e678fded3a00d098 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 15:24:59 +0100 Subject: [PATCH 15/93] add getAbove() in Matchfield - Case: nothingAbove --- src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java | 3 +++ .../tims/fleetstorm/matchfield/MatchfieldCreationTest.java | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 1716226..5dac96f 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -41,6 +41,9 @@ public class Matchfield { } public Coordinate getField(int x, int y) { + if (x == 0) { + return null; + } return matchfield[x][y]; } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index d4c5f3a..c829a9d 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -74,6 +74,9 @@ class MatchfieldCreationTest { static Stream getCoordinateAbove() { Matchfield matchfield = new Matchfield(10); - return Stream.of(Arguments.of("above from (5/5)", matchfield, new Coordinate(5, 5), matchfield.getField(6, 5))); + return Stream.of( + Arguments.of("above from (5/5) - should be (6,5)", matchfield, new Coordinate(5, 5), + matchfield.getField(6, 5)), + Arguments.of("above from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); } } From ac41493feb3a58e4d605d48feb922cd8c3c228c4 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 15:34:51 +0100 Subject: [PATCH 16/93] add getBelow() in Matchfield - DefaultCase --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 7 ++++++- .../matchfield/MatchfieldCreationTest.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 5dac96f..e803cee 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -44,7 +44,12 @@ public class Matchfield { if (x == 0) { return null; } - return matchfield[x][y]; + return this.matchfield[x][y]; + } + + public Coordinate getBelow(Coordinate center) { + // TODO Auto-generated method stub + return this.matchfield[center.getX() - 1][center.getY()]; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index c829a9d..edb160c 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -79,4 +79,18 @@ class MatchfieldCreationTest { matchfield.getField(6, 5)), Arguments.of("above from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); } + + @ParameterizedTest(name = "Get the Coordinate below") + @MethodSource("getCoordinateBelow") + void testGetBelow(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { + + Coordinate result = matchfield.getBelow(center); + assertThat(result).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream getCoordinateBelow() { + Matchfield matchfield = new Matchfield(10); + return Stream.of(Arguments.of("bewlow from (5/5) - should be (4,5)", matchfield, new Coordinate(5, 5), + matchfield.getField(4, 5))); + } } From bdb4c30cdb7b1e5d175b9ed2986c1acf7a418413 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 15:41:52 +0100 Subject: [PATCH 17/93] add getAbove() in Matchfield - Case: nothingBelow --- src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java | 4 +++- .../tims/fleetstorm/matchfield/MatchfieldCreationTest.java | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index e803cee..9c86328 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -48,7 +48,9 @@ public class Matchfield { } public Coordinate getBelow(Coordinate center) { - // TODO Auto-generated method stub + if (center.getX() == 0) { + return null; + } return this.matchfield[center.getX() - 1][center.getY()]; } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index edb160c..1936f86 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -90,7 +90,9 @@ class MatchfieldCreationTest { static Stream getCoordinateBelow() { Matchfield matchfield = new Matchfield(10); - return Stream.of(Arguments.of("bewlow from (5/5) - should be (4,5)", matchfield, new Coordinate(5, 5), - matchfield.getField(4, 5))); + return Stream.of( + Arguments.of("bewlow from (5/5) - should be (4,5)", matchfield, new Coordinate(5, 5), + matchfield.getField(4, 5)), + Arguments.of("bewlow from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); } } From 2603cb9f83862afacd97830bfc33ea704bd2bd17 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 15:55:59 +0100 Subject: [PATCH 18/93] Fix: Above = Right and Below = Left --- .../fleetstorm/matchfield/Matchfield.java | 23 +++++++++--------- .../matchfield/MatchfieldCreationTest.java | 24 +++++++++---------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 9c86328..4c2b5b2 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -32,26 +32,27 @@ public class Matchfield { this.matchfield[x][y].setState(state); } - public Coordinate getAbove(Coordinate center) { + public Coordinate getRight(Coordinate center) { + if (center.getX() == matchfield.length - 1) { + return null; + } return this.matchfield[center.getX() + 1][center.getY()]; } + public Coordinate getLeft(Coordinate center) { + if (center.getX() == 0) { + return null; + } + return this.matchfield[center.getX() - 1][center.getY()]; + } + public Coordinate getField(Coordinate coordinate) { return matchfield[coordinate.getX()][coordinate.getY()]; } public Coordinate getField(int x, int y) { - if (x == 0) { - return null; - } - return this.matchfield[x][y]; - } - public Coordinate getBelow(Coordinate center) { - if (center.getX() == 0) { - return null; - } - return this.matchfield[center.getX() - 1][center.getY()]; + return this.matchfield[x][y]; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 1936f86..e99406d 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -64,35 +64,35 @@ class MatchfieldCreationTest { Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Coordinate.SHIP, Coordinate.SHIP)); } - @ParameterizedTest(name = "Get the Coordinate above") - @MethodSource("getCoordinateAbove") + @ParameterizedTest(name = "Get the Coordinate right") + @MethodSource("getCoordinateRight") void testGetAbove(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { - Coordinate result = matchfield.getAbove(center); + Coordinate result = matchfield.getRight(center); assertThat(result).describedAs(testName).isEqualTo(expectedResult); } - static Stream getCoordinateAbove() { + static Stream getCoordinateRight() { Matchfield matchfield = new Matchfield(10); return Stream.of( - Arguments.of("above from (5/5) - should be (6,5)", matchfield, new Coordinate(5, 5), + Arguments.of("right from (5/5) - should be (6,5)", matchfield, new Coordinate(5, 5), matchfield.getField(6, 5)), - Arguments.of("above from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); + Arguments.of("right from (9/5) - should be null", matchfield, new Coordinate(9, 5), null)); } - @ParameterizedTest(name = "Get the Coordinate below") - @MethodSource("getCoordinateBelow") + @ParameterizedTest(name = "Get the Coordinate left") + @MethodSource("getCoordinateLeft") void testGetBelow(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { - Coordinate result = matchfield.getBelow(center); + Coordinate result = matchfield.getLeft(center); assertThat(result).describedAs(testName).isEqualTo(expectedResult); } - static Stream getCoordinateBelow() { + static Stream getCoordinateLeft() { Matchfield matchfield = new Matchfield(10); return Stream.of( - Arguments.of("bewlow from (5/5) - should be (4,5)", matchfield, new Coordinate(5, 5), + Arguments.of("left from (5/5) - should be (4,5)", matchfield, new Coordinate(5, 5), matchfield.getField(4, 5)), - Arguments.of("bewlow from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); + Arguments.of("left from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); } } From 304b3c943e0b3fe909f705b51b227640755c1cac Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 16:30:32 +0100 Subject: [PATCH 19/93] add getAbove() in Matchfield - DefaultCase --- .../tims/fleetstorm/matchfield/Matchfield.java | 17 +++++++++++------ .../matchfield/MatchfieldCreationTest.java | 18 ++++++++++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 4c2b5b2..f98cc81 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -32,6 +32,15 @@ public class Matchfield { this.matchfield[x][y].setState(state); } + public Coordinate getField(Coordinate coordinate) { + return matchfield[coordinate.getX()][coordinate.getY()]; + } + + public Coordinate getField(int x, int y) { + + return this.matchfield[x][y]; + } + public Coordinate getRight(Coordinate center) { if (center.getX() == matchfield.length - 1) { return null; @@ -46,13 +55,9 @@ public class Matchfield { return this.matchfield[center.getX() - 1][center.getY()]; } - public Coordinate getField(Coordinate coordinate) { - return matchfield[coordinate.getX()][coordinate.getY()]; - } + public Coordinate getAbove(Coordinate center) { - public Coordinate getField(int x, int y) { - - return this.matchfield[x][y]; + return matchfield[center.getX()][center.getY() + 1]; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index e99406d..d62cf69 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -66,7 +66,7 @@ class MatchfieldCreationTest { @ParameterizedTest(name = "Get the Coordinate right") @MethodSource("getCoordinateRight") - void testGetAbove(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { + void testGetRight(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { Coordinate result = matchfield.getRight(center); assertThat(result).describedAs(testName).isEqualTo(expectedResult); @@ -82,7 +82,7 @@ class MatchfieldCreationTest { @ParameterizedTest(name = "Get the Coordinate left") @MethodSource("getCoordinateLeft") - void testGetBelow(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { + void testGetLeft(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { Coordinate result = matchfield.getLeft(center); assertThat(result).describedAs(testName).isEqualTo(expectedResult); @@ -95,4 +95,18 @@ class MatchfieldCreationTest { matchfield.getField(4, 5)), Arguments.of("left from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); } + + @ParameterizedTest(name = "Get the Coordinate above") + @MethodSource("getCoordinateAbove") + void testGetAbove(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { + + Coordinate result = matchfield.getAbove(center); + assertThat(result).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream getCoordinateAbove() { + Matchfield matchfield = new Matchfield(10); + return Stream.of(Arguments.of("above from (5/5) - should be (5,6)", matchfield, new Coordinate(5, 5), + matchfield.getField(5, 6))); + } } From 7c67747398c50f7c3cd1852ffaa4455ed454ec37 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 16:37:13 +0100 Subject: [PATCH 20/93] add getAbove() in Matchfield - Case: nothingAbove --- src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java | 4 +++- .../tims/fleetstorm/matchfield/MatchfieldCreationTest.java | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index f98cc81..919bbe9 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -56,7 +56,9 @@ public class Matchfield { } public Coordinate getAbove(Coordinate center) { - + if (center.getY() == matchfield.length - 1) { + return null; + } return matchfield[center.getX()][center.getY() + 1]; } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index d62cf69..41f1790 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -106,7 +106,9 @@ class MatchfieldCreationTest { static Stream getCoordinateAbove() { Matchfield matchfield = new Matchfield(10); - return Stream.of(Arguments.of("above from (5/5) - should be (5,6)", matchfield, new Coordinate(5, 5), - matchfield.getField(5, 6))); + return Stream.of( + Arguments.of("above from (5/5) - should be (5,6)", matchfield, new Coordinate(5, 5), + matchfield.getField(5, 6)), + Arguments.of("above from (5/9) - should be null", matchfield, new Coordinate(5, 5), null)); } } From 7a43204fddc37520b6efc3ff739e6e43bd11c30b Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 16:44:38 +0100 Subject: [PATCH 21/93] add getBelow() in Matchfield - DefaultCase --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 5 +++++ .../matchfield/MatchfieldCreationTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 919bbe9..a02750a 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -62,4 +62,9 @@ public class Matchfield { return matchfield[center.getX()][center.getY() + 1]; } + public Coordinate getBelow(Coordinate center) { + // TODO Auto-generated method stub + return matchfield[center.getX()][center.getY() - 1]; + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 41f1790..5b165f8 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -111,4 +111,18 @@ class MatchfieldCreationTest { matchfield.getField(5, 6)), Arguments.of("above from (5/9) - should be null", matchfield, new Coordinate(5, 5), null)); } + + @ParameterizedTest(name = "Get the Coordinate below") + @MethodSource("getCoordinateBelow") + void testGetBelow(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { + + Coordinate result = matchfield.getBelow(center); + assertThat(result).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream getCoordinateBelow() { + Matchfield matchfield = new Matchfield(10); + return Stream.of(Arguments.of("below from (5/5) - should be (5,4)", matchfield, new Coordinate(5, 5), + matchfield.getField(5, 4))); + } } From 66e3394f09274e0b80373267058880f9851e685d Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 16:48:10 +0100 Subject: [PATCH 22/93] add getBelow() in Matchfield - case: nothingBelow --- src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java | 4 +++- .../tims/fleetstorm/matchfield/MatchfieldCreationTest.java | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index a02750a..45b08c4 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -63,7 +63,9 @@ public class Matchfield { } public Coordinate getBelow(Coordinate center) { - // TODO Auto-generated method stub + if (center.getY() == 0) { + return null; + } return matchfield[center.getX()][center.getY() - 1]; } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 5b165f8..6db8108 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -122,7 +122,9 @@ class MatchfieldCreationTest { static Stream getCoordinateBelow() { Matchfield matchfield = new Matchfield(10); - return Stream.of(Arguments.of("below from (5/5) - should be (5,4)", matchfield, new Coordinate(5, 5), - matchfield.getField(5, 4))); + return Stream.of( + Arguments.of("below from (5/5) - should be (5,4)", matchfield, new Coordinate(5, 5), + matchfield.getField(5, 4)), + Arguments.of("below from (5/0) - should be null", matchfield, new Coordinate(5, 0), null)); } } From d847e33ef59403e16faf0cffc7901e5e3908bf93 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 17:00:53 +0100 Subject: [PATCH 23/93] Refact6oring: ai-logicTest restructured --- src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 7c472aa..9a201d8 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -9,12 +9,13 @@ import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; class LogicTest { - Logic logic = new Logic(); - int size = 3; - private Matchfield matchfield = new Matchfield(size); @Test void testFieldIsNotNull() { + Logic logic = new Logic(); + Matchfield matchfield; + int size = 5; + matchfield = new Matchfield(size); matchfield.createMatchfield(); Coordinate calcResult = logic.chooseField(matchfield); assertNotNull(calcResult); @@ -22,7 +23,12 @@ class LogicTest { @Test void testChoosenFieldHasNotStateShot() { + Logic logic = new Logic(); + Matchfield matchfield; + int size = 5; + matchfield = new Matchfield(size); matchfield.createMatchfield(); + for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { // matchfield.setState(x, y, Matchfield.SHOT); From f3beee8e452d1955175f0f35fe3a9a5125cf8806 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 19:04:11 +0100 Subject: [PATCH 24/93] add getEverySecondField in ai-logic --- .../java/de/tims/fleetstorm/ai/Logic.java | 13 ++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 33 +++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 3fefa1b..ddbc555 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -22,4 +22,17 @@ public class Logic { return possibleFields.get(randy.nextInt(possibleFields.size())); } + public ArrayList getEverySecondField(Matchfield matchfield) { + ArrayList out = new ArrayList(); + for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { + for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { + if ((x % 2 == 0 && y % 2 == 0) || (x % 2 == 1 && y % 2 == 1)) { + out.add(matchfield.getField(x, y)); + } + } + } + + return out; + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 9a201d8..96d8eae 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -1,8 +1,11 @@ package de.tims.fleetstorm.ai; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.ArrayList; + import org.junit.jupiter.api.Test; import de.tims.fleetstorm.matchfield.Coordinate; @@ -31,13 +34,39 @@ class LogicTest { for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { - // matchfield.setState(x, y, Matchfield.SHOT); + matchfield.setState(x, y, Coordinate.SHOT); } } - // matchfield.setState(0, 0, Matchfield.EMPTY); + matchfield.setState(2, 2, Coordinate.EMPTY); Coordinate choosenField = logic.chooseField(matchfield); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); } + + @Test + void testGetEverySecondField() { + Logic logic = new Logic(); + Matchfield matchfield; + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + + ArrayList everySecondField = logic.getEverySecondField(matchfield); + ArrayList expectedResult = new ArrayList(); + + expectedResult.add(new Coordinate(0, 0)); + expectedResult.add(new Coordinate(0, 2)); + expectedResult.add(new Coordinate(1, 1)); + expectedResult.add(new Coordinate(1, 3)); + expectedResult.add(new Coordinate(2, 0)); + expectedResult.add(new Coordinate(2, 2)); + expectedResult.add(new Coordinate(3, 1)); + expectedResult.add(new Coordinate(3, 3)); + + for (int i = 0; i < expectedResult.size(); i++) { + assertEquals(everySecondField.get(i).getX(), expectedResult.get(i).getX()); + assertEquals(everySecondField.get(i).getY(), expectedResult.get(i).getY()); + } + } } From 25d399e1a60f1eccc0562a120d7e80378bf08779 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Thu, 13 Jan 2022 12:16:32 +0100 Subject: [PATCH 25/93] Refactoring testEverySecondField (override Equals of Coordinate) --- .../java/de/tims/fleetstorm/matchfield/Coordinate.java | 8 ++++++++ src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 5 +---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index 3e16694..705c77c 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -32,4 +32,12 @@ public class Coordinate { this.state = state; } + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Coordinate)) { + return false; + } + return this.x == ((Coordinate) obj).getX() && this.y == ((Coordinate) obj).getY(); + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 96d8eae..5eb55c0 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -64,9 +64,6 @@ class LogicTest { expectedResult.add(new Coordinate(3, 1)); expectedResult.add(new Coordinate(3, 3)); - for (int i = 0; i < expectedResult.size(); i++) { - assertEquals(everySecondField.get(i).getX(), expectedResult.get(i).getX()); - assertEquals(everySecondField.get(i).getY(), expectedResult.get(i).getY()); - } + assertEquals(everySecondField, expectedResult); } } From 6a00b0bad3bc531e934daea9e6ae9f57fafc30eb Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Thu, 13 Jan 2022 13:19:17 +0100 Subject: [PATCH 26/93] add lastShot with Getter/Setter in ai-logic --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 11 +++++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index ddbc555..ca0e69e 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -8,6 +8,8 @@ import de.tims.fleetstorm.matchfield.Matchfield; public class Logic { + private Coordinate lastShot; + public Coordinate chooseField(Matchfield matchfield) { ArrayList possibleFields = new ArrayList(); for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { @@ -35,4 +37,13 @@ public class Logic { return out; } + public void setLastShot(Coordinate coordinate, Matchfield matchfield) { + lastShot = matchfield.getField(coordinate); + + } + + public Coordinate getLastShot() { + return lastShot; + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 5eb55c0..dd4f8c3 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -66,4 +66,21 @@ class LogicTest { assertEquals(everySecondField, expectedResult); } + + @Test + void testGetAndSetLastShot() { + Logic logic = new Logic(); + Matchfield matchfield; + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + + Coordinate expectedResult = new Coordinate(2, 2); + logic.setLastShot(expectedResult, matchfield); + Coordinate result = logic.getLastShot(); + + assertEquals(result, expectedResult); + + } + } From bc2e79b7a35656c26a08495ca77277a20ee58e13 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Thu, 13 Jan 2022 13:30:35 +0100 Subject: [PATCH 27/93] add Matchfield variable with Getter/Setter in ai-logic --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 10 ++++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index ca0e69e..e6547ee 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -8,6 +8,7 @@ import de.tims.fleetstorm.matchfield.Matchfield; public class Logic { + private Matchfield matchfield; private Coordinate lastShot; public Coordinate chooseField(Matchfield matchfield) { @@ -46,4 +47,13 @@ public class Logic { return lastShot; } + public void setMatchfield(Matchfield matchfield) { + this.matchfield = matchfield; + + } + + public Matchfield getMatchfield() { + return matchfield; + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index dd4f8c3..8fafc91 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -83,4 +83,20 @@ class LogicTest { } + @Test + void testSetMatchfield() { + Logic logic = new Logic(); + Matchfield matchfield; + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + + Matchfield result = logic.getMatchfield(); + assertEquals(result, null); + logic.setMatchfield(matchfield); + result = logic.getMatchfield(); + assertNotNull(result); + + } + } From ba00efe5e28f5cd5d73cd84bd74453c13feba100 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Thu, 13 Jan 2022 13:34:41 +0100 Subject: [PATCH 28/93] Refactoring: setLastShot() doesn't need param matchfield anymore --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 4 ++-- src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index e6547ee..e80125d 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -38,8 +38,8 @@ public class Logic { return out; } - public void setLastShot(Coordinate coordinate, Matchfield matchfield) { - lastShot = matchfield.getField(coordinate); + public void setLastShot(Coordinate coordinate) { + lastShot = this.matchfield.getField(coordinate); } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 8fafc91..0c47f54 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -74,9 +74,10 @@ class LogicTest { int size = 4; matchfield = new Matchfield(size); matchfield.createMatchfield(); + logic.setMatchfield(matchfield); Coordinate expectedResult = new Coordinate(2, 2); - logic.setLastShot(expectedResult, matchfield); + logic.setLastShot(expectedResult); Coordinate result = logic.getLastShot(); assertEquals(result, expectedResult); @@ -96,7 +97,6 @@ class LogicTest { logic.setMatchfield(matchfield); result = logic.getMatchfield(); assertNotNull(result); - } } From 2ace6f42a9e2ccc592b18d6b60afbd0e8258ad86 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Thu, 13 Jan 2022 13:54:27 +0100 Subject: [PATCH 29/93] Refactoring: chooseField() doesn't need param matchfield anymore --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 9 +++++---- src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index e80125d..668984b 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -11,11 +11,12 @@ public class Logic { private Matchfield matchfield; private Coordinate lastShot; - public Coordinate chooseField(Matchfield matchfield) { + public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); - for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { - for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { - if (matchfield.getState(x, y) == Coordinate.EMPTY || matchfield.getState(x, y) == Coordinate.SHIP) { + for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) { + for (int y = 0; y < Math.sqrt(this.matchfield.getSize()); y++) { + if (this.matchfield.getState(x, y) == Coordinate.EMPTY + || this.matchfield.getState(x, y) == Coordinate.SHIP) { possibleFields.add(new Coordinate(x, y)); } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 0c47f54..440710d 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -20,7 +20,8 @@ class LogicTest { int size = 5; matchfield = new Matchfield(size); matchfield.createMatchfield(); - Coordinate calcResult = logic.chooseField(matchfield); + logic.setMatchfield(matchfield); + Coordinate calcResult = logic.chooseField(); assertNotNull(calcResult); } @@ -31,6 +32,7 @@ class LogicTest { int size = 5; matchfield = new Matchfield(size); matchfield.createMatchfield(); + logic.setMatchfield(matchfield); for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { @@ -39,7 +41,7 @@ class LogicTest { } matchfield.setState(2, 2, Coordinate.EMPTY); - Coordinate choosenField = logic.chooseField(matchfield); + Coordinate choosenField = logic.chooseField(); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); } From bf1f6b2ac306d022d6188da4ebb023c910d9beff Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Thu, 13 Jan 2022 14:56:55 +0100 Subject: [PATCH 30/93] Refactoring: getEverySecondField() doesn't need param matchfield anymore --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 8 ++++---- src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 668984b..8acdc8d 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -26,12 +26,12 @@ public class Logic { return possibleFields.get(randy.nextInt(possibleFields.size())); } - public ArrayList getEverySecondField(Matchfield matchfield) { + public ArrayList getEverySecondField() { ArrayList out = new ArrayList(); - for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { - for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { + for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) { + for (int y = 0; y < Math.sqrt(this.matchfield.getSize()); y++) { if ((x % 2 == 0 && y % 2 == 0) || (x % 2 == 1 && y % 2 == 1)) { - out.add(matchfield.getField(x, y)); + out.add(this.matchfield.getField(x, y)); } } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 440710d..7257ccf 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -53,8 +53,9 @@ class LogicTest { int size = 4; matchfield = new Matchfield(size); matchfield.createMatchfield(); + logic.setMatchfield(matchfield); - ArrayList everySecondField = logic.getEverySecondField(matchfield); + ArrayList everySecondField = logic.getEverySecondField(); ArrayList expectedResult = new ArrayList(); expectedResult.add(new Coordinate(0, 0)); From 61cd9ad08abce996b34c338ef25c3a4c3cad426f Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Thu, 13 Jan 2022 15:14:37 +0100 Subject: [PATCH 31/93] add findShip() --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 9 +++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 8acdc8d..76e29f8 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -10,6 +10,7 @@ public class Logic { private Matchfield matchfield; private Coordinate lastShot; + public boolean foundShip = false; public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); @@ -57,4 +58,12 @@ public class Logic { return matchfield; } + public void findShip() { + if (lastShot.getState() == Coordinate.HIT) { + + foundShip = true; + } + System.out.println("Do this"); + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 7257ccf..f266a76 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -102,4 +102,19 @@ class LogicTest { assertNotNull(result); } + @Test + void testFindShip() { + Logic logic = new Logic(); + Matchfield matchfield; + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + logic.setMatchfield(matchfield); + + logic.setLastShot(new Coordinate(2, 2)); + logic.getLastShot().setState(Coordinate.HIT); + logic.findShip(); + assertEquals(logic.foundShip, true); + } + } From e064873635d46569ca2a1527f42b9280c2fff36b Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 10:31:14 +0100 Subject: [PATCH 32/93] Refactoring: boolean foundShip is private now --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 11 ++++++++++- src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 76e29f8..e6838d4 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -10,7 +10,7 @@ public class Logic { private Matchfield matchfield; private Coordinate lastShot; - public boolean foundShip = false; + private boolean foundShip = false; public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); @@ -66,4 +66,13 @@ public class Logic { System.out.println("Do this"); } + public void setFoundShip(boolean b) { + this.foundShip = b; + } + + public boolean getFoundShip() { + // TODO Auto-generated method stub + return this.foundShip; + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index f266a76..c0b7f08 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -114,7 +114,14 @@ class LogicTest { logic.setLastShot(new Coordinate(2, 2)); logic.getLastShot().setState(Coordinate.HIT); logic.findShip(); - assertEquals(logic.foundShip, true); + assertEquals(logic.getFoundShip(), true); + } + + @Test + void testGetAndsetFoundShip() { + Logic logic = new Logic(); + logic.setFoundShip(true); + assertEquals(logic.getFoundShip(), true); } } From 794f4bbb516ad731f5d43b34cd749ff5f0dbfd27 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 11:43:18 +0100 Subject: [PATCH 33/93] add clearAbove --- .../java/de/tims/fleetstorm/ai/Logic.java | 20 +++++++++++++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 17 ++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index e6838d4..2db3212 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -10,7 +10,9 @@ public class Logic { private Matchfield matchfield; private Coordinate lastShot; + public Coordinate target; private boolean foundShip = false; + public boolean clearedAbove = false; public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); @@ -75,4 +77,22 @@ public class Logic { return this.foundShip; } + public void checkAround(Coordinate coordinate) { + // TODO Auto-generated method stub + + } + + public void clearAbove(Coordinate shot) { + target = matchfield.getAbove(shot); + + if (target.getState() == Coordinate.EMPTY) { + clearedAbove = true; + } + } + + private void shot() { + // TODO Auto-generated method stub + + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index c0b7f08..76f4759 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -124,4 +124,21 @@ class LogicTest { assertEquals(logic.getFoundShip(), true); } + @Test + void testClearAbove() { + Logic logic = new Logic(); + Matchfield matchfield; + Coordinate shot = new Coordinate(2, 2); + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + logic.setMatchfield(matchfield); + logic.setLastShot(shot); + matchfield.getField(shot).setState(Coordinate.EMPTY); + + logic.clearAbove(shot); + + assertEquals(logic.clearedAbove, true); + } + } From 0387c76c9733edf6749bdfd32bf3831d6341a64a Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 11:54:05 +0100 Subject: [PATCH 34/93] add clearBelow --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 9 +++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 2db3212..64f5e6d 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -13,6 +13,7 @@ public class Logic { public Coordinate target; private boolean foundShip = false; public boolean clearedAbove = false; + public boolean clearedBelow = false; public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); @@ -95,4 +96,12 @@ public class Logic { } + public void clearBelow(Coordinate shot) { + target = matchfield.getBelow(shot); + + if (target.getState() == Coordinate.EMPTY) { + clearedBelow = true; + } + + } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 76f4759..0267c1a 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -141,4 +141,21 @@ class LogicTest { assertEquals(logic.clearedAbove, true); } + @Test + void testClearBelow() { + Logic logic = new Logic(); + Matchfield matchfield; + Coordinate shot = new Coordinate(2, 2); + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + logic.setMatchfield(matchfield); + logic.setLastShot(shot); + matchfield.getField(shot).setState(Coordinate.EMPTY); + + logic.clearBelow(shot); + + assertEquals(logic.clearedBelow, true); + } + } From ba410306ac6976fc3ec89974ed2c744a8c1fc0ae Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 11:57:30 +0100 Subject: [PATCH 35/93] add clearRight --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 10 ++++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 64f5e6d..ad891cc 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -14,6 +14,7 @@ public class Logic { private boolean foundShip = false; public boolean clearedAbove = false; public boolean clearedBelow = false; + public boolean clearedRight = false; public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); @@ -104,4 +105,13 @@ public class Logic { } } + + public void clearRight(Coordinate shot) { + target = matchfield.getRight(shot); + + if (target.getState() == Coordinate.EMPTY) { + clearedRight = true; + } + + } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 0267c1a..79a8ab8 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -158,4 +158,21 @@ class LogicTest { assertEquals(logic.clearedBelow, true); } + @Test + void testClearRight() { + Logic logic = new Logic(); + Matchfield matchfield; + Coordinate shot = new Coordinate(2, 2); + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + logic.setMatchfield(matchfield); + logic.setLastShot(shot); + matchfield.getField(shot).setState(Coordinate.EMPTY); + + logic.clearRight(shot); + + assertEquals(logic.clearedRight, true); + } + } From 6b53586a1ba79120b32d562c52544a1419b5db69 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 12:04:37 +0100 Subject: [PATCH 36/93] add clearleft --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 10 ++++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index ad891cc..6a93698 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -15,6 +15,7 @@ public class Logic { public boolean clearedAbove = false; public boolean clearedBelow = false; public boolean clearedRight = false; + public boolean clearedLeft = false; public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); @@ -114,4 +115,13 @@ public class Logic { } } + + public void clearLeft(Coordinate shot) { + target = matchfield.getLeft(shot); + + if (target.getState() == Coordinate.EMPTY) { + clearedLeft = true; + } + + } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 79a8ab8..622eebb 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -175,4 +175,20 @@ class LogicTest { assertEquals(logic.clearedRight, true); } + @Test + void testClearLeft() { + Logic logic = new Logic(); + Matchfield matchfield; + Coordinate shot = new Coordinate(2, 2); + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + logic.setMatchfield(matchfield); + logic.setLastShot(shot); + matchfield.getField(shot).setState(Coordinate.EMPTY); + + logic.clearLeft(shot); + + assertEquals(logic.clearedLeft, true); + } } From cdf4b5b14da17c6ea271fcd8279386846fa39c8b Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 12:09:47 +0100 Subject: [PATCH 37/93] Refactoring: cleared-booleans are private now (add getters for Test) --- .../java/de/tims/fleetstorm/ai/Logic.java | 24 +++++++++++++++---- .../java/de/tims/fleetstorm/ai/LogicTest.java | 8 +++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 6a93698..2c198d4 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -12,10 +12,10 @@ public class Logic { private Coordinate lastShot; public Coordinate target; private boolean foundShip = false; - public boolean clearedAbove = false; - public boolean clearedBelow = false; - public boolean clearedRight = false; - public boolean clearedLeft = false; + private boolean clearedAbove = false; + private boolean clearedBelow = false; + private boolean clearedRight = false; + private boolean clearedLeft = false; public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); @@ -124,4 +124,20 @@ public class Logic { } } + + public boolean getClearedAbove() { + return this.clearedAbove; + } + + public boolean getClearedBelow() { + return this.clearedBelow; + } + + public boolean getClearedRight() { + return this.clearedRight; + } + + public boolean getClearedLeft() { + return this.clearedLeft; + } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 622eebb..dbce6ea 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -138,7 +138,7 @@ class LogicTest { logic.clearAbove(shot); - assertEquals(logic.clearedAbove, true); + assertEquals(logic.getClearedAbove(), true); } @Test @@ -155,7 +155,7 @@ class LogicTest { logic.clearBelow(shot); - assertEquals(logic.clearedBelow, true); + assertEquals(logic.getClearedBelow(), true); } @Test @@ -172,7 +172,7 @@ class LogicTest { logic.clearRight(shot); - assertEquals(logic.clearedRight, true); + assertEquals(logic.getClearedRight(), true); } @Test @@ -189,6 +189,6 @@ class LogicTest { logic.clearLeft(shot); - assertEquals(logic.clearedLeft, true); + assertEquals(logic.getClearedLeft(), true); } } From 7db470de84a2212c4aad900fe499915f1ecaa274 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 12:53:07 +0100 Subject: [PATCH 38/93] Refactoring: Coordinate target is private now --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 2c198d4..b05558e 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -10,7 +10,7 @@ public class Logic { private Matchfield matchfield; private Coordinate lastShot; - public Coordinate target; + private Coordinate target; private boolean foundShip = false; private boolean clearedAbove = false; private boolean clearedBelow = false; From ed7474228a523caf7150def32a72f491f7eb4412 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 13:28:10 +0100 Subject: [PATCH 39/93] Refactoring: add Constructor to set DefaultValues --- .../java/de/tims/fleetstorm/ai/Logic.java | 33 +++++++++++-------- .../java/de/tims/fleetstorm/ai/LogicTest.java | 24 ++++++++++---- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index b05558e..98db585 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -11,20 +11,28 @@ public class Logic { private Matchfield matchfield; private Coordinate lastShot; private Coordinate target; - private boolean foundShip = false; - private boolean clearedAbove = false; - private boolean clearedBelow = false; - private boolean clearedRight = false; - private boolean clearedLeft = false; + private boolean foundShip; + private boolean clearedAbove; + private boolean clearedBelow; + private boolean clearedRight; + private boolean clearedLeft; + + public Logic() { + foundShip = false; + clearedAbove = false; + clearedBelow = false; + clearedRight = false; + clearedLeft = false; + + } + + public Coordinate chooseField(ArrayList coordinates) { - public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); - for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) { - for (int y = 0; y < Math.sqrt(this.matchfield.getSize()); y++) { - if (this.matchfield.getState(x, y) == Coordinate.EMPTY - || this.matchfield.getState(x, y) == Coordinate.SHIP) { - possibleFields.add(new Coordinate(x, y)); - } + + for (int i = 0; i < coordinates.size(); i++) { + if (coordinates.get(i).getState() != Coordinate.SHOT && coordinates.get(i).getState() != Coordinate.HIT) { + possibleFields.add(coordinates.get(i)); } } Random randy = new Random(); @@ -68,7 +76,6 @@ public class Logic { foundShip = true; } - System.out.println("Do this"); } public void setFoundShip(boolean b) { diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index dbce6ea..b89fe84 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -21,7 +21,9 @@ class LogicTest { matchfield = new Matchfield(size); matchfield.createMatchfield(); logic.setMatchfield(matchfield); - Coordinate calcResult = logic.chooseField(); + ArrayList everySecondField = logic.getEverySecondField(); + + Coordinate calcResult = logic.chooseField(everySecondField); assertNotNull(calcResult); } @@ -34,14 +36,13 @@ class LogicTest { matchfield.createMatchfield(); logic.setMatchfield(matchfield); - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - matchfield.setState(x, y, Coordinate.SHOT); - } + ArrayList everySecondField = logic.getEverySecondField(); + for (int i = 0; i < everySecondField.size(); i++) { + everySecondField.get(i).setState(Coordinate.SHOT); } matchfield.setState(2, 2, Coordinate.EMPTY); - Coordinate choosenField = logic.chooseField(); + Coordinate choosenField = logic.chooseField(everySecondField); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); } @@ -191,4 +192,15 @@ class LogicTest { assertEquals(logic.getClearedLeft(), true); } + + @Test + void testConstructor() { + Logic logic = new Logic(); + + assertEquals(logic.getFoundShip(), false); + assertEquals(logic.getClearedAbove(), false); + assertEquals(logic.getClearedBelow(), false); + assertEquals(logic.getClearedLeft(), false); + assertEquals(logic.getClearedRight(), false); + } } From 3ddb76e0abcddb5dc2284afbb58d8fd0bc5a74d1 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 13:40:49 +0100 Subject: [PATCH 40/93] add Setter for ClearedAbove --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 9 +++++++++ src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 98db585..42e9415 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -147,4 +147,13 @@ public class Logic { public boolean getClearedLeft() { return this.clearedLeft; } + + public void sinkShip() { + // TODO Auto-generated method stub + + } + + public void setClearedAbove(boolean b) { + this.clearedAbove = b; + } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index b89fe84..1e74ea3 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -203,4 +203,13 @@ class LogicTest { assertEquals(logic.getClearedLeft(), false); assertEquals(logic.getClearedRight(), false); } + + @Test + void testSetterClearedAbove() { + Logic logic = new Logic(); + logic.setClearedAbove(true); + + assertEquals(logic.getClearedAbove(), true); + } + } From 6aa384e17330aac1ee08ecd91491684b2f6b3bf9 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 13:41:49 +0100 Subject: [PATCH 41/93] add Setter for ClearedBelow --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 5 +++++ src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 42e9415..9387e67 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -156,4 +156,9 @@ public class Logic { public void setClearedAbove(boolean b) { this.clearedAbove = b; } + + public void setClearedBelow(boolean b) { + this.clearedBelow = b; + + } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 1e74ea3..52eff70 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -212,4 +212,12 @@ class LogicTest { assertEquals(logic.getClearedAbove(), true); } + @Test + void testSetterClearedBelow() { + Logic logic = new Logic(); + logic.setClearedBelow(true); + + assertEquals(logic.getClearedBelow(), true); + } + } From 908beb15c81c33776518863556f4fc1310abaaef Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 13:43:48 +0100 Subject: [PATCH 42/93] add Setter for ClearedRight --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 3 +++ src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 9387e67..74bdffe 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -159,6 +159,9 @@ public class Logic { public void setClearedBelow(boolean b) { this.clearedBelow = b; + } + public void setClearedRight(boolean b) { + this.clearedRight = b; } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 52eff70..f3272da 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -220,4 +220,12 @@ class LogicTest { assertEquals(logic.getClearedBelow(), true); } + @Test + void testSetterClearedRight() { + Logic logic = new Logic(); + logic.setClearedRight(true); + + assertEquals(logic.getClearedRight(), true); + } + } From 8a169d52de1a924973b870be87ca2248f5698ee4 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 13:45:06 +0100 Subject: [PATCH 43/93] add Setter for ClearedLeft --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 5 +++++ src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 74bdffe..11cbb85 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -164,4 +164,9 @@ public class Logic { public void setClearedRight(boolean b) { this.clearedRight = b; } + + public void setClearedLeft(boolean b) { + this.clearedLeft = b; + + } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index f3272da..3171cb2 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -228,4 +228,12 @@ class LogicTest { assertEquals(logic.getClearedRight(), true); } + @Test + void testSetterClearedLeft() { + Logic logic = new Logic(); + logic.setClearedLeft(true); + + assertEquals(logic.getClearedLeft(), true); + } + } From 3364446cd68bdbdb59cfe4b8ec1775f6e989e610 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 13:49:40 +0100 Subject: [PATCH 44/93] add sinkShip --- .../java/de/tims/fleetstorm/ai/Logic.java | 8 +++++++- .../java/de/tims/fleetstorm/ai/LogicTest.java | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 11cbb85..d8b2e9b 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -149,7 +149,13 @@ public class Logic { } public void sinkShip() { - // TODO Auto-generated method stub + if (foundShip && clearedAbove && clearedBelow && clearedLeft && clearedRight) { + this.foundShip = false; + this.clearedAbove = false; + this.clearedBelow = false; + this.clearedLeft = false; + this.clearedRight = false; + } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 3171cb2..5579585 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -236,4 +236,23 @@ class LogicTest { assertEquals(logic.getClearedLeft(), true); } + @Test + void testSinkShip() { + Logic logic = new Logic(); + + logic.setFoundShip(true); + logic.setClearedAbove(true); + logic.setClearedBelow(true); + logic.setClearedRight(true); + logic.setClearedLeft(true); + + logic.sinkShip(); + + assertEquals(logic.getFoundShip(), false); + assertEquals(logic.getClearedAbove(), false); + assertEquals(logic.getClearedBelow(), false); + assertEquals(logic.getClearedLeft(), false); + assertEquals(logic.getClearedRight(), false); + + } } From ac9183ea27b58936f7cf095cdae19cd901a75646 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 14:18:53 +0100 Subject: [PATCH 45/93] overload getState() in Matchfield --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 1 - .../de/tims/fleetstorm/matchfield/Matchfield.java | 4 ++++ src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 1 + .../matchfield/MatchfieldCreationTest.java | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index d8b2e9b..ebab5c5 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -173,6 +173,5 @@ public class Logic { public void setClearedLeft(boolean b) { this.clearedLeft = b; - } } diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 45b08c4..99c5a8d 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -32,6 +32,10 @@ public class Matchfield { this.matchfield[x][y].setState(state); } + public void setState(Coordinate coordinate, int state) { + this.matchfield[coordinate.getX()][coordinate.getY()].setState(state); + } + public Coordinate getField(Coordinate coordinate) { return matchfield[coordinate.getX()][coordinate.getY()]; } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 5579585..1154eb5 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -255,4 +255,5 @@ class LogicTest { assertEquals(logic.getClearedRight(), false); } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 6db8108..7a6d218 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -1,6 +1,7 @@ package de.tims.fleetstorm.matchfield; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.stream.Stream; @@ -127,4 +128,17 @@ class MatchfieldCreationTest { matchfield.getField(5, 4)), Arguments.of("below from (5/0) - should be null", matchfield, new Coordinate(5, 0), null)); } + + @Test + void testsetStateOverloaded() { + + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + int x = 3; + int y = 3; + matchfield.setState(new Coordinate(x, y), Coordinate.SHIP); + int result = matchfield.getState(x, y); + assertEquals(result, Coordinate.SHIP); + + } } From af264751e5b1d5a6a94561c656d983c29d1f9c0b Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 14:56:55 +0100 Subject: [PATCH 46/93] chooseField returns correekt Field cae:Up has Ship --- .../java/de/tims/fleetstorm/ai/Logic.java | 18 ++++++++++--- .../java/de/tims/fleetstorm/ai/LogicTest.java | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index ebab5c5..67a96ff 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -28,16 +28,28 @@ public class Logic { public Coordinate chooseField(ArrayList coordinates) { - ArrayList possibleFields = new ArrayList(); + if (foundShip) { + if (!clearedAbove) { + + target = matchfield.getAbove(lastShot); + + return target; + } + } + // If ship is sinked + ArrayList possibleFields = new ArrayList(); for (int i = 0; i < coordinates.size(); i++) { if (coordinates.get(i).getState() != Coordinate.SHOT && coordinates.get(i).getState() != Coordinate.HIT) { possibleFields.add(coordinates.get(i)); } } Random randy = new Random(); - - return possibleFields.get(randy.nextInt(possibleFields.size())); + lastShot = possibleFields.get(randy.nextInt(possibleFields.size())); + if (lastShot.getState() == Coordinate.SHIP) { + foundShip = true; + } + return lastShot; } public ArrayList getEverySecondField() { diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 1154eb5..1d1df19 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -1,12 +1,17 @@ package de.tims.fleetstorm.ai; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.ArrayList; +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; import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; @@ -256,4 +261,25 @@ class LogicTest { } + @ParameterizedTest(name = "choose the right Coordinate after Hit") + @MethodSource("getCorrectNext") + void testNextChooseFieldAfterHit(String testName, Coordinate center, Coordinate expectedResult) { + Logic logic = new Logic(); + int size = 4; + Matchfield matchfield = new Matchfield(size); + matchfield.createMatchfield(); + matchfield.setState(center, Coordinate.HIT); + matchfield.setState(expectedResult, Coordinate.HIT); + logic.setMatchfield(matchfield); + logic.setLastShot(center); + logic.setFoundShip(true); + + Coordinate result = logic.chooseField(logic.getEverySecondField()); + assertThat(result).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream getCorrectNext() { + return Stream + .of(Arguments.of("center (2/2) - target should be (2,3)", new Coordinate(2, 2), new Coordinate(2, 3))); + } } From d5bd72a53cc15d0755c12e0fba465b245a116a86 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 16:24:48 +0100 Subject: [PATCH 47/93] Refactoring: testChooseFieldAboveAfterHit --- .../java/de/tims/fleetstorm/ai/Logic.java | 10 ++++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 20 ++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 67a96ff..5534407 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -46,6 +46,7 @@ public class Logic { } Random randy = new Random(); lastShot = possibleFields.get(randy.nextInt(possibleFields.size())); + target = possibleFields.get(randy.nextInt(possibleFields.size())); if (lastShot.getState() == Coordinate.SHIP) { foundShip = true; } @@ -186,4 +187,13 @@ public class Logic { public void setClearedLeft(boolean b) { this.clearedLeft = b; } + + public Coordinate getTarget() { + return target; + } + + public void setTarget(Coordinate target) { + this.target = target; + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 1d1df19..46acee3 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -1,17 +1,12 @@ package de.tims.fleetstorm.ai; -import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.ArrayList; -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; import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; @@ -90,7 +85,6 @@ class LogicTest { Coordinate result = logic.getLastShot(); assertEquals(result, expectedResult); - } @Test @@ -261,25 +255,23 @@ class LogicTest { } - @ParameterizedTest(name = "choose the right Coordinate after Hit") - @MethodSource("getCorrectNext") - void testNextChooseFieldAfterHit(String testName, Coordinate center, Coordinate expectedResult) { + @Test + void testChooseFieldAboveAfterHit() { Logic logic = new Logic(); int size = 4; + Coordinate center = new Coordinate(2, 2); + Coordinate expectedResult = new Coordinate(2, 3); Matchfield matchfield = new Matchfield(size); matchfield.createMatchfield(); matchfield.setState(center, Coordinate.HIT); matchfield.setState(expectedResult, Coordinate.HIT); logic.setMatchfield(matchfield); logic.setLastShot(center); + logic.setTarget(center); logic.setFoundShip(true); Coordinate result = logic.chooseField(logic.getEverySecondField()); - assertThat(result).describedAs(testName).isEqualTo(expectedResult); + assertEquals(result, expectedResult); } - static Stream getCorrectNext() { - return Stream - .of(Arguments.of("center (2/2) - target should be (2,3)", new Coordinate(2, 2), new Coordinate(2, 3))); - } } From fbdade30e3050884145f0456a274be3864755a21 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 16:26:37 +0100 Subject: [PATCH 48/93] chooseField returns correekt Field case:Down has Ship --- .../java/de/tims/fleetstorm/ai/Logic.java | 6 +++++- .../java/de/tims/fleetstorm/ai/LogicTest.java | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 5534407..a7525aa 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -31,8 +31,12 @@ public class Logic { if (foundShip) { if (!clearedAbove) { - target = matchfield.getAbove(lastShot); + if (target.equals(lastShot)) { + target = matchfield.getAbove(lastShot); + } else { + target = matchfield.getBelow(lastShot); + } return target; } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 46acee3..1e8b47c 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -274,4 +274,24 @@ class LogicTest { assertEquals(result, expectedResult); } + @Test + void testChooseFieldBelowAfterHit() { + Logic logic = new Logic(); + int size = 4; + Coordinate center = new Coordinate(2, 2); + Coordinate empty = new Coordinate(2, 3); + Coordinate expectedResult = new Coordinate(2, 1); + Matchfield matchfield = new Matchfield(size); + matchfield.createMatchfield(); + matchfield.setState(center, Coordinate.HIT); + matchfield.setState(empty, Coordinate.EMPTY); + matchfield.setState(expectedResult, Coordinate.HIT); + logic.setMatchfield(matchfield); + logic.setLastShot(center); + logic.setTarget(empty); + logic.setFoundShip(true); + + Coordinate result = logic.chooseField(logic.getEverySecondField()); + assertEquals(result, expectedResult); + } } From 1fd251655cbd92731b6da5b83f332da6d07c654f Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 16:43:21 +0100 Subject: [PATCH 49/93] Fix: chooseField makes way more sense now --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 15 +++++++++++---- .../java/de/tims/fleetstorm/ai/LogicTest.java | 7 +++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index a7525aa..f4e6ffd 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -30,12 +30,19 @@ public class Logic { if (foundShip) { if (!clearedAbove) { + target = matchfield.getAbove(target); + if (target.getState() == Coordinate.EMPTY) { + clearedAbove = true; + } + return target; + } - if (target.equals(lastShot)) { + target = lastShot; - target = matchfield.getAbove(lastShot); - } else { - target = matchfield.getBelow(lastShot); + if (!clearedBelow) { + target = matchfield.getBelow(target); + if (target.getState() == Coordinate.EMPTY) { + clearedBelow = true; } return target; } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 1e8b47c..cd6461f 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -288,10 +288,13 @@ class LogicTest { matchfield.setState(expectedResult, Coordinate.HIT); logic.setMatchfield(matchfield); logic.setLastShot(center); - logic.setTarget(empty); + logic.setTarget(center); logic.setFoundShip(true); - Coordinate result = logic.chooseField(logic.getEverySecondField()); + logic.chooseField(logic.getEverySecondField()); // first Shot + Coordinate result = logic.chooseField(logic.getEverySecondField()); // second Shot + assertEquals(result, expectedResult); } + } From f7c0a6da9c18cc7a5a71aa51e1d72ffadfc7a013 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 16:50:25 +0100 Subject: [PATCH 50/93] small Refactoring: chooseFieldBelowAfterHit --- src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index cd6461f..1662c6c 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -279,12 +279,12 @@ class LogicTest { Logic logic = new Logic(); int size = 4; Coordinate center = new Coordinate(2, 2); - Coordinate empty = new Coordinate(2, 3); + // Coordinate empty = new Coordinate(2, 3); Coordinate expectedResult = new Coordinate(2, 1); Matchfield matchfield = new Matchfield(size); matchfield.createMatchfield(); matchfield.setState(center, Coordinate.HIT); - matchfield.setState(empty, Coordinate.EMPTY); + // matchfield.setState(empty, Coordinate.EMPTY); matchfield.setState(expectedResult, Coordinate.HIT); logic.setMatchfield(matchfield); logic.setLastShot(center); From d983b436421cafaa31803c116723d9732cd8426b Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 16:53:13 +0100 Subject: [PATCH 51/93] chooseField returns correekt Field case:Right has Ship --- .../java/de/tims/fleetstorm/ai/Logic.java | 11 ++++++++- .../java/de/tims/fleetstorm/ai/LogicTest.java | 23 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index f4e6ffd..6ee6bd3 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -46,6 +46,16 @@ public class Logic { } return target; } + + target = lastShot; + + if (!clearedRight) { + target = matchfield.getRight(target); + if (target.getState() == Coordinate.EMPTY) { + clearedRight = true; + } + return target; + } } // If ship is sinked @@ -180,7 +190,6 @@ public class Logic { this.clearedLeft = false; this.clearedRight = false; } - } public void setClearedAbove(boolean b) { diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 1662c6c..fdf9d6f 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -279,12 +279,10 @@ class LogicTest { Logic logic = new Logic(); int size = 4; Coordinate center = new Coordinate(2, 2); - // Coordinate empty = new Coordinate(2, 3); Coordinate expectedResult = new Coordinate(2, 1); Matchfield matchfield = new Matchfield(size); matchfield.createMatchfield(); matchfield.setState(center, Coordinate.HIT); - // matchfield.setState(empty, Coordinate.EMPTY); matchfield.setState(expectedResult, Coordinate.HIT); logic.setMatchfield(matchfield); logic.setLastShot(center); @@ -297,4 +295,25 @@ class LogicTest { assertEquals(result, expectedResult); } + @Test + void testChooseFieldRightAfterHit() { + Logic logic = new Logic(); + int size = 4; + Coordinate center = new Coordinate(2, 2); + Coordinate expectedResult = new Coordinate(3, 2); + Matchfield matchfield = new Matchfield(size); + matchfield.createMatchfield(); + matchfield.setState(center, Coordinate.HIT); + matchfield.setState(expectedResult, Coordinate.HIT); + logic.setMatchfield(matchfield); + logic.setLastShot(center); + logic.setTarget(center); + logic.setFoundShip(true); + + logic.chooseField(logic.getEverySecondField()); // first Shot + logic.chooseField(logic.getEverySecondField()); // second Shot + Coordinate result = logic.chooseField(logic.getEverySecondField()); + + assertEquals(result, expectedResult); + } } From 4a02c0b7cc2ac100b222f59b4ee643bd052087e5 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 16:55:28 +0100 Subject: [PATCH 52/93] chooseField returns correekt Field case:Left has Ship --- .../java/de/tims/fleetstorm/ai/Logic.java | 10 ++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 6ee6bd3..d65227b 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -56,6 +56,16 @@ public class Logic { } return target; } + + target = lastShot; + + if (!clearedLeft) { + target = matchfield.getLeft(target); + if (target.getState() == Coordinate.EMPTY) { + clearedLeft = true; + } + return target; + } } // If ship is sinked diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index fdf9d6f..ac4e835 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -316,4 +316,27 @@ class LogicTest { assertEquals(result, expectedResult); } + + @Test + void testChooseFieldLeftAfterHit() { + Logic logic = new Logic(); + int size = 4; + Coordinate center = new Coordinate(2, 2); + Coordinate expectedResult = new Coordinate(1, 2); + Matchfield matchfield = new Matchfield(size); + matchfield.createMatchfield(); + matchfield.setState(center, Coordinate.HIT); + matchfield.setState(expectedResult, Coordinate.HIT); + logic.setMatchfield(matchfield); + logic.setLastShot(center); + logic.setTarget(center); + logic.setFoundShip(true); + + logic.chooseField(logic.getEverySecondField()); // first Shot + logic.chooseField(logic.getEverySecondField()); // second Shot + logic.chooseField(logic.getEverySecondField()); // third Shot + Coordinate result = logic.chooseField(logic.getEverySecondField()); + + assertEquals(result, expectedResult); + } } From 2022df66824dadc1083f2f2926b377de53ecdc1e Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 17:05:19 +0100 Subject: [PATCH 53/93] restrucered logic-Class --- .../java/de/tims/fleetstorm/ai/Logic.java | 108 +++++++----------- 1 file changed, 44 insertions(+), 64 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index d65227b..36a3cc4 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -68,7 +68,8 @@ public class Logic { } } - // If ship is sinked + sinkShip(); + ArrayList possibleFields = new ArrayList(); for (int i = 0; i < coordinates.size(); i++) { if (coordinates.get(i).getState() != Coordinate.SHOT && coordinates.get(i).getState() != Coordinate.HIT) { @@ -93,137 +94,116 @@ public class Logic { } } } - return out; } - public void setLastShot(Coordinate coordinate) { - lastShot = this.matchfield.getField(coordinate); - - } - - public Coordinate getLastShot() { - return lastShot; - } - - public void setMatchfield(Matchfield matchfield) { - this.matchfield = matchfield; - - } - - public Matchfield getMatchfield() { - return matchfield; - } - public void findShip() { if (lastShot.getState() == Coordinate.HIT) { - foundShip = true; } } - public void setFoundShip(boolean b) { - this.foundShip = b; - } - - public boolean getFoundShip() { - // TODO Auto-generated method stub - return this.foundShip; - } - - public void checkAround(Coordinate coordinate) { - // TODO Auto-generated method stub - + public void sinkShip() { + if (foundShip && clearedAbove && clearedBelow && clearedLeft && clearedRight) { + this.foundShip = false; + this.clearedAbove = false; + this.clearedBelow = false; + this.clearedLeft = false; + this.clearedRight = false; + } } public void clearAbove(Coordinate shot) { target = matchfield.getAbove(shot); - if (target.getState() == Coordinate.EMPTY) { clearedAbove = true; } } - private void shot() { - // TODO Auto-generated method stub - - } - public void clearBelow(Coordinate shot) { target = matchfield.getBelow(shot); - if (target.getState() == Coordinate.EMPTY) { clearedBelow = true; } - } public void clearRight(Coordinate shot) { target = matchfield.getRight(shot); - if (target.getState() == Coordinate.EMPTY) { clearedRight = true; } - } public void clearLeft(Coordinate shot) { target = matchfield.getLeft(shot); - if (target.getState() == Coordinate.EMPTY) { clearedLeft = true; } - } - public boolean getClearedAbove() { - return this.clearedAbove; - } + // Getter And Setter - public boolean getClearedBelow() { - return this.clearedBelow; + public void setLastShot(Coordinate coordinate) { + lastShot = this.matchfield.getField(coordinate); } - public boolean getClearedRight() { - return this.clearedRight; + public Coordinate getLastShot() { + return lastShot; } - public boolean getClearedLeft() { - return this.clearedLeft; + public void setFoundShip(boolean b) { + this.foundShip = b; } - public void sinkShip() { - if (foundShip && clearedAbove && clearedBelow && clearedLeft && clearedRight) { - this.foundShip = false; - this.clearedAbove = false; - this.clearedBelow = false; - this.clearedLeft = false; - this.clearedRight = false; - } + public boolean getFoundShip() { + return this.foundShip; } public void setClearedAbove(boolean b) { this.clearedAbove = b; } + public boolean getClearedAbove() { + return this.clearedAbove; + } + public void setClearedBelow(boolean b) { this.clearedBelow = b; } + public boolean getClearedBelow() { + return this.clearedBelow; + } + public void setClearedRight(boolean b) { this.clearedRight = b; } + public boolean getClearedRight() { + return this.clearedRight; + } + public void setClearedLeft(boolean b) { this.clearedLeft = b; } - public Coordinate getTarget() { - return target; + public boolean getClearedLeft() { + return this.clearedLeft; } public void setTarget(Coordinate target) { this.target = target; } + public Coordinate getTarget() { + return target; + } + + public void setMatchfield(Matchfield matchfield) { + this.matchfield = matchfield; + } + + public Matchfield getMatchfield() { + return matchfield; + } } From 3c1e3d19e53a2742b2fcf9cb5208af8b25b5abb7 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 17:29:16 +0100 Subject: [PATCH 54/93] Refactoring: chooseField doesent need Params now --- .../java/de/tims/fleetstorm/ai/Logic.java | 13 +++++++--- .../java/de/tims/fleetstorm/ai/LogicTest.java | 25 ++++++++++--------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 36a3cc4..7accfde 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -9,6 +9,7 @@ import de.tims.fleetstorm.matchfield.Matchfield; public class Logic { private Matchfield matchfield; + private ArrayList everySecondField; private Coordinate lastShot; private Coordinate target; private boolean foundShip; @@ -26,7 +27,7 @@ public class Logic { } - public Coordinate chooseField(ArrayList coordinates) { + public Coordinate chooseField() { if (foundShip) { if (!clearedAbove) { @@ -47,6 +48,8 @@ public class Logic { return target; } + // if() + target = lastShot; if (!clearedRight) { @@ -71,9 +74,10 @@ public class Logic { sinkShip(); ArrayList possibleFields = new ArrayList(); - for (int i = 0; i < coordinates.size(); i++) { - if (coordinates.get(i).getState() != Coordinate.SHOT && coordinates.get(i).getState() != Coordinate.HIT) { - possibleFields.add(coordinates.get(i)); + for (int i = 0; i < everySecondField.size(); i++) { + if (everySecondField.get(i).getState() != Coordinate.SHOT + && everySecondField.get(i).getState() != Coordinate.HIT) { + possibleFields.add(everySecondField.get(i)); } } Random randy = new Random(); @@ -201,6 +205,7 @@ public class Logic { public void setMatchfield(Matchfield matchfield) { this.matchfield = matchfield; + this.everySecondField = getEverySecondField(); } public Matchfield getMatchfield() { diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index ac4e835..f1d44b3 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -23,7 +23,7 @@ class LogicTest { logic.setMatchfield(matchfield); ArrayList everySecondField = logic.getEverySecondField(); - Coordinate calcResult = logic.chooseField(everySecondField); + Coordinate calcResult = logic.chooseField(); assertNotNull(calcResult); } @@ -42,7 +42,7 @@ class LogicTest { } matchfield.setState(2, 2, Coordinate.EMPTY); - Coordinate choosenField = logic.chooseField(everySecondField); + Coordinate choosenField = logic.chooseField(); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); } @@ -270,7 +270,7 @@ class LogicTest { logic.setTarget(center); logic.setFoundShip(true); - Coordinate result = logic.chooseField(logic.getEverySecondField()); + Coordinate result = logic.chooseField(); assertEquals(result, expectedResult); } @@ -289,8 +289,8 @@ class LogicTest { logic.setTarget(center); logic.setFoundShip(true); - logic.chooseField(logic.getEverySecondField()); // first Shot - Coordinate result = logic.chooseField(logic.getEverySecondField()); // second Shot + logic.chooseField(); // first Shot + Coordinate result = logic.chooseField(); // second Shot assertEquals(result, expectedResult); } @@ -310,9 +310,9 @@ class LogicTest { logic.setTarget(center); logic.setFoundShip(true); - logic.chooseField(logic.getEverySecondField()); // first Shot - logic.chooseField(logic.getEverySecondField()); // second Shot - Coordinate result = logic.chooseField(logic.getEverySecondField()); + logic.chooseField(); // first Shot + logic.chooseField(); // second Shot + Coordinate result = logic.chooseField(); assertEquals(result, expectedResult); } @@ -332,11 +332,12 @@ class LogicTest { logic.setTarget(center); logic.setFoundShip(true); - logic.chooseField(logic.getEverySecondField()); // first Shot - logic.chooseField(logic.getEverySecondField()); // second Shot - logic.chooseField(logic.getEverySecondField()); // third Shot - Coordinate result = logic.chooseField(logic.getEverySecondField()); + logic.chooseField(); // first Shot + logic.chooseField(); // second Shot + logic.chooseField(); // third Shot + Coordinate result = logic.chooseField(); assertEquals(result, expectedResult); } + } From 79a7556286964000cd4798cc2b1a5f21d673c9aa Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Sat, 15 Jan 2022 12:12:16 +0100 Subject: [PATCH 55/93] if ShipIsOnYAxis, ai doesent check XAxis --- .../java/de/tims/fleetstorm/ai/Logic.java | 46 ++++++++++++------ .../fleetstorm/matchfield/Coordinate.java | 4 ++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 47 ++++++++++++++++--- 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 7accfde..e5c923d 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -10,6 +10,7 @@ public class Logic { private Matchfield matchfield; private ArrayList everySecondField; + private Coordinate lastShot; private Coordinate target; private boolean foundShip; @@ -24,55 +25,57 @@ public class Logic { clearedBelow = false; clearedRight = false; clearedLeft = false; - } public Coordinate chooseField() { + Coordinate out; if (foundShip) { + // Clear Y-Axis if (!clearedAbove) { target = matchfield.getAbove(target); + out = target; if (target.getState() == Coordinate.EMPTY) { clearedAbove = true; + target = lastShot; } - return target; + return out; } - target = lastShot; - if (!clearedBelow) { target = matchfield.getBelow(target); + out = target; if (target.getState() == Coordinate.EMPTY) { clearedBelow = true; + target = lastShot; } - return target; + isShipOnYAxis(); + return out; } - // if() - - target = lastShot; - + // Clear x-Axis if (!clearedRight) { target = matchfield.getRight(target); + out = target; if (target.getState() == Coordinate.EMPTY) { clearedRight = true; + target = lastShot; } - return target; + return out; } - target = lastShot; - if (!clearedLeft) { target = matchfield.getLeft(target); + out = target; if (target.getState() == Coordinate.EMPTY) { clearedLeft = true; + target = lastShot; } - return target; + sinkShip(); + return out; } } - sinkShip(); - ArrayList possibleFields = new ArrayList(); for (int i = 0; i < everySecondField.size(); i++) { if (everySecondField.get(i).getState() != Coordinate.SHOT @@ -89,6 +92,15 @@ public class Logic { return lastShot; } + public void isShipOnYAxis() { + if ((clearedAbove && clearedBelow) && ((matchfield.getAbove(lastShot).getState() == Coordinate.HIT) + || (matchfield.getBelow(lastShot).getState() == Coordinate.HIT))) { + clearedLeft = true; + clearedRight = true; + sinkShip(); + } + } + public ArrayList getEverySecondField() { ArrayList out = new ArrayList(); for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) { @@ -147,6 +159,10 @@ public class Logic { // Getter And Setter + public void setEverySecondField(ArrayList everySecondField) { + this.everySecondField = everySecondField; + } + public void setLastShot(Coordinate coordinate) { lastShot = this.matchfield.getField(coordinate); } diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index 705c77c..a11c666 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -40,4 +40,8 @@ public class Coordinate { return this.x == ((Coordinate) obj).getX() && this.y == ((Coordinate) obj).getY(); } + public void print() { + System.out.println("X = " + this.x + ", Y = " + this.y + ", State = " + this.state); + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index f1d44b3..79935b5 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -21,7 +21,7 @@ class LogicTest { matchfield = new Matchfield(size); matchfield.createMatchfield(); logic.setMatchfield(matchfield); - ArrayList everySecondField = logic.getEverySecondField(); + // ArrayList everySecondField = logic.getEverySecondField(); Coordinate calcResult = logic.chooseField(); assertNotNull(calcResult); @@ -41,9 +41,9 @@ class LogicTest { everySecondField.get(i).setState(Coordinate.SHOT); } matchfield.setState(2, 2, Coordinate.EMPTY); + logic.setEverySecondField(everySecondField); - Coordinate choosenField = logic.chooseField(); - int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); + int calcState = logic.chooseField().getState(); assertNotEquals(calcState, Coordinate.SHOT); } @@ -288,8 +288,7 @@ class LogicTest { logic.setLastShot(center); logic.setTarget(center); logic.setFoundShip(true); - - logic.chooseField(); // first Shot + logic.chooseField();// first Shot Coordinate result = logic.chooseField(); // second Shot assertEquals(result, expectedResult); @@ -334,10 +333,44 @@ class LogicTest { logic.chooseField(); // first Shot logic.chooseField(); // second Shot - logic.chooseField(); // third Shot + logic.chooseField(); // third Shot Coordinate Coordinate result = logic.chooseField(); - assertEquals(result, expectedResult); } + @Test + void testShipIsOnYAxis() { + int size = 10; + Logic logic = new Logic(); + Matchfield matchfield = new Matchfield(size); + Coordinate s1 = new Coordinate(5, 4); + Coordinate s2 = new Coordinate(5, 5); + Coordinate s3 = new Coordinate(5, 6); + + matchfield.createMatchfield(); + matchfield.setState(s1, Coordinate.SHIP); + matchfield.setState(s2, Coordinate.HIT); + matchfield.setState(s3, Coordinate.SHIP); + + logic.setMatchfield(matchfield); + logic.setLastShot(s2); + logic.setTarget(s2); + logic.setFoundShip(true); + + logic.chooseField(); // First shot (s3) + logic.getMatchfield().setState(s3, Coordinate.HIT); // + logic.chooseField(); // Second Shot + logic.getMatchfield().setState(new Coordinate(5, 7), Coordinate.SHOT); + + logic.chooseField(); // Third Shot (s1) + logic.getMatchfield().setState(s1, Coordinate.HIT); // + logic.chooseField(); // fourth Shot + + assertEquals(logic.getFoundShip(), false); + assertEquals(logic.getClearedAbove(), false); + assertEquals(logic.getClearedBelow(), false); + assertEquals(logic.getClearedLeft(), false); + assertEquals(logic.getClearedRight(), false); + + } } From 557984b581229b14451bfdc726b581817544d882 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 12 Jan 2022 11:41:14 +0100 Subject: [PATCH 56/93] Added GameManager and check if GameState is correct after start function call --- .../java/de/tims/fleetstorm/GameManager.java | 19 +++++++++++++++++ .../de/tims/fleetstorm/GameManagerTest.java | 21 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/main/java/de/tims/fleetstorm/GameManager.java create mode 100644 src/test/java/de/tims/fleetstorm/GameManagerTest.java diff --git a/src/main/java/de/tims/fleetstorm/GameManager.java b/src/main/java/de/tims/fleetstorm/GameManager.java new file mode 100644 index 0000000..8f682cc --- /dev/null +++ b/src/main/java/de/tims/fleetstorm/GameManager.java @@ -0,0 +1,19 @@ +package de.tims.fleetstorm; + +public class GameManager { + + private int gameState; + + public static final int PREPARATION = 1; + public static final int RUNNING = 2; + public static final int OVER = 3; + + public void start() { + this.gameState = GameManager.PREPARATION; + } + + public int getGameState() { + return gameState; + } + +} diff --git a/src/test/java/de/tims/fleetstorm/GameManagerTest.java b/src/test/java/de/tims/fleetstorm/GameManagerTest.java new file mode 100644 index 0000000..d90e85f --- /dev/null +++ b/src/test/java/de/tims/fleetstorm/GameManagerTest.java @@ -0,0 +1,21 @@ +package de.tims.fleetstorm; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class GameManagerTest { + + GameManager gameManager = new GameManager(); + + @Test + void testIfGameStateIsPreparationAfterStart() { + gameManager.start(); + int expectedState = GameManager.PREPARATION; + + int calculatedState = gameManager.getGameState(); + + assertEquals(expectedState, calculatedState); + } + +} From 3211601bd5c1e5f1d281e622b763cc1948c8360b Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 12 Jan 2022 11:55:09 +0100 Subject: [PATCH 57/93] Added nextMove() function to check wheter the player or AI has move --- .../java/de/tims/fleetstorm/GameManager.java | 15 ++++++++++++ .../de/tims/fleetstorm/GameManagerTest.java | 24 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/GameManager.java b/src/main/java/de/tims/fleetstorm/GameManager.java index 8f682cc..9a8a59d 100644 --- a/src/main/java/de/tims/fleetstorm/GameManager.java +++ b/src/main/java/de/tims/fleetstorm/GameManager.java @@ -1,8 +1,13 @@ package de.tims.fleetstorm; +import de.tims.fleetstorm.matchfield.Matchfield; + public class GameManager { private int gameState; + private Matchfield matchfield; + private int matchfieldSize = 10; + private boolean playerMove = true; public static final int PREPARATION = 1; public static final int RUNNING = 2; @@ -10,10 +15,20 @@ public class GameManager { public void start() { this.gameState = GameManager.PREPARATION; + + this.matchfield = new Matchfield(matchfieldSize); + } + + public void nextMove() { + this.playerMove = !this.playerMove; } public int getGameState() { return gameState; } + public boolean isPlayerMove() { + return playerMove; + } + } diff --git a/src/test/java/de/tims/fleetstorm/GameManagerTest.java b/src/test/java/de/tims/fleetstorm/GameManagerTest.java index d90e85f..5c329c7 100644 --- a/src/test/java/de/tims/fleetstorm/GameManagerTest.java +++ b/src/test/java/de/tims/fleetstorm/GameManagerTest.java @@ -1,16 +1,23 @@ package de.tims.fleetstorm; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class GameManagerTest { GameManager gameManager = new GameManager(); + @BeforeEach + void setup() { + gameManager.start(); + } + @Test void testIfGameStateIsPreparationAfterStart() { - gameManager.start(); int expectedState = GameManager.PREPARATION; int calculatedState = gameManager.getGameState(); @@ -18,4 +25,19 @@ class GameManagerTest { assertEquals(expectedState, calculatedState); } + @Test + void testNextMoveIsNotPlayer() { + + // 20 tries => every even move is a player move + for (int i = 0; i < 20; i++) { + gameManager.nextMove(); + boolean calculatedResult = gameManager.isPlayerMove(); + if (i % 2 == 0) { + assertFalse(calculatedResult); + } else { + assertTrue(calculatedResult); + } + } + } + } From 936a563422f1268775e778d644c2fdabb3fd4628 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 12 Jan 2022 13:18:57 +0100 Subject: [PATCH 58/93] Created gui with minimal elements --- src/main/java/de/tims/fleetstorm/gui/GUI.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/de/tims/fleetstorm/gui/GUI.java diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/GUI.java new file mode 100644 index 0000000..70d3333 --- /dev/null +++ b/src/main/java/de/tims/fleetstorm/gui/GUI.java @@ -0,0 +1,63 @@ +package de.tims.fleetstorm.gui; + +import java.awt.Color; +import java.awt.Font; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.MatteBorder; + +public class GUI extends JPanel { + + private ArrayList fields; + + public GUI() { + this.fields = new ArrayList<>(); + setSize(640, 480); + + JPanel fieldWrapper = new JPanel(); + fieldWrapper.setBounds(10, 11, 362, 458); + fieldWrapper.setLayout(null); + + JPanel infoWrapper = new JPanel(); + infoWrapper.setBounds(382, 11, 248, 458); + + JLabel hasMoveLabel = new JLabel("Am Zug:"); + hasMoveLabel.setFont(new Font("Tahoma", Font.BOLD, 11)); + infoWrapper.add(hasMoveLabel); + + JLabel hasMove = new JLabel("Spieler"); + infoWrapper.add(hasMove); + setLayout(null); + add(fieldWrapper); + add(infoWrapper); + + int gap = 30; + int width = 28; + + for (int x = 0; x < 10; x++) { + for (int y = 0; y < 10; y++) { + JPanel field = new JPanel(); + field.setBounds(gap + x * width, gap + y * width, width, width); + field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); + fields.add(field); + fieldWrapper.add(field); + } + } + + } + + /** + * This function is only for testing + */ + public static void main(String[] args) { + JFrame frame = new JFrame("Test GUI"); + GUI gui = new GUI(); + frame.setContentPane(gui); + frame.setSize(640, 480); + frame.setResizable(true); + frame.setVisible(true); + } +} From b937245b635d34c4889bbf337655f854b9912f44 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 11:43:58 +0100 Subject: [PATCH 59/93] Added game info sidebar --- src/main/java/de/tims/fleetstorm/gui/GUI.java | 129 +++++++++++++++++- 1 file changed, 122 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/GUI.java index 70d3333..10f7d8a 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GUI.java +++ b/src/main/java/de/tims/fleetstorm/gui/GUI.java @@ -2,6 +2,7 @@ package de.tims.fleetstorm.gui; import java.awt.Color; import java.awt.Font; +import java.awt.GridLayout; import java.util.ArrayList; import javax.swing.JFrame; @@ -23,17 +24,131 @@ public class GUI extends JPanel { JPanel infoWrapper = new JPanel(); infoWrapper.setBounds(382, 11, 248, 458); - - JLabel hasMoveLabel = new JLabel("Am Zug:"); - hasMoveLabel.setFont(new Font("Tahoma", Font.BOLD, 11)); - infoWrapper.add(hasMoveLabel); - - JLabel hasMove = new JLabel("Spieler"); - infoWrapper.add(hasMove); + infoWrapper.setLayout(null); setLayout(null); add(fieldWrapper); add(infoWrapper); + JPanel yourFleet = new JPanel(); + yourFleet.setBounds(10, 110, 228, 93); + infoWrapper.add(yourFleet); + yourFleet.setLayout(null); + + JPanel panel_1 = new JPanel(); + panel_1.setBounds(0, 0, 228, 27); + yourFleet.add(panel_1); + panel_1.setLayout(null); + + JLabel ownFleetHeading = new JLabel("Deine Flotte:"); + ownFleetHeading.setBounds(0, 0, 228, 17); + panel_1.add(ownFleetHeading); + ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + + JPanel panel_2 = new JPanel(); + panel_2.setBounds(0, 26, 228, 67); + yourFleet.add(panel_2); + panel_2.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel own2ShipLabel = new JLabel("1x2 Schiff"); + panel_2.add(own2ShipLabel); + + JLabel own2Ship = new JLabel("OK"); + panel_2.add(own2Ship); + + JLabel own3ShipLabel = new JLabel("1x3 Schiff"); + panel_2.add(own3ShipLabel); + + JLabel own3Ship = new JLabel("OK"); + panel_2.add(own3Ship); + + JLabel own4ShipLabel = new JLabel("1x4 Schiff"); + panel_2.add(own4ShipLabel); + + JLabel own4Ship = new JLabel("OK"); + panel_2.add(own4Ship); + + JLabel own5ShipLabel = new JLabel("1x5 Schiff"); + panel_2.add(own5ShipLabel); + + JLabel own5Ship = new JLabel("OK"); + panel_2.add(own5Ship); + + JPanel enemyFleet = new JPanel(); + enemyFleet.setLayout(null); + enemyFleet.setBounds(10, 237, 228, 93); + infoWrapper.add(enemyFleet); + + JPanel panel_1_1 = new JPanel(); + panel_1_1.setLayout(null); + panel_1_1.setBounds(0, 0, 228, 27); + enemyFleet.add(panel_1_1); + + JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:"); + enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + enemyFleetHeading.setBounds(0, 0, 228, 17); + panel_1_1.add(enemyFleetHeading); + + JPanel panel_2_1 = new JPanel(); + panel_2_1.setBounds(0, 26, 228, 67); + enemyFleet.add(panel_2_1); + panel_2_1.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel enemy2ShipLabel = new JLabel("1x2 Schiff"); + panel_2_1.add(enemy2ShipLabel); + + JLabel enemy2Ship = new JLabel("OK"); + panel_2_1.add(enemy2Ship); + + JLabel enemy3ShipLabel = new JLabel("1x3 Schiff"); + panel_2_1.add(enemy3ShipLabel); + + JLabel enemy3Ship = new JLabel("OK"); + panel_2_1.add(enemy3Ship); + + JLabel enemy4ShipLabel = new JLabel("1x4 Schiff"); + panel_2_1.add(enemy4ShipLabel); + + JLabel enemy4Ship = new JLabel("OK"); + panel_2_1.add(enemy4Ship); + + JLabel enemy5ShipLabel = new JLabel("1x5 Schiff"); + panel_2_1.add(enemy5ShipLabel); + + JLabel enemy5Ship = new JLabel("OK"); + panel_2_1.add(enemy5Ship); + + JPanel infos = new JPanel(); + infos.setLayout(null); + infos.setBounds(10, 11, 228, 68); + infoWrapper.add(infos); + + JPanel panel_1_2 = new JPanel(); + panel_1_2.setLayout(null); + panel_1_2.setBounds(0, 0, 228, 27); + infos.add(panel_1_2); + + JLabel gameInfoHeading = new JLabel("Spielinfos"); + gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + gameInfoHeading.setBounds(0, 0, 228, 17); + panel_1_2.add(gameInfoHeading); + + JPanel panel_2_2 = new JPanel(); + panel_2_2.setBounds(0, 26, 228, 31); + infos.add(panel_2_2); + panel_2_2.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel matchfieldLabel = new JLabel("Spielfeld"); + panel_2_2.add(matchfieldLabel); + + JLabel matchfieldValue = new JLabel("Eigenes"); + panel_2_2.add(matchfieldValue); + + JLabel moveLabel = new JLabel("Am Zug"); + panel_2_2.add(moveLabel); + + JLabel moveValue = new JLabel("Du"); + panel_2_2.add(moveValue); + int gap = 30; int width = 28; From 46d51b2b8bdaa02317800aa23219cfcedd8a12bb Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 11:55:05 +0100 Subject: [PATCH 60/93] Refactored GUI and generated Getters --- src/main/java/de/tims/fleetstorm/gui/GUI.java | 183 ++++++++++++------ 1 file changed, 119 insertions(+), 64 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/GUI.java index 10f7d8a..5bd0a99 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GUI.java +++ b/src/main/java/de/tims/fleetstorm/gui/GUI.java @@ -13,8 +13,18 @@ import javax.swing.border.MatteBorder; public class GUI extends JPanel { private ArrayList fields; - - public GUI() { + private JLabel matchfieldValue; + private JLabel moveValue; + private JLabel own2Ship; + private JLabel own3Ship; + private JLabel own4Ship; + private JLabel own5Ship; + private JLabel enemy2Ship; + private JLabel enemy3Ship; + private JLabel enemy4Ship; + private JLabel enemy5Ship; + + public GUI(int gapToFrameBorder, int fieldWidth, int spaceBetween) { this.fields = new ArrayList<>(); setSize(640, 480); @@ -34,128 +44,129 @@ public class GUI extends JPanel { infoWrapper.add(yourFleet); yourFleet.setLayout(null); - JPanel panel_1 = new JPanel(); - panel_1.setBounds(0, 0, 228, 27); - yourFleet.add(panel_1); - panel_1.setLayout(null); + JPanel ownFleetHeadingPanel = new JPanel(); + ownFleetHeadingPanel.setBounds(0, 0, 228, 27); + yourFleet.add(ownFleetHeadingPanel); + ownFleetHeadingPanel.setLayout(null); JLabel ownFleetHeading = new JLabel("Deine Flotte:"); ownFleetHeading.setBounds(0, 0, 228, 17); - panel_1.add(ownFleetHeading); + ownFleetHeadingPanel.add(ownFleetHeading); ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); - JPanel panel_2 = new JPanel(); - panel_2.setBounds(0, 26, 228, 67); - yourFleet.add(panel_2); - panel_2.setLayout(new GridLayout(0, 2, 0, 3)); + JPanel ownFleetContentPanel = new JPanel(); + ownFleetContentPanel.setBounds(0, 26, 228, 67); + yourFleet.add(ownFleetContentPanel); + ownFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); JLabel own2ShipLabel = new JLabel("1x2 Schiff"); - panel_2.add(own2ShipLabel); + ownFleetContentPanel.add(own2ShipLabel); - JLabel own2Ship = new JLabel("OK"); - panel_2.add(own2Ship); + own2Ship = new JLabel("OK"); + ownFleetContentPanel.add(own2Ship); JLabel own3ShipLabel = new JLabel("1x3 Schiff"); - panel_2.add(own3ShipLabel); + ownFleetContentPanel.add(own3ShipLabel); - JLabel own3Ship = new JLabel("OK"); - panel_2.add(own3Ship); + own3Ship = new JLabel("OK"); + ownFleetContentPanel.add(own3Ship); JLabel own4ShipLabel = new JLabel("1x4 Schiff"); - panel_2.add(own4ShipLabel); + ownFleetContentPanel.add(own4ShipLabel); - JLabel own4Ship = new JLabel("OK"); - panel_2.add(own4Ship); + own4Ship = new JLabel("OK"); + ownFleetContentPanel.add(own4Ship); JLabel own5ShipLabel = new JLabel("1x5 Schiff"); - panel_2.add(own5ShipLabel); + ownFleetContentPanel.add(own5ShipLabel); - JLabel own5Ship = new JLabel("OK"); - panel_2.add(own5Ship); + own5Ship = new JLabel("OK"); + ownFleetContentPanel.add(own5Ship); JPanel enemyFleet = new JPanel(); enemyFleet.setLayout(null); enemyFleet.setBounds(10, 237, 228, 93); infoWrapper.add(enemyFleet); - JPanel panel_1_1 = new JPanel(); - panel_1_1.setLayout(null); - panel_1_1.setBounds(0, 0, 228, 27); - enemyFleet.add(panel_1_1); + JPanel enemyFleetHeadingPanel = new JPanel(); + enemyFleetHeadingPanel.setLayout(null); + enemyFleetHeadingPanel.setBounds(0, 0, 228, 27); + enemyFleet.add(enemyFleetHeadingPanel); JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:"); enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); enemyFleetHeading.setBounds(0, 0, 228, 17); - panel_1_1.add(enemyFleetHeading); + enemyFleetHeadingPanel.add(enemyFleetHeading); - JPanel panel_2_1 = new JPanel(); - panel_2_1.setBounds(0, 26, 228, 67); - enemyFleet.add(panel_2_1); - panel_2_1.setLayout(new GridLayout(0, 2, 0, 3)); + JPanel enemyFleetContentPanel = new JPanel(); + enemyFleetContentPanel.setBounds(0, 26, 228, 67); + enemyFleet.add(enemyFleetContentPanel); + enemyFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); JLabel enemy2ShipLabel = new JLabel("1x2 Schiff"); - panel_2_1.add(enemy2ShipLabel); + enemyFleetContentPanel.add(enemy2ShipLabel); - JLabel enemy2Ship = new JLabel("OK"); - panel_2_1.add(enemy2Ship); + enemy2Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy2Ship); JLabel enemy3ShipLabel = new JLabel("1x3 Schiff"); - panel_2_1.add(enemy3ShipLabel); + enemyFleetContentPanel.add(enemy3ShipLabel); - JLabel enemy3Ship = new JLabel("OK"); - panel_2_1.add(enemy3Ship); + enemy3Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy3Ship); JLabel enemy4ShipLabel = new JLabel("1x4 Schiff"); - panel_2_1.add(enemy4ShipLabel); + enemyFleetContentPanel.add(enemy4ShipLabel); - JLabel enemy4Ship = new JLabel("OK"); - panel_2_1.add(enemy4Ship); + enemy4Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy4Ship); JLabel enemy5ShipLabel = new JLabel("1x5 Schiff"); - panel_2_1.add(enemy5ShipLabel); + enemyFleetContentPanel.add(enemy5ShipLabel); - JLabel enemy5Ship = new JLabel("OK"); - panel_2_1.add(enemy5Ship); + enemy5Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy5Ship); JPanel infos = new JPanel(); infos.setLayout(null); infos.setBounds(10, 11, 228, 68); infoWrapper.add(infos); - JPanel panel_1_2 = new JPanel(); - panel_1_2.setLayout(null); - panel_1_2.setBounds(0, 0, 228, 27); - infos.add(panel_1_2); + JPanel gameInfoHeadingPanel = new JPanel(); + gameInfoHeadingPanel.setLayout(null); + gameInfoHeadingPanel.setBounds(0, 0, 228, 27); + infos.add(gameInfoHeadingPanel); JLabel gameInfoHeading = new JLabel("Spielinfos"); gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); gameInfoHeading.setBounds(0, 0, 228, 17); - panel_1_2.add(gameInfoHeading); + gameInfoHeadingPanel.add(gameInfoHeading); - JPanel panel_2_2 = new JPanel(); - panel_2_2.setBounds(0, 26, 228, 31); - infos.add(panel_2_2); - panel_2_2.setLayout(new GridLayout(0, 2, 0, 3)); + JPanel gameInfoContentPanel = new JPanel(); + gameInfoContentPanel.setBounds(0, 26, 228, 31); + infos.add(gameInfoContentPanel); + gameInfoContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); JLabel matchfieldLabel = new JLabel("Spielfeld"); - panel_2_2.add(matchfieldLabel); + gameInfoContentPanel.add(matchfieldLabel); - JLabel matchfieldValue = new JLabel("Eigenes"); - panel_2_2.add(matchfieldValue); + matchfieldValue = new JLabel("Eigenes"); + gameInfoContentPanel.add(matchfieldValue); JLabel moveLabel = new JLabel("Am Zug"); - panel_2_2.add(moveLabel); - - JLabel moveValue = new JLabel("Du"); - panel_2_2.add(moveValue); + gameInfoContentPanel.add(moveLabel); - int gap = 30; - int width = 28; + moveValue = new JLabel("Du"); + gameInfoContentPanel.add(moveValue); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { JPanel field = new JPanel(); - field.setBounds(gap + x * width, gap + y * width, width, width); + + int xPos = gapToFrameBorder + x * fieldWidth + (x * spaceBetween); + int yPos = gapToFrameBorder + y * fieldWidth + (y * spaceBetween); + + field.setBounds(xPos, yPos, fieldWidth, fieldWidth); field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); fields.add(field); fieldWrapper.add(field); @@ -164,12 +175,56 @@ public class GUI extends JPanel { } + public ArrayList getFields() { + return fields; + } + + public JLabel getMatchfieldValue() { + return matchfieldValue; + } + + public JLabel getMoveValue() { + return moveValue; + } + + public JLabel getOwn2Ship() { + return own2Ship; + } + + public JLabel getOwn3Ship() { + return own3Ship; + } + + public JLabel getOwn4Ship() { + return own4Ship; + } + + public JLabel getOwn5Ship() { + return own5Ship; + } + + public JLabel getEnemy2Ship() { + return enemy2Ship; + } + + public JLabel getEnemy3Ship() { + return enemy3Ship; + } + + public JLabel getEnemy4Ship() { + return enemy4Ship; + } + + public JLabel getEnemy5Ship() { + return enemy5Ship; + } + /** * This function is only for testing */ public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - GUI gui = new GUI(); + GUI gui = new GUI(15, 28, 1); frame.setContentPane(gui); frame.setSize(640, 480); frame.setResizable(true); From f36500d195900bbddab59dc2bb82c2a20a357bad Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 12:05:01 +0100 Subject: [PATCH 61/93] Added mouse listener to register click on matchfield --- src/main/java/de/tims/fleetstorm/gui/GUI.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/GUI.java index 5bd0a99..0a4083c 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GUI.java +++ b/src/main/java/de/tims/fleetstorm/gui/GUI.java @@ -3,6 +3,8 @@ package de.tims.fleetstorm.gui; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.util.ArrayList; import javax.swing.JFrame; @@ -170,6 +172,15 @@ public class GUI extends JPanel { field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); fields.add(field); fieldWrapper.add(field); + + field.addMouseListener(new MouseAdapter() { + + public void mouseClicked(MouseEvent e) { + field.setBackground(Color.RED); + field.revalidate(); + field.repaint(); + } + }); } } From c1cbf6a07774e9bf7e2a242eb4ecc62594dce617 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 12:13:29 +0100 Subject: [PATCH 62/93] United GameManager with GUI class (Refactoring) --- .../java/de/tims/fleetstorm/GameManager.java | 34 ----------------- .../fleetstorm/gui/{GUI.java => Logic.java} | 37 +++++++++++++++++-- .../LogicTest.java} | 18 +++++---- 3 files changed, 44 insertions(+), 45 deletions(-) delete mode 100644 src/main/java/de/tims/fleetstorm/GameManager.java rename src/main/java/de/tims/fleetstorm/gui/{GUI.java => Logic.java} (89%) rename src/test/java/de/tims/fleetstorm/{GameManagerTest.java => gui/LogicTest.java} (66%) diff --git a/src/main/java/de/tims/fleetstorm/GameManager.java b/src/main/java/de/tims/fleetstorm/GameManager.java deleted file mode 100644 index 9a8a59d..0000000 --- a/src/main/java/de/tims/fleetstorm/GameManager.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.tims.fleetstorm; - -import de.tims.fleetstorm.matchfield.Matchfield; - -public class GameManager { - - private int gameState; - private Matchfield matchfield; - private int matchfieldSize = 10; - private boolean playerMove = true; - - public static final int PREPARATION = 1; - public static final int RUNNING = 2; - public static final int OVER = 3; - - public void start() { - this.gameState = GameManager.PREPARATION; - - this.matchfield = new Matchfield(matchfieldSize); - } - - public void nextMove() { - this.playerMove = !this.playerMove; - } - - public int getGameState() { - return gameState; - } - - public boolean isPlayerMove() { - return playerMove; - } - -} diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/Logic.java similarity index 89% rename from src/main/java/de/tims/fleetstorm/gui/GUI.java rename to src/main/java/de/tims/fleetstorm/gui/Logic.java index 0a4083c..a014d0b 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GUI.java +++ b/src/main/java/de/tims/fleetstorm/gui/Logic.java @@ -12,8 +12,21 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.MatteBorder; -public class GUI extends JPanel { +import de.tims.fleetstorm.matchfield.Matchfield; +public class Logic extends JPanel { + + // GameManager stuff + private int gameState; + private Matchfield matchfield; + private int matchfieldSize = 10; + private boolean playerMove = true; + + public static final int PREPARATION = 1; + public static final int RUNNING = 2; + public static final int OVER = 3; + + // GUI stuff private ArrayList fields; private JLabel matchfieldValue; private JLabel moveValue; @@ -26,7 +39,7 @@ public class GUI extends JPanel { private JLabel enemy4Ship; private JLabel enemy5Ship; - public GUI(int gapToFrameBorder, int fieldWidth, int spaceBetween) { + public Logic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { this.fields = new ArrayList<>(); setSize(640, 480); @@ -235,10 +248,28 @@ public class GUI extends JPanel { */ public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - GUI gui = new GUI(15, 28, 1); + Logic gui = new Logic(15, 28, 1); frame.setContentPane(gui); frame.setSize(640, 480); frame.setResizable(true); frame.setVisible(true); } + + public void start() { + this.gameState = Logic.PREPARATION; + + this.matchfield = new Matchfield(matchfieldSize); + } + + public void nextMove() { + this.playerMove = !this.playerMove; + } + + public int getGameState() { + return gameState; + } + + public boolean isPlayerMove() { + return playerMove; + } } diff --git a/src/test/java/de/tims/fleetstorm/GameManagerTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java similarity index 66% rename from src/test/java/de/tims/fleetstorm/GameManagerTest.java rename to src/test/java/de/tims/fleetstorm/gui/LogicTest.java index 5c329c7..5d5e47c 100644 --- a/src/test/java/de/tims/fleetstorm/GameManagerTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -1,4 +1,4 @@ -package de.tims.fleetstorm; +package de.tims.fleetstorm.gui; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -7,20 +7,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -class GameManagerTest { +import de.tims.fleetstorm.gui.Logic; - GameManager gameManager = new GameManager(); +class LogicTest { + + Logic guiLogic = new Logic(0, 0, 0); @BeforeEach void setup() { - gameManager.start(); + guiLogic.start(); } @Test void testIfGameStateIsPreparationAfterStart() { - int expectedState = GameManager.PREPARATION; + int expectedState = Logic.PREPARATION; - int calculatedState = gameManager.getGameState(); + int calculatedState = guiLogic.getGameState(); assertEquals(expectedState, calculatedState); } @@ -30,8 +32,8 @@ class GameManagerTest { // 20 tries => every even move is a player move for (int i = 0; i < 20; i++) { - gameManager.nextMove(); - boolean calculatedResult = gameManager.isPlayerMove(); + guiLogic.nextMove(); + boolean calculatedResult = guiLogic.isPlayerMove(); if (i % 2 == 0) { assertFalse(calculatedResult); } else { From 871f5fb860d9e1be92961956fd3394fdd6d8bed5 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 12:35:21 +0100 Subject: [PATCH 63/93] United AI with GameLogic --- .../gui/{Logic.java => GameLogic.java} | 17 +++++++++++++---- .../java/de/tims/fleetstorm/gui/LogicTest.java | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) rename src/main/java/de/tims/fleetstorm/gui/{Logic.java => GameLogic.java} (92%) diff --git a/src/main/java/de/tims/fleetstorm/gui/Logic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java similarity index 92% rename from src/main/java/de/tims/fleetstorm/gui/Logic.java rename to src/main/java/de/tims/fleetstorm/gui/GameLogic.java index a014d0b..d00b7a9 100644 --- a/src/main/java/de/tims/fleetstorm/gui/Logic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -12,15 +12,18 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.MatteBorder; +import de.tims.fleetstorm.ai.Logic; import de.tims.fleetstorm.matchfield.Matchfield; -public class Logic extends JPanel { +public class GameLogic extends JPanel { // GameManager stuff private int gameState; private Matchfield matchfield; + private Matchfield enemyMatchfield; private int matchfieldSize = 10; private boolean playerMove = true; + private Logic aiLogic; public static final int PREPARATION = 1; public static final int RUNNING = 2; @@ -39,7 +42,7 @@ public class Logic extends JPanel { private JLabel enemy4Ship; private JLabel enemy5Ship; - public Logic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { + public GameLogic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { this.fields = new ArrayList<>(); setSize(640, 480); @@ -248,7 +251,7 @@ public class Logic extends JPanel { */ public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - Logic gui = new Logic(15, 28, 1); + GameLogic gui = new GameLogic(15, 28, 1); frame.setContentPane(gui); frame.setSize(640, 480); frame.setResizable(true); @@ -256,9 +259,15 @@ public class Logic extends JPanel { } public void start() { - this.gameState = Logic.PREPARATION; + this.gameState = GameLogic.PREPARATION; + // create player matchfield this.matchfield = new Matchfield(matchfieldSize); + + // create enemy matchfield and initialize AI (with player's matchfield) + this.enemyMatchfield = new Matchfield(matchfieldSize); + this.aiLogic = new Logic(); + this.aiLogic.setMatchfield(this.matchfield); } public void nextMove() { diff --git a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java index 5d5e47c..ee73c74 100644 --- a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -7,11 +7,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import de.tims.fleetstorm.gui.Logic; +import de.tims.fleetstorm.gui.GameLogic; class LogicTest { - Logic guiLogic = new Logic(0, 0, 0); + GameLogic guiLogic = new GameLogic(0, 0, 0); @BeforeEach void setup() { @@ -20,7 +20,7 @@ class LogicTest { @Test void testIfGameStateIsPreparationAfterStart() { - int expectedState = Logic.PREPARATION; + int expectedState = GameLogic.PREPARATION; int calculatedState = guiLogic.getGameState(); From 6de05cb1340a3ee612de9b03e49b614f930b4b0d Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 13:32:51 +0100 Subject: [PATCH 64/93] Implemented setShip() in Matchfield with first test case for 2x1 ship in direction 0 --- .../fleetstorm/matchfield/Matchfield.java | 8 ++++ .../matchfield/MatchfieldPositioningTest.java | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 99c5a8d..1d5bdb1 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -73,4 +73,12 @@ public class Matchfield { return matchfield[center.getX()][center.getY() - 1]; } + /** + * direction 0 => x axis (to the right) direction 1 => y axis (to the bottom) + */ + public void setShip(Coordinate coordinate, int length, int direction) { + this.setState(coordinate, Coordinate.SHIP); + this.setState(this.getRight(coordinate), Coordinate.SHIP); + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java new file mode 100644 index 0000000..22d6723 --- /dev/null +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java @@ -0,0 +1,38 @@ + +package de.tims.fleetstorm.matchfield; + +import static org.junit.Assert.assertTrue; + +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class MatchfieldPositioningTest { + + @ParameterizedTest(name = "ship was set on the correct positions") + @MethodSource("testShipPositioning") + void testMatchfieldShipSetHasCorrectPositions(String testName, int matchfieldSize, Coordinate coordinate, + int direction, int length, Coordinate[] coordinatesWithShip) { + Matchfield matchfield = new Matchfield(matchfieldSize); + matchfield.createMatchfield(); + + matchfield.setShip(coordinate, length, direction); + + int shipSetCounter = 0; + for (Coordinate toCheckCoordinate : coordinatesWithShip) { + if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP) + shipSetCounter++; + } + + assertTrue(shipSetCounter == length); + } + + static Stream testShipPositioning() { + Coordinate[] coordinatesWithShip00_2_0 = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 0) }; + + return Stream.of(Arguments.of("set ship from 0:0, length 2, direction 0", 10, new Coordinate(0, 0), 0, 2, + coordinatesWithShip00_2_0)); + } +} From d47d765c9272a590ceaed89a6ab942f4e7f5f0d9 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 13:34:18 +0100 Subject: [PATCH 65/93] Added second test case for setShip() (3x1, direction 0) --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 7 +++++-- .../matchfield/MatchfieldPositioningTest.java | 11 ++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 1d5bdb1..cf50512 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -77,8 +77,11 @@ public class Matchfield { * direction 0 => x axis (to the right) direction 1 => y axis (to the bottom) */ public void setShip(Coordinate coordinate, int length, int direction) { - this.setState(coordinate, Coordinate.SHIP); - this.setState(this.getRight(coordinate), Coordinate.SHIP); + + for (int i = 0; i < length; i++) { + this.getField(coordinate).setState(Coordinate.SHIP); + coordinate = this.getRight(coordinate); + } } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java index 22d6723..9f7c02c 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java @@ -31,8 +31,13 @@ class MatchfieldPositioningTest { static Stream testShipPositioning() { Coordinate[] coordinatesWithShip00_2_0 = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 0) }; - - return Stream.of(Arguments.of("set ship from 0:0, length 2, direction 0", 10, new Coordinate(0, 0), 0, 2, - coordinatesWithShip00_2_0)); + Coordinate[] coordinatesWithShip01_3_0 = new Coordinate[] { new Coordinate(0, 1), new Coordinate(1, 1), + new Coordinate(2, 1) }; + + return Stream.of( + Arguments.of("set ship from 0:0, length 2, direction 0", 10, new Coordinate(0, 0), 0, 2, + coordinatesWithShip00_2_0), + Arguments.of("set ship from 0:1, length 3, direction 0", 10, new Coordinate(0, 1), 0, 3, + coordinatesWithShip01_3_0)); } } From 4426627a5b19c00576e59c118434b817ab1e4b94 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 13:35:12 +0100 Subject: [PATCH 66/93] Added third test case for setShip() (4x1, direction 1) --- src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java | 5 ++++- .../fleetstorm/matchfield/MatchfieldPositioningTest.java | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index cf50512..726666d 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -80,7 +80,10 @@ public class Matchfield { for (int i = 0; i < length; i++) { this.getField(coordinate).setState(Coordinate.SHIP); - coordinate = this.getRight(coordinate); + if (direction == 0) + coordinate = this.getRight(coordinate); + if (direction == 1) + coordinate = this.getAbove(coordinate); } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java index 9f7c02c..72b4359 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java @@ -33,11 +33,15 @@ class MatchfieldPositioningTest { Coordinate[] coordinatesWithShip00_2_0 = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 0) }; Coordinate[] coordinatesWithShip01_3_0 = new Coordinate[] { new Coordinate(0, 1), new Coordinate(1, 1), new Coordinate(2, 1) }; + Coordinate[] coordinatesWithShip02_4_1 = new Coordinate[] { new Coordinate(0, 2), new Coordinate(0, 3), + new Coordinate(0, 4), new Coordinate(0, 5) }; return Stream.of( Arguments.of("set ship from 0:0, length 2, direction 0", 10, new Coordinate(0, 0), 0, 2, coordinatesWithShip00_2_0), Arguments.of("set ship from 0:1, length 3, direction 0", 10, new Coordinate(0, 1), 0, 3, - coordinatesWithShip01_3_0)); + coordinatesWithShip01_3_0), + Arguments.of("set ship from 0:2, length 4, direction 1", 10, new Coordinate(0, 2), 1, 4, + coordinatesWithShip02_4_1)); } } From 246ecfa6bdc8ed28e525d138ef1204fd97b273ed Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 13:37:03 +0100 Subject: [PATCH 67/93] Added test case for invalid coordinates on setShip (2x1, direction 0) --- .../tims/fleetstorm/matchfield/Matchfield.java | 7 ++++++- .../matchfield/MatchfieldPositioningTest.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 726666d..bfc29fc 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -76,7 +76,7 @@ public class Matchfield { /** * direction 0 => x axis (to the right) direction 1 => y axis (to the bottom) */ - public void setShip(Coordinate coordinate, int length, int direction) { + public boolean setShip(Coordinate coordinate, int length, int direction) { for (int i = 0; i < length; i++) { this.getField(coordinate).setState(Coordinate.SHIP); @@ -84,7 +84,12 @@ public class Matchfield { coordinate = this.getRight(coordinate); if (direction == 1) coordinate = this.getAbove(coordinate); + + if (coordinate == null) + return false; } + + return true; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java index 72b4359..e969003 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java @@ -1,6 +1,7 @@ package de.tims.fleetstorm.matchfield; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.stream.Stream; @@ -44,4 +45,21 @@ class MatchfieldPositioningTest { Arguments.of("set ship from 0:2, length 4, direction 1", 10, new Coordinate(0, 2), 1, 4, coordinatesWithShip02_4_1)); } + + @ParameterizedTest(name = "ship positioning on invalid or valid coordinates") + @MethodSource("testShipPositioningFailed") + void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, int matchfieldSize, Coordinate coordinate, + int direction, int length) { + Matchfield matchfield = new Matchfield(matchfieldSize); + matchfield.createMatchfield(); + + boolean calculatedResult = matchfield.setShip(coordinate, length, direction); + assertFalse(calculatedResult); + } + + static Stream testShipPositioningFailed() { + + return Stream.of( + Arguments.of("invalid ship position from 9:0, length 2, direction 0", 10, new Coordinate(9, 0), 0, 2)); + } } From 65ad3513055b54b1dcde2c35e9e84f31fa5bf1a7 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 13:38:51 +0100 Subject: [PATCH 68/93] Added second test case for invalid coordinates on setShip (5x1, direction 1) --- .../matchfield/MatchfieldPositioningTest.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java index e969003..2fb254a 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java @@ -49,17 +49,24 @@ class MatchfieldPositioningTest { @ParameterizedTest(name = "ship positioning on invalid or valid coordinates") @MethodSource("testShipPositioningFailed") void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, int matchfieldSize, Coordinate coordinate, - int direction, int length) { + int direction, int length, boolean expectedResult) { Matchfield matchfield = new Matchfield(matchfieldSize); matchfield.createMatchfield(); boolean calculatedResult = matchfield.setShip(coordinate, length, direction); - assertFalse(calculatedResult); + + if (expectedResult) + assertTrue(calculatedResult); + + if (!expectedResult) + assertFalse(calculatedResult); } static Stream testShipPositioningFailed() { - return Stream.of( - Arguments.of("invalid ship position from 9:0, length 2, direction 0", 10, new Coordinate(9, 0), 0, 2)); + Arguments.of("invalid ship position from 9:0, length 2, direction 0", 10, new Coordinate(9, 0), 0, 2, + false), + Arguments.of("valid ship position from 8:0, length 5, direction 1", 10, new Coordinate(9, 0), 1, 5, + true)); } } From 86e79f0a49e894817ffb3f90e3bcac52a3b80f68 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 13:39:41 +0100 Subject: [PATCH 69/93] Implemented test if ship is already on position in shipSet --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 7 ++++++- .../matchfield/MatchfieldPositioningTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index bfc29fc..f9fcd5e 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -79,7 +79,12 @@ public class Matchfield { public boolean setShip(Coordinate coordinate, int length, int direction) { for (int i = 0; i < length; i++) { - this.getField(coordinate).setState(Coordinate.SHIP); + + Coordinate field = this.getField(coordinate); + if (field.getState() == Coordinate.SHIP) + return false; + + field.setState(Coordinate.SHIP); if (direction == 0) coordinate = this.getRight(coordinate); if (direction == 1) diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java index 2fb254a..6ad6bb7 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue; 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; @@ -69,4 +70,16 @@ class MatchfieldPositioningTest { Arguments.of("valid ship position from 8:0, length 5, direction 1", 10, new Coordinate(9, 0), 1, 5, true)); } + + @Test + void testShipPositionAlreadySetWithShip() { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + matchfield.setShip(new Coordinate(0, 0), 5, 1); + assertFalse(matchfield.setShip(new Coordinate(0, 1), 5, 1)); + + assertTrue(matchfield.setShip(new Coordinate(1, 1), 4, 0)); + assertFalse(matchfield.setShip(new Coordinate(4, 1), 2, 1)); + } } From 7038a4c607685de72392d63b326897e5885761aa Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:08:23 +0100 Subject: [PATCH 70/93] Added default ship positioning to GUI --- .../de/tims/fleetstorm/gui/GameLogic.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index d00b7a9..0d6d14f 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -13,6 +13,7 @@ import javax.swing.JPanel; import javax.swing.border.MatteBorder; import de.tims.fleetstorm.ai.Logic; +import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; public class GameLogic extends JPanel { @@ -186,6 +187,7 @@ public class GameLogic extends JPanel { field.setBounds(xPos, yPos, fieldWidth, fieldWidth); field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); + field.setName(x + ":" + y); fields.add(field); fieldWrapper.add(field); @@ -246,16 +248,38 @@ public class GameLogic extends JPanel { return enemy5Ship; } + private void updateField(boolean player, Matchfield matchfield) { + for (JPanel coordinatePanel : this.fields) { + String panelName = coordinatePanel.getName(); + String[] panelNameSplit = panelName.split(":"); + int coordX = Integer.parseInt(panelNameSplit[0]); + int coordY = Integer.parseInt(panelNameSplit[1]); + + matchfield.getField(coordX, coordY).print(); + int state = matchfield.getField(coordX, coordY).getState(); + + if (state == Coordinate.SHIP) + coordinatePanel.setBackground(Color.GREEN); + + coordinatePanel.revalidate(); + coordinatePanel.repaint(); + } + } + /** * This function is only for testing */ + private static GameLogic gui; + public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - GameLogic gui = new GameLogic(15, 28, 1); + gui = new GameLogic(15, 28, 1); frame.setContentPane(gui); frame.setSize(640, 480); frame.setResizable(true); frame.setVisible(true); + + gui.start(); } public void start() { @@ -263,11 +287,24 @@ public class GameLogic extends JPanel { // create player matchfield this.matchfield = new Matchfield(matchfieldSize); + this.matchfield.createMatchfield(); + this.matchfield.setShip(new Coordinate(4, 6), 2, 0); + this.matchfield.setShip(new Coordinate(1, 3), 3, 0); + this.matchfield.setShip(new Coordinate(9, 1), 4, 1); + this.matchfield.setShip(new Coordinate(0, 0), 5, 0); // create enemy matchfield and initialize AI (with player's matchfield) this.enemyMatchfield = new Matchfield(matchfieldSize); + this.enemyMatchfield.createMatchfield(); + this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); + this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); + this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); + this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); this.aiLogic = new Logic(); this.aiLogic.setMatchfield(this.matchfield); + + // show player own matchfield + gui.updateField(true, this.matchfield); } public void nextMove() { From 94ff308379218f2c297f7476cc75fea6b1f8cf88 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:18:50 +0100 Subject: [PATCH 71/93] Implemented first move 'animation' for GUI --- .../de/tims/fleetstorm/gui/GameLogic.java | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 0d6d14f..f8ba163 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -23,7 +23,7 @@ public class GameLogic extends JPanel { private Matchfield matchfield; private Matchfield enemyMatchfield; private int matchfieldSize = 10; - private boolean playerMove = true; + private boolean playerMove; private Logic aiLogic; public static final int PREPARATION = 1; @@ -248,18 +248,32 @@ public class GameLogic extends JPanel { return enemy5Ship; } - private void updateField(boolean player, Matchfield matchfield) { + private void updateField(boolean playerMove, Matchfield matchfield) { for (JPanel coordinatePanel : this.fields) { String panelName = coordinatePanel.getName(); String[] panelNameSplit = panelName.split(":"); int coordX = Integer.parseInt(panelNameSplit[0]); int coordY = Integer.parseInt(panelNameSplit[1]); - matchfield.getField(coordX, coordY).print(); + // reset field + coordinatePanel.setBackground(null); + coordinatePanel.removeAll(); + int state = matchfield.getField(coordX, coordY).getState(); - if (state == Coordinate.SHIP) - coordinatePanel.setBackground(Color.GREEN); + switch (state) { + case Coordinate.SHIP: + if (playerMove) + coordinatePanel.setBackground(new Color(91, 58, 41)); + break; + case Coordinate.SHOT: + coordinatePanel.setBackground(new Color(0, 94, 184)); + break; + case Coordinate.HIT: + coordinatePanel.setBackground(new Color(0, 94, 184)); + coordinatePanel.add(new JLabel("x")); + break; + } coordinatePanel.revalidate(); coordinatePanel.repaint(); @@ -283,6 +297,7 @@ public class GameLogic extends JPanel { } public void start() { + this.playerMove = true; this.gameState = GameLogic.PREPARATION; // create player matchfield @@ -304,11 +319,27 @@ public class GameLogic extends JPanel { this.aiLogic.setMatchfield(this.matchfield); // show player own matchfield - gui.updateField(true, this.matchfield); + gui.updateField(this.playerMove, this.matchfield); + + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // enter game loop + this.nextMove(); } public void nextMove() { this.playerMove = !this.playerMove; + + if (this.playerMove) { + gui.updateField(this.playerMove, this.enemyMatchfield); + } else { + gui.updateField(this.playerMove, this.matchfield); + } + } public int getGameState() { From fff174937b7adfe84fe2e6806776f574f076db32 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:28:20 +0100 Subject: [PATCH 72/93] Refactoring: getCoordinateFromLabel() --- .../de/tims/fleetstorm/gui/GameLogic.java | 725 +++++++++--------- 1 file changed, 373 insertions(+), 352 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index f8ba163..9225309 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -1,352 +1,373 @@ -package de.tims.fleetstorm.gui; - -import java.awt.Color; -import java.awt.Font; -import java.awt.GridLayout; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.util.ArrayList; - -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.border.MatteBorder; - -import de.tims.fleetstorm.ai.Logic; -import de.tims.fleetstorm.matchfield.Coordinate; -import de.tims.fleetstorm.matchfield.Matchfield; - -public class GameLogic extends JPanel { - - // GameManager stuff - private int gameState; - private Matchfield matchfield; - private Matchfield enemyMatchfield; - private int matchfieldSize = 10; - private boolean playerMove; - private Logic aiLogic; - - public static final int PREPARATION = 1; - public static final int RUNNING = 2; - public static final int OVER = 3; - - // GUI stuff - private ArrayList fields; - private JLabel matchfieldValue; - private JLabel moveValue; - private JLabel own2Ship; - private JLabel own3Ship; - private JLabel own4Ship; - private JLabel own5Ship; - private JLabel enemy2Ship; - private JLabel enemy3Ship; - private JLabel enemy4Ship; - private JLabel enemy5Ship; - - public GameLogic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { - this.fields = new ArrayList<>(); - setSize(640, 480); - - JPanel fieldWrapper = new JPanel(); - fieldWrapper.setBounds(10, 11, 362, 458); - fieldWrapper.setLayout(null); - - JPanel infoWrapper = new JPanel(); - infoWrapper.setBounds(382, 11, 248, 458); - infoWrapper.setLayout(null); - setLayout(null); - add(fieldWrapper); - add(infoWrapper); - - JPanel yourFleet = new JPanel(); - yourFleet.setBounds(10, 110, 228, 93); - infoWrapper.add(yourFleet); - yourFleet.setLayout(null); - - JPanel ownFleetHeadingPanel = new JPanel(); - ownFleetHeadingPanel.setBounds(0, 0, 228, 27); - yourFleet.add(ownFleetHeadingPanel); - ownFleetHeadingPanel.setLayout(null); - - JLabel ownFleetHeading = new JLabel("Deine Flotte:"); - ownFleetHeading.setBounds(0, 0, 228, 17); - ownFleetHeadingPanel.add(ownFleetHeading); - ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); - - JPanel ownFleetContentPanel = new JPanel(); - ownFleetContentPanel.setBounds(0, 26, 228, 67); - yourFleet.add(ownFleetContentPanel); - ownFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); - - JLabel own2ShipLabel = new JLabel("1x2 Schiff"); - ownFleetContentPanel.add(own2ShipLabel); - - own2Ship = new JLabel("OK"); - ownFleetContentPanel.add(own2Ship); - - JLabel own3ShipLabel = new JLabel("1x3 Schiff"); - ownFleetContentPanel.add(own3ShipLabel); - - own3Ship = new JLabel("OK"); - ownFleetContentPanel.add(own3Ship); - - JLabel own4ShipLabel = new JLabel("1x4 Schiff"); - ownFleetContentPanel.add(own4ShipLabel); - - own4Ship = new JLabel("OK"); - ownFleetContentPanel.add(own4Ship); - - JLabel own5ShipLabel = new JLabel("1x5 Schiff"); - ownFleetContentPanel.add(own5ShipLabel); - - own5Ship = new JLabel("OK"); - ownFleetContentPanel.add(own5Ship); - - JPanel enemyFleet = new JPanel(); - enemyFleet.setLayout(null); - enemyFleet.setBounds(10, 237, 228, 93); - infoWrapper.add(enemyFleet); - - JPanel enemyFleetHeadingPanel = new JPanel(); - enemyFleetHeadingPanel.setLayout(null); - enemyFleetHeadingPanel.setBounds(0, 0, 228, 27); - enemyFleet.add(enemyFleetHeadingPanel); - - JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:"); - enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); - enemyFleetHeading.setBounds(0, 0, 228, 17); - enemyFleetHeadingPanel.add(enemyFleetHeading); - - JPanel enemyFleetContentPanel = new JPanel(); - enemyFleetContentPanel.setBounds(0, 26, 228, 67); - enemyFleet.add(enemyFleetContentPanel); - enemyFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); - - JLabel enemy2ShipLabel = new JLabel("1x2 Schiff"); - enemyFleetContentPanel.add(enemy2ShipLabel); - - enemy2Ship = new JLabel("OK"); - enemyFleetContentPanel.add(enemy2Ship); - - JLabel enemy3ShipLabel = new JLabel("1x3 Schiff"); - enemyFleetContentPanel.add(enemy3ShipLabel); - - enemy3Ship = new JLabel("OK"); - enemyFleetContentPanel.add(enemy3Ship); - - JLabel enemy4ShipLabel = new JLabel("1x4 Schiff"); - enemyFleetContentPanel.add(enemy4ShipLabel); - - enemy4Ship = new JLabel("OK"); - enemyFleetContentPanel.add(enemy4Ship); - - JLabel enemy5ShipLabel = new JLabel("1x5 Schiff"); - enemyFleetContentPanel.add(enemy5ShipLabel); - - enemy5Ship = new JLabel("OK"); - enemyFleetContentPanel.add(enemy5Ship); - - JPanel infos = new JPanel(); - infos.setLayout(null); - infos.setBounds(10, 11, 228, 68); - infoWrapper.add(infos); - - JPanel gameInfoHeadingPanel = new JPanel(); - gameInfoHeadingPanel.setLayout(null); - gameInfoHeadingPanel.setBounds(0, 0, 228, 27); - infos.add(gameInfoHeadingPanel); - - JLabel gameInfoHeading = new JLabel("Spielinfos"); - gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); - gameInfoHeading.setBounds(0, 0, 228, 17); - gameInfoHeadingPanel.add(gameInfoHeading); - - JPanel gameInfoContentPanel = new JPanel(); - gameInfoContentPanel.setBounds(0, 26, 228, 31); - infos.add(gameInfoContentPanel); - gameInfoContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); - - JLabel matchfieldLabel = new JLabel("Spielfeld"); - gameInfoContentPanel.add(matchfieldLabel); - - matchfieldValue = new JLabel("Eigenes"); - gameInfoContentPanel.add(matchfieldValue); - - JLabel moveLabel = new JLabel("Am Zug"); - gameInfoContentPanel.add(moveLabel); - - moveValue = new JLabel("Du"); - gameInfoContentPanel.add(moveValue); - - for (int x = 0; x < 10; x++) { - for (int y = 0; y < 10; y++) { - JPanel field = new JPanel(); - - int xPos = gapToFrameBorder + x * fieldWidth + (x * spaceBetween); - int yPos = gapToFrameBorder + y * fieldWidth + (y * spaceBetween); - - field.setBounds(xPos, yPos, fieldWidth, fieldWidth); - field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); - field.setName(x + ":" + y); - fields.add(field); - fieldWrapper.add(field); - - field.addMouseListener(new MouseAdapter() { - - public void mouseClicked(MouseEvent e) { - field.setBackground(Color.RED); - field.revalidate(); - field.repaint(); - } - }); - } - } - - } - - public ArrayList getFields() { - return fields; - } - - public JLabel getMatchfieldValue() { - return matchfieldValue; - } - - public JLabel getMoveValue() { - return moveValue; - } - - public JLabel getOwn2Ship() { - return own2Ship; - } - - public JLabel getOwn3Ship() { - return own3Ship; - } - - public JLabel getOwn4Ship() { - return own4Ship; - } - - public JLabel getOwn5Ship() { - return own5Ship; - } - - public JLabel getEnemy2Ship() { - return enemy2Ship; - } - - public JLabel getEnemy3Ship() { - return enemy3Ship; - } - - public JLabel getEnemy4Ship() { - return enemy4Ship; - } - - public JLabel getEnemy5Ship() { - return enemy5Ship; - } - - private void updateField(boolean playerMove, Matchfield matchfield) { - for (JPanel coordinatePanel : this.fields) { - String panelName = coordinatePanel.getName(); - String[] panelNameSplit = panelName.split(":"); - int coordX = Integer.parseInt(panelNameSplit[0]); - int coordY = Integer.parseInt(panelNameSplit[1]); - - // reset field - coordinatePanel.setBackground(null); - coordinatePanel.removeAll(); - - int state = matchfield.getField(coordX, coordY).getState(); - - switch (state) { - case Coordinate.SHIP: - if (playerMove) - coordinatePanel.setBackground(new Color(91, 58, 41)); - break; - case Coordinate.SHOT: - coordinatePanel.setBackground(new Color(0, 94, 184)); - break; - case Coordinate.HIT: - coordinatePanel.setBackground(new Color(0, 94, 184)); - coordinatePanel.add(new JLabel("x")); - break; - } - - coordinatePanel.revalidate(); - coordinatePanel.repaint(); - } - } - - /** - * This function is only for testing - */ - private static GameLogic gui; - - public static void main(String[] args) { - JFrame frame = new JFrame("Test GUI"); - gui = new GameLogic(15, 28, 1); - frame.setContentPane(gui); - frame.setSize(640, 480); - frame.setResizable(true); - frame.setVisible(true); - - gui.start(); - } - - public void start() { - this.playerMove = true; - this.gameState = GameLogic.PREPARATION; - - // create player matchfield - this.matchfield = new Matchfield(matchfieldSize); - this.matchfield.createMatchfield(); - this.matchfield.setShip(new Coordinate(4, 6), 2, 0); - this.matchfield.setShip(new Coordinate(1, 3), 3, 0); - this.matchfield.setShip(new Coordinate(9, 1), 4, 1); - this.matchfield.setShip(new Coordinate(0, 0), 5, 0); - - // create enemy matchfield and initialize AI (with player's matchfield) - this.enemyMatchfield = new Matchfield(matchfieldSize); - this.enemyMatchfield.createMatchfield(); - this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); - this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); - this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); - this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); - this.aiLogic = new Logic(); - this.aiLogic.setMatchfield(this.matchfield); - - // show player own matchfield - gui.updateField(this.playerMove, this.matchfield); - - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - // enter game loop - this.nextMove(); - } - - public void nextMove() { - this.playerMove = !this.playerMove; - - if (this.playerMove) { - gui.updateField(this.playerMove, this.enemyMatchfield); - } else { - gui.updateField(this.playerMove, this.matchfield); - } - - } - - public int getGameState() { - return gameState; - } - - public boolean isPlayerMove() { - return playerMove; - } -} +package de.tims.fleetstorm.gui; + +import java.awt.Color; +import java.awt.Font; +import java.awt.GridLayout; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.MatteBorder; + +import de.tims.fleetstorm.ai.Logic; +import de.tims.fleetstorm.matchfield.Coordinate; +import de.tims.fleetstorm.matchfield.Matchfield; + +public class GameLogic extends JPanel { + + // GameManager stuff + private int gameState; + private Matchfield matchfield; + private Matchfield enemyMatchfield; + private int matchfieldSize = 10; + private boolean playerMove; + private Logic aiLogic; + + public static final int PREPARATION = 1; + public static final int RUNNING = 2; + public static final int OVER = 3; + + // GUI stuff + private ArrayList fields; + private JLabel matchfieldValue; + private JLabel moveValue; + private JLabel own2Ship; + private JLabel own3Ship; + private JLabel own4Ship; + private JLabel own5Ship; + private JLabel enemy2Ship; + private JLabel enemy3Ship; + private JLabel enemy4Ship; + private JLabel enemy5Ship; + + public GameLogic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { + this.fields = new ArrayList<>(); + setSize(640, 480); + + JPanel fieldWrapper = new JPanel(); + fieldWrapper.setBounds(10, 11, 362, 458); + fieldWrapper.setLayout(null); + + JPanel infoWrapper = new JPanel(); + infoWrapper.setBounds(382, 11, 248, 458); + infoWrapper.setLayout(null); + setLayout(null); + add(fieldWrapper); + add(infoWrapper); + + JPanel yourFleet = new JPanel(); + yourFleet.setBounds(10, 110, 228, 93); + infoWrapper.add(yourFleet); + yourFleet.setLayout(null); + + JPanel ownFleetHeadingPanel = new JPanel(); + ownFleetHeadingPanel.setBounds(0, 0, 228, 27); + yourFleet.add(ownFleetHeadingPanel); + ownFleetHeadingPanel.setLayout(null); + + JLabel ownFleetHeading = new JLabel("Deine Flotte:"); + ownFleetHeading.setBounds(0, 0, 228, 17); + ownFleetHeadingPanel.add(ownFleetHeading); + ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + + JPanel ownFleetContentPanel = new JPanel(); + ownFleetContentPanel.setBounds(0, 26, 228, 67); + yourFleet.add(ownFleetContentPanel); + ownFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel own2ShipLabel = new JLabel("1x2 Schiff"); + ownFleetContentPanel.add(own2ShipLabel); + + own2Ship = new JLabel("OK"); + ownFleetContentPanel.add(own2Ship); + + JLabel own3ShipLabel = new JLabel("1x3 Schiff"); + ownFleetContentPanel.add(own3ShipLabel); + + own3Ship = new JLabel("OK"); + ownFleetContentPanel.add(own3Ship); + + JLabel own4ShipLabel = new JLabel("1x4 Schiff"); + ownFleetContentPanel.add(own4ShipLabel); + + own4Ship = new JLabel("OK"); + ownFleetContentPanel.add(own4Ship); + + JLabel own5ShipLabel = new JLabel("1x5 Schiff"); + ownFleetContentPanel.add(own5ShipLabel); + + own5Ship = new JLabel("OK"); + ownFleetContentPanel.add(own5Ship); + + JPanel enemyFleet = new JPanel(); + enemyFleet.setLayout(null); + enemyFleet.setBounds(10, 237, 228, 93); + infoWrapper.add(enemyFleet); + + JPanel enemyFleetHeadingPanel = new JPanel(); + enemyFleetHeadingPanel.setLayout(null); + enemyFleetHeadingPanel.setBounds(0, 0, 228, 27); + enemyFleet.add(enemyFleetHeadingPanel); + + JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:"); + enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + enemyFleetHeading.setBounds(0, 0, 228, 17); + enemyFleetHeadingPanel.add(enemyFleetHeading); + + JPanel enemyFleetContentPanel = new JPanel(); + enemyFleetContentPanel.setBounds(0, 26, 228, 67); + enemyFleet.add(enemyFleetContentPanel); + enemyFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel enemy2ShipLabel = new JLabel("1x2 Schiff"); + enemyFleetContentPanel.add(enemy2ShipLabel); + + enemy2Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy2Ship); + + JLabel enemy3ShipLabel = new JLabel("1x3 Schiff"); + enemyFleetContentPanel.add(enemy3ShipLabel); + + enemy3Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy3Ship); + + JLabel enemy4ShipLabel = new JLabel("1x4 Schiff"); + enemyFleetContentPanel.add(enemy4ShipLabel); + + enemy4Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy4Ship); + + JLabel enemy5ShipLabel = new JLabel("1x5 Schiff"); + enemyFleetContentPanel.add(enemy5ShipLabel); + + enemy5Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy5Ship); + + JPanel infos = new JPanel(); + infos.setLayout(null); + infos.setBounds(10, 11, 228, 68); + infoWrapper.add(infos); + + JPanel gameInfoHeadingPanel = new JPanel(); + gameInfoHeadingPanel.setLayout(null); + gameInfoHeadingPanel.setBounds(0, 0, 228, 27); + infos.add(gameInfoHeadingPanel); + + JLabel gameInfoHeading = new JLabel("Spielinfos"); + gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + gameInfoHeading.setBounds(0, 0, 228, 17); + gameInfoHeadingPanel.add(gameInfoHeading); + + JPanel gameInfoContentPanel = new JPanel(); + gameInfoContentPanel.setBounds(0, 26, 228, 31); + infos.add(gameInfoContentPanel); + gameInfoContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel matchfieldLabel = new JLabel("Spielfeld"); + gameInfoContentPanel.add(matchfieldLabel); + + matchfieldValue = new JLabel("-"); + gameInfoContentPanel.add(matchfieldValue); + + JLabel moveLabel = new JLabel("Am Zug"); + gameInfoContentPanel.add(moveLabel); + + moveValue = new JLabel("-"); + gameInfoContentPanel.add(moveValue); + + for (int x = 0; x < 10; x++) { + for (int y = 0; y < 10; y++) { + JPanel field = new JPanel(); + + int xPos = gapToFrameBorder + x * fieldWidth + (x * spaceBetween); + int yPos = gapToFrameBorder + y * fieldWidth + (y * spaceBetween); + + field.setBounds(xPos, yPos, fieldWidth, fieldWidth); + field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); + field.setName(x + ":" + y); + fields.add(field); + fieldWrapper.add(field); + + field.addMouseListener(new MouseAdapter() { + + public void mouseClicked(MouseEvent e) { + if (!playerMove) + return; + + int[] coords = getCoordinateFromLabel(field); + int coordX = coords[0]; + int coordY = coords[1]; + + // TODO add shoot function + } + }); + } + } + + } + + public ArrayList getFields() { + return fields; + } + + public JLabel getMatchfieldValue() { + return matchfieldValue; + } + + public JLabel getMoveValue() { + return moveValue; + } + + public JLabel getOwn2Ship() { + return own2Ship; + } + + public JLabel getOwn3Ship() { + return own3Ship; + } + + public JLabel getOwn4Ship() { + return own4Ship; + } + + public JLabel getOwn5Ship() { + return own5Ship; + } + + public JLabel getEnemy2Ship() { + return enemy2Ship; + } + + public JLabel getEnemy3Ship() { + return enemy3Ship; + } + + public JLabel getEnemy4Ship() { + return enemy4Ship; + } + + public JLabel getEnemy5Ship() { + return enemy5Ship; + } + + private int[] getCoordinateFromLabel(JPanel panel) { + String panelName = panel.getName(); + String[] panelNameSplit = panelName.split(":"); + int coordX = Integer.parseInt(panelNameSplit[0]); + int coordY = Integer.parseInt(panelNameSplit[1]); + return new int[] { coordX, coordY }; + } + + private void updateField(boolean playerMove, Matchfield matchfield) { + for (JPanel coordinatePanel : this.fields) { + int[] coords = this.getCoordinateFromLabel(coordinatePanel); + int coordX = coords[0]; + int coordY = coords[1]; + + // reset field + coordinatePanel.setBackground(null); + coordinatePanel.removeAll(); + + int state = matchfield.getField(coordX, coordY).getState(); + + switch (state) { + case Coordinate.SHIP: + if (playerMove) + coordinatePanel.setBackground(new Color(91, 58, 41)); + break; + case Coordinate.SHOT: + coordinatePanel.setBackground(new Color(0, 94, 184)); + break; + case Coordinate.HIT: + coordinatePanel.setBackground(new Color(0, 94, 184)); + coordinatePanel.add(new JLabel("x")); + break; + } + + coordinatePanel.revalidate(); + coordinatePanel.repaint(); + } + + // set game infos + if (this.playerMove) { + this.getMatchfieldValue().setText("Gegner"); + this.getMoveValue().setText("Du"); + } else { + this.getMatchfieldValue().setText("Eigenes"); + this.getMoveValue().setText("Gegner"); + } + } + + /** + * This function is only for testing + */ + private static GameLogic gui; + + public static void main(String[] args) { + JFrame frame = new JFrame("Test GUI"); + gui = new GameLogic(15, 28, 1); + frame.setContentPane(gui); + frame.setSize(640, 480); + frame.setResizable(true); + frame.setVisible(true); + + gui.start(); + } + + public void start() { + this.playerMove = true; + this.gameState = GameLogic.PREPARATION; + + // create player matchfield + this.matchfield = new Matchfield(matchfieldSize); + this.matchfield.createMatchfield(); + this.matchfield.setShip(new Coordinate(4, 6), 2, 0); + this.matchfield.setShip(new Coordinate(1, 3), 3, 0); + this.matchfield.setShip(new Coordinate(9, 1), 4, 1); + this.matchfield.setShip(new Coordinate(0, 0), 5, 0); + + // create enemy matchfield and initialize AI (with player's matchfield) + this.enemyMatchfield = new Matchfield(matchfieldSize); + this.enemyMatchfield.createMatchfield(); + this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); + this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); + this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); + this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); + this.aiLogic = new Logic(); + this.aiLogic.setMatchfield(this.matchfield); + + // show player own matchfield + gui.updateField(this.playerMove, this.matchfield); + + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // enter game loop + this.nextMove(); + } + + public void nextMove() { + this.playerMove = !this.playerMove; + + if (this.playerMove) { + gui.updateField(this.playerMove, this.enemyMatchfield); + } else { + gui.updateField(this.playerMove, this.matchfield); + } + + } + + public int getGameState() { + return gameState; + } + + public boolean isPlayerMove() { + return playerMove; + } +} From fadf708556e8858251b6be27f707fbc958ca3eb1 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:38:25 +0100 Subject: [PATCH 73/93] Added shoot() function to Coordinate class with first test case --- .../fleetstorm/matchfield/Coordinate.java | 4 ++++ .../fleetstorm/matchfield/CoordinateTest.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index a11c666..f31b14c 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -44,4 +44,8 @@ public class Coordinate { System.out.println("X = " + this.x + ", Y = " + this.y + ", State = " + this.state); } + public void shoot() { + this.setState(Coordinate.HIT); + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 3de5fc8..9705b32 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -44,4 +44,23 @@ class CoordinateTest { Arguments.of("State = EMPTY", 0, Coordinate.EMPTY, Coordinate.EMPTY)); } + @ParameterizedTest(name = "test if shoot function sets correct field states") + @MethodSource("ShootFunctionFieldStates") + void testShootFunctionSetsCorrectFieldStates(String testName, Coordinate coordinateToTest, int expectedState) { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + // set dummy ship + matchfield.setShip(new Coordinate(0, 0), 5, 0); + + Coordinate realCoordinate = matchfield.getField(coordinateToTest); + realCoordinate.shoot(); + int calculatedResult = realCoordinate.getState(); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedState); + } + + static Stream ShootFunctionFieldStates() { + return Stream.of(Arguments.of("Field State is HIT", new Coordinate(0, 0), Coordinate.HIT)); + } + } From dd87d19b182a089c1440f2f4f078018047b58a32 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:40:38 +0100 Subject: [PATCH 74/93] Added second test case to shoot(): Call on Coordinate without ship --- .../java/de/tims/fleetstorm/matchfield/Coordinate.java | 7 ++++++- .../java/de/tims/fleetstorm/matchfield/CoordinateTest.java | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index f31b14c..7ce2692 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -45,7 +45,12 @@ public class Coordinate { } public void shoot() { - this.setState(Coordinate.HIT); + if (this.state == Coordinate.SHIP) { + this.state = Coordinate.HIT; + return; + } + + this.state = Coordinate.SHOT; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 9705b32..0aefe99 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -60,7 +60,8 @@ class CoordinateTest { } static Stream ShootFunctionFieldStates() { - return Stream.of(Arguments.of("Field State is HIT", new Coordinate(0, 0), Coordinate.HIT)); + return Stream.of(Arguments.of("Field State is HIT", new Coordinate(0, 0), Coordinate.HIT), + Arguments.of("Field State is SHOT", new Coordinate(0, 1), Coordinate.SHOT)); } } From 8c49de97677c1e14901dd99c51abc43fb5fa3333 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:44:51 +0100 Subject: [PATCH 75/93] New test case for shoot(): Check if field was already shot --- .../de/tims/fleetstorm/matchfield/Coordinate.java | 6 ++++-- .../tims/fleetstorm/matchfield/CoordinateTest.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index 7ce2692..c3d3394 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -44,13 +44,15 @@ public class Coordinate { System.out.println("X = " + this.x + ", Y = " + this.y + ", State = " + this.state); } - public void shoot() { + public boolean shoot() { if (this.state == Coordinate.SHIP) { this.state = Coordinate.HIT; - return; + return true; } this.state = Coordinate.SHOT; + + return true; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 0aefe99..fd4b73e 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -1,6 +1,7 @@ package de.tims.fleetstorm.matchfield; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.stream.Stream; @@ -64,4 +65,15 @@ class CoordinateTest { Arguments.of("Field State is SHOT", new Coordinate(0, 1), Coordinate.SHOT)); } + @Test + void testIfShootCheckWhenFieldIsAlreadyShot() { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + // set dummy ship + matchfield.setShip(new Coordinate(3, 3), 4, 1); + + assertTrue(matchfield.getField(0, 0).shoot()); + } + } From 4d69cc3e7609f2c9fdf4f3b43baa142144351c69 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:46:46 +0100 Subject: [PATCH 76/93] Added test case when field was already shot (expect false) --- src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java | 3 +++ .../java/de/tims/fleetstorm/matchfield/CoordinateTest.java | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index c3d3394..aeebdbf 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -45,6 +45,9 @@ public class Coordinate { } public boolean shoot() { + if (this.state == Coordinate.SHOT) + return false; + if (this.state == Coordinate.SHIP) { this.state = Coordinate.HIT; return true; diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index fd4b73e..779e432 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -1,6 +1,7 @@ package de.tims.fleetstorm.matchfield; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -74,6 +75,7 @@ class CoordinateTest { matchfield.setShip(new Coordinate(3, 3), 4, 1); assertTrue(matchfield.getField(0, 0).shoot()); + assertFalse(matchfield.getField(0, 0).shoot()); } } From cc34afa847f4421d1a6cf53bafd8414bf50e1184 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:48:13 +0100 Subject: [PATCH 77/93] Added test case when field was already hitted --- src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java | 2 +- .../java/de/tims/fleetstorm/matchfield/CoordinateTest.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index aeebdbf..ede9574 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -45,7 +45,7 @@ public class Coordinate { } public boolean shoot() { - if (this.state == Coordinate.SHOT) + if (this.state == Coordinate.SHOT || this.state == Coordinate.HIT) return false; if (this.state == Coordinate.SHIP) { diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 779e432..1b9a560 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -76,6 +76,9 @@ class CoordinateTest { assertTrue(matchfield.getField(0, 0).shoot()); assertFalse(matchfield.getField(0, 0).shoot()); + + matchfield.getField(3, 4).shoot(); + assertFalse(matchfield.getField(3, 4).shoot()); } } From a42df0aac28f1a40aa79fb7ee52826a14dcfa3f0 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 15:08:11 +0100 Subject: [PATCH 78/93] Added shoot() function to panel click for general game loop --- .../de/tims/fleetstorm/gui/GameLogic.java | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 9225309..293e2b2 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -33,7 +33,7 @@ public class GameLogic extends JPanel { // GUI stuff private ArrayList fields; private JLabel matchfieldValue; - private JLabel moveValue; + private JLabel stateValue; private JLabel own2Ship; private JLabel own3Ship; private JLabel own4Ship; @@ -172,11 +172,11 @@ public class GameLogic extends JPanel { matchfieldValue = new JLabel("-"); gameInfoContentPanel.add(matchfieldValue); - JLabel moveLabel = new JLabel("Am Zug"); - gameInfoContentPanel.add(moveLabel); + JLabel stateLabel = new JLabel("Status"); + gameInfoContentPanel.add(stateLabel); - moveValue = new JLabel("-"); - gameInfoContentPanel.add(moveValue); + stateValue = new JLabel("-"); + gameInfoContentPanel.add(stateValue); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { @@ -201,7 +201,11 @@ public class GameLogic extends JPanel { int coordX = coords[0]; int coordY = coords[1]; - // TODO add shoot function + boolean shotSuccess = enemyMatchfield.getField(coordX, coordY).shoot(); + gui.updateField(playerMove, enemyMatchfield); + + if (!shotSuccess) + nextMove(); } }); } @@ -217,8 +221,8 @@ public class GameLogic extends JPanel { return matchfieldValue; } - public JLabel getMoveValue() { - return moveValue; + public JLabel getStateValue() { + return stateValue; } public JLabel getOwn2Ship() { @@ -282,7 +286,7 @@ public class GameLogic extends JPanel { coordinatePanel.setBackground(new Color(0, 94, 184)); break; case Coordinate.HIT: - coordinatePanel.setBackground(new Color(0, 94, 184)); + coordinatePanel.setBackground(new Color(91, 58, 41)); coordinatePanel.add(new JLabel("x")); break; } @@ -293,11 +297,16 @@ public class GameLogic extends JPanel { // set game infos if (this.playerMove) { - this.getMatchfieldValue().setText("Gegner"); - this.getMoveValue().setText("Du"); + if (this.gameState == GameLogic.PREPARATION) { + this.getMatchfieldValue().setText("Eigenes"); + this.getStateValue().setText("Vorbereiten..."); + } else { + this.getMatchfieldValue().setText("Gegner"); + this.getStateValue().setText("Du bist am Zug"); + } } else { this.getMatchfieldValue().setText("Eigenes"); - this.getMoveValue().setText("Gegner"); + this.getStateValue().setText("Dein Gegner ist am Zug"); } } @@ -349,6 +358,7 @@ public class GameLogic extends JPanel { } // enter game loop + this.gameState = GameLogic.RUNNING; this.nextMove(); } @@ -357,8 +367,27 @@ public class GameLogic extends JPanel { if (this.playerMove) { gui.updateField(this.playerMove, this.enemyMatchfield); + // waiting for mouse click event.... } else { gui.updateField(this.playerMove, this.matchfield); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + Coordinate aiChosenField = this.aiLogic.chooseField(); + aiChosenField.shoot(); + gui.updateField(this.playerMove, this.matchfield); + + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + this.nextMove(); } } From 79f90946a5c3527646813a1522a8f44a708e49b4 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 15:19:21 +0100 Subject: [PATCH 79/93] Refactored LogicTest: No longer useful with general game loop --- .../de/tims/fleetstorm/gui/GameLogic.java | 17 +++++++------ .../de/tims/fleetstorm/gui/LogicTest.java | 24 +++---------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 293e2b2..bd98e85 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -204,8 +204,8 @@ public class GameLogic extends JPanel { boolean shotSuccess = enemyMatchfield.getField(coordX, coordY).shoot(); gui.updateField(playerMove, enemyMatchfield); - if (!shotSuccess) - nextMove(); +// if (!shotSuccess) +// nextMove(); } }); } @@ -327,7 +327,6 @@ public class GameLogic extends JPanel { } public void start() { - this.playerMove = true; this.gameState = GameLogic.PREPARATION; // create player matchfield @@ -359,11 +358,11 @@ public class GameLogic extends JPanel { // enter game loop this.gameState = GameLogic.RUNNING; - this.nextMove(); + this.nextMove(true); } - public void nextMove() { - this.playerMove = !this.playerMove; + public void nextMove(boolean playerMove) { + this.playerMove = playerMove; if (this.playerMove) { gui.updateField(this.playerMove, this.enemyMatchfield); @@ -387,7 +386,7 @@ public class GameLogic extends JPanel { e.printStackTrace(); } - this.nextMove(); +// this.nextMove(); } } @@ -399,4 +398,8 @@ public class GameLogic extends JPanel { public boolean isPlayerMove() { return playerMove; } + + public static void setGui(GameLogic gui) { + GameLogic.gui = gui; + } } diff --git a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java index ee73c74..fc6490d 100644 --- a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -1,45 +1,27 @@ package de.tims.fleetstorm.gui; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import de.tims.fleetstorm.gui.GameLogic; - class LogicTest { GameLogic guiLogic = new GameLogic(0, 0, 0); @BeforeEach void setup() { + GameLogic.setGui(new GameLogic(0, 0, 0)); guiLogic.start(); } @Test - void testIfGameStateIsPreparationAfterStart() { - int expectedState = GameLogic.PREPARATION; + void testIfGameStateIsRunningAfterStart() { + int expectedState = GameLogic.RUNNING; int calculatedState = guiLogic.getGameState(); assertEquals(expectedState, calculatedState); } - @Test - void testNextMoveIsNotPlayer() { - - // 20 tries => every even move is a player move - for (int i = 0; i < 20; i++) { - guiLogic.nextMove(); - boolean calculatedResult = guiLogic.isPlayerMove(); - if (i % 2 == 0) { - assertFalse(calculatedResult); - } else { - assertTrue(calculatedResult); - } - } - } - } From d472759a1c94dce52bf606a8eef04ba1da31c235 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 15:34:18 +0100 Subject: [PATCH 80/93] Implemented logic after ai made move and player made move --- .../de/tims/fleetstorm/gui/GameLogic.java | 44 ++++++++++++------- .../de/tims/fleetstorm/gui/LogicTest.java | 1 - 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index bd98e85..aacf1db 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -201,11 +201,15 @@ public class GameLogic extends JPanel { int coordX = coords[0]; int coordY = coords[1]; - boolean shotSuccess = enemyMatchfield.getField(coordX, coordY).shoot(); - gui.updateField(playerMove, enemyMatchfield); + Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); + + boolean shotSuccess = chosenField.shoot(); + gui.updateField(enemyMatchfield); + + if (shotSuccess && chosenField.getState() != Coordinate.HIT) { + nextMove(false); + } -// if (!shotSuccess) -// nextMove(); } }); } @@ -257,6 +261,13 @@ public class GameLogic extends JPanel { return enemy5Ship; } + private JLabel getXLabel() { + JLabel x = new JLabel("x"); + x.setForeground(Color.WHITE); + + return x; + } + private int[] getCoordinateFromLabel(JPanel panel) { String panelName = panel.getName(); String[] panelNameSplit = panelName.split(":"); @@ -265,7 +276,7 @@ public class GameLogic extends JPanel { return new int[] { coordX, coordY }; } - private void updateField(boolean playerMove, Matchfield matchfield) { + private void updateField(Matchfield matchfield) { for (JPanel coordinatePanel : this.fields) { int[] coords = this.getCoordinateFromLabel(coordinatePanel); int coordX = coords[0]; @@ -287,7 +298,7 @@ public class GameLogic extends JPanel { break; case Coordinate.HIT: coordinatePanel.setBackground(new Color(91, 58, 41)); - coordinatePanel.add(new JLabel("x")); + coordinatePanel.add(this.getXLabel()); break; } @@ -297,6 +308,7 @@ public class GameLogic extends JPanel { // set game infos if (this.playerMove) { + System.out.println("own update"); if (this.gameState == GameLogic.PREPARATION) { this.getMatchfieldValue().setText("Eigenes"); this.getStateValue().setText("Vorbereiten..."); @@ -305,8 +317,9 @@ public class GameLogic extends JPanel { this.getStateValue().setText("Du bist am Zug"); } } else { + System.out.println("enemy update"); this.getMatchfieldValue().setText("Eigenes"); - this.getStateValue().setText("Dein Gegner ist am Zug"); + this.getStateValue().setText("Gegner am Zug"); } } @@ -328,6 +341,7 @@ public class GameLogic extends JPanel { public void start() { this.gameState = GameLogic.PREPARATION; + this.playerMove = true; // create player matchfield this.matchfield = new Matchfield(matchfieldSize); @@ -348,7 +362,7 @@ public class GameLogic extends JPanel { this.aiLogic.setMatchfield(this.matchfield); // show player own matchfield - gui.updateField(this.playerMove, this.matchfield); + gui.updateField(this.matchfield); try { Thread.sleep(3000); @@ -365,10 +379,10 @@ public class GameLogic extends JPanel { this.playerMove = playerMove; if (this.playerMove) { - gui.updateField(this.playerMove, this.enemyMatchfield); + gui.updateField(this.enemyMatchfield); // waiting for mouse click event.... } else { - gui.updateField(this.playerMove, this.matchfield); + gui.updateField(this.matchfield); try { Thread.sleep(1000); @@ -378,7 +392,9 @@ public class GameLogic extends JPanel { Coordinate aiChosenField = this.aiLogic.chooseField(); aiChosenField.shoot(); - gui.updateField(this.playerMove, this.matchfield); + gui.updateField(this.matchfield); + + boolean aiHasNextMove = aiChosenField.getState() == Coordinate.HIT; try { Thread.sleep(2000); @@ -386,7 +402,7 @@ public class GameLogic extends JPanel { e.printStackTrace(); } -// this.nextMove(); + this.nextMove(aiHasNextMove); } } @@ -398,8 +414,4 @@ public class GameLogic extends JPanel { public boolean isPlayerMove() { return playerMove; } - - public static void setGui(GameLogic gui) { - GameLogic.gui = gui; - } } diff --git a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java index fc6490d..87786d4 100644 --- a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -11,7 +11,6 @@ class LogicTest { @BeforeEach void setup() { - GameLogic.setGui(new GameLogic(0, 0, 0)); guiLogic.start(); } From b11262ba56232f985415f1cb94be72624d9d40cb Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 13:55:48 +0100 Subject: [PATCH 81/93] Changed whole GUI layout to two matchfields --- .../de/tims/fleetstorm/gui/GameLogic.java | 305 ++++-------------- .../de/tims/fleetstorm/gui/LogicTest.java | 2 +- 2 files changed, 64 insertions(+), 243 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index aacf1db..414fd36 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -1,8 +1,6 @@ package de.tims.fleetstorm.gui; import java.awt.Color; -import java.awt.Font; -import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; @@ -31,234 +29,86 @@ public class GameLogic extends JPanel { public static final int OVER = 3; // GUI stuff - private ArrayList fields; - private JLabel matchfieldValue; - private JLabel stateValue; - private JLabel own2Ship; - private JLabel own3Ship; - private JLabel own4Ship; - private JLabel own5Ship; - private JLabel enemy2Ship; - private JLabel enemy3Ship; - private JLabel enemy4Ship; - private JLabel enemy5Ship; - - public GameLogic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { - this.fields = new ArrayList<>(); + private ArrayList playerFields; + private ArrayList enemyFields; + + public GameLogic(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween) { + this.playerFields = new ArrayList<>(); + this.enemyFields = new ArrayList<>(); setSize(640, 480); JPanel fieldWrapper = new JPanel(); - fieldWrapper.setBounds(10, 11, 362, 458); + fieldWrapper.setBounds(10, 11, 305, 458); fieldWrapper.setLayout(null); - - JPanel infoWrapper = new JPanel(); - infoWrapper.setBounds(382, 11, 248, 458); - infoWrapper.setLayout(null); setLayout(null); add(fieldWrapper); - add(infoWrapper); - - JPanel yourFleet = new JPanel(); - yourFleet.setBounds(10, 110, 228, 93); - infoWrapper.add(yourFleet); - yourFleet.setLayout(null); - - JPanel ownFleetHeadingPanel = new JPanel(); - ownFleetHeadingPanel.setBounds(0, 0, 228, 27); - yourFleet.add(ownFleetHeadingPanel); - ownFleetHeadingPanel.setLayout(null); - - JLabel ownFleetHeading = new JLabel("Deine Flotte:"); - ownFleetHeading.setBounds(0, 0, 228, 17); - ownFleetHeadingPanel.add(ownFleetHeading); - ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); - - JPanel ownFleetContentPanel = new JPanel(); - ownFleetContentPanel.setBounds(0, 26, 228, 67); - yourFleet.add(ownFleetContentPanel); - ownFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); - - JLabel own2ShipLabel = new JLabel("1x2 Schiff"); - ownFleetContentPanel.add(own2ShipLabel); - - own2Ship = new JLabel("OK"); - ownFleetContentPanel.add(own2Ship); - - JLabel own3ShipLabel = new JLabel("1x3 Schiff"); - ownFleetContentPanel.add(own3ShipLabel); - - own3Ship = new JLabel("OK"); - ownFleetContentPanel.add(own3Ship); - - JLabel own4ShipLabel = new JLabel("1x4 Schiff"); - ownFleetContentPanel.add(own4ShipLabel); - - own4Ship = new JLabel("OK"); - ownFleetContentPanel.add(own4Ship); - - JLabel own5ShipLabel = new JLabel("1x5 Schiff"); - ownFleetContentPanel.add(own5ShipLabel); - - own5Ship = new JLabel("OK"); - ownFleetContentPanel.add(own5Ship); - - JPanel enemyFleet = new JPanel(); - enemyFleet.setLayout(null); - enemyFleet.setBounds(10, 237, 228, 93); - infoWrapper.add(enemyFleet); - - JPanel enemyFleetHeadingPanel = new JPanel(); - enemyFleetHeadingPanel.setLayout(null); - enemyFleetHeadingPanel.setBounds(0, 0, 228, 27); - enemyFleet.add(enemyFleetHeadingPanel); - - JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:"); - enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); - enemyFleetHeading.setBounds(0, 0, 228, 17); - enemyFleetHeadingPanel.add(enemyFleetHeading); - JPanel enemyFleetContentPanel = new JPanel(); - enemyFleetContentPanel.setBounds(0, 26, 228, 67); - enemyFleet.add(enemyFleetContentPanel); - enemyFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); + JPanel enemyFieldWrapper = new JPanel(); + enemyFieldWrapper.setLayout(null); + enemyFieldWrapper.setBounds(326, 11, 305, 458); + add(enemyFieldWrapper); - JLabel enemy2ShipLabel = new JLabel("1x2 Schiff"); - enemyFleetContentPanel.add(enemy2ShipLabel); + this.generateFieldsForGUI(gapToFrameBorderX, gapToFrameBorderY, fieldWidth, spaceBetween, fieldWrapper, + this.playerFields, false); - enemy2Ship = new JLabel("OK"); - enemyFleetContentPanel.add(enemy2Ship); + JLabel ownFieldLabel = new JLabel("Eigenes Spielfeld"); + ownFieldLabel.setBounds(10, 11, 114, 14); + fieldWrapper.add(ownFieldLabel); + this.generateFieldsForGUI(gapToFrameBorderX, gapToFrameBorderY, fieldWidth, spaceBetween, enemyFieldWrapper, + this.enemyFields, true); - JLabel enemy3ShipLabel = new JLabel("1x3 Schiff"); - enemyFleetContentPanel.add(enemy3ShipLabel); - - enemy3Ship = new JLabel("OK"); - enemyFleetContentPanel.add(enemy3Ship); - - JLabel enemy4ShipLabel = new JLabel("1x4 Schiff"); - enemyFleetContentPanel.add(enemy4ShipLabel); - - enemy4Ship = new JLabel("OK"); - enemyFleetContentPanel.add(enemy4Ship); - - JLabel enemy5ShipLabel = new JLabel("1x5 Schiff"); - enemyFleetContentPanel.add(enemy5ShipLabel); - - enemy5Ship = new JLabel("OK"); - enemyFleetContentPanel.add(enemy5Ship); - - JPanel infos = new JPanel(); - infos.setLayout(null); - infos.setBounds(10, 11, 228, 68); - infoWrapper.add(infos); - - JPanel gameInfoHeadingPanel = new JPanel(); - gameInfoHeadingPanel.setLayout(null); - gameInfoHeadingPanel.setBounds(0, 0, 228, 27); - infos.add(gameInfoHeadingPanel); - - JLabel gameInfoHeading = new JLabel("Spielinfos"); - gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); - gameInfoHeading.setBounds(0, 0, 228, 17); - gameInfoHeadingPanel.add(gameInfoHeading); - - JPanel gameInfoContentPanel = new JPanel(); - gameInfoContentPanel.setBounds(0, 26, 228, 31); - infos.add(gameInfoContentPanel); - gameInfoContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); - - JLabel matchfieldLabel = new JLabel("Spielfeld"); - gameInfoContentPanel.add(matchfieldLabel); - - matchfieldValue = new JLabel("-"); - gameInfoContentPanel.add(matchfieldValue); - - JLabel stateLabel = new JLabel("Status"); - gameInfoContentPanel.add(stateLabel); - - stateValue = new JLabel("-"); - gameInfoContentPanel.add(stateValue); + JLabel enemyFieldLabel = new JLabel("Gegnerisches Spielfeld"); + enemyFieldLabel.setBounds(10, 11, 168, 14); + enemyFieldWrapper.add(enemyFieldLabel); + } + private void generateFieldsForGUI(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween, + JPanel panel, ArrayList fields, boolean registerMouseListener) { for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { JPanel field = new JPanel(); - int xPos = gapToFrameBorder + x * fieldWidth + (x * spaceBetween); - int yPos = gapToFrameBorder + y * fieldWidth + (y * spaceBetween); + int xPos = gapToFrameBorderX + x * fieldWidth + (x * spaceBetween); + int yPos = gapToFrameBorderY + y * fieldWidth + (y * spaceBetween); field.setBounds(xPos, yPos, fieldWidth, fieldWidth); field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); field.setName(x + ":" + y); fields.add(field); - fieldWrapper.add(field); + panel.add(field); - field.addMouseListener(new MouseAdapter() { + if (registerMouseListener) { + field.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent e) { - if (!playerMove) - return; + public void mouseClicked(MouseEvent e) { + System.out.println("mouse clicked"); + if (!playerMove) + return; - int[] coords = getCoordinateFromLabel(field); - int coordX = coords[0]; - int coordY = coords[1]; + int[] coords = getCoordinateFromLabel(field); + int coordX = coords[0]; + int coordY = coords[1]; - Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); + Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); - boolean shotSuccess = chosenField.shoot(); - gui.updateField(enemyMatchfield); + boolean shotSuccess = chosenField.shoot(); + gui.updateFields(); - if (shotSuccess && chosenField.getState() != Coordinate.HIT) { - nextMove(false); - } + if (shotSuccess && chosenField.getState() != Coordinate.HIT) { + nextMove(false); + } else { + nextMove(true); + } - } - }); + } + }); + } } } - } public ArrayList getFields() { - return fields; - } - - public JLabel getMatchfieldValue() { - return matchfieldValue; - } - - public JLabel getStateValue() { - return stateValue; - } - - public JLabel getOwn2Ship() { - return own2Ship; - } - - public JLabel getOwn3Ship() { - return own3Ship; - } - - public JLabel getOwn4Ship() { - return own4Ship; - } - - public JLabel getOwn5Ship() { - return own5Ship; - } - - public JLabel getEnemy2Ship() { - return enemy2Ship; - } - - public JLabel getEnemy3Ship() { - return enemy3Ship; - } - - public JLabel getEnemy4Ship() { - return enemy4Ship; - } - - public JLabel getEnemy5Ship() { - return enemy5Ship; + return playerFields; } private JLabel getXLabel() { @@ -276,9 +126,15 @@ public class GameLogic extends JPanel { return new int[] { coordX, coordY }; } - private void updateField(Matchfield matchfield) { - for (JPanel coordinatePanel : this.fields) { - int[] coords = this.getCoordinateFromLabel(coordinatePanel); + private void updateFields() { + this.updateField(enemyMatchfield, this.enemyFields); + this.updateField(matchfield, this.playerFields); + } + + private void updateField(Matchfield matchfield, ArrayList fields) { + + for (JPanel coordinatePanel : fields) { + int[] coords = getCoordinateFromLabel(coordinatePanel); int coordX = coords[0]; int coordY = coords[1]; @@ -298,7 +154,7 @@ public class GameLogic extends JPanel { break; case Coordinate.HIT: coordinatePanel.setBackground(new Color(91, 58, 41)); - coordinatePanel.add(this.getXLabel()); + coordinatePanel.add(getXLabel()); break; } @@ -306,21 +162,6 @@ public class GameLogic extends JPanel { coordinatePanel.repaint(); } - // set game infos - if (this.playerMove) { - System.out.println("own update"); - if (this.gameState == GameLogic.PREPARATION) { - this.getMatchfieldValue().setText("Eigenes"); - this.getStateValue().setText("Vorbereiten..."); - } else { - this.getMatchfieldValue().setText("Gegner"); - this.getStateValue().setText("Du bist am Zug"); - } - } else { - System.out.println("enemy update"); - this.getMatchfieldValue().setText("Eigenes"); - this.getStateValue().setText("Gegner am Zug"); - } } /** @@ -330,9 +171,10 @@ public class GameLogic extends JPanel { public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - gui = new GameLogic(15, 28, 1); + gui = new GameLogic(12, 35, 25, 1); frame.setContentPane(gui); frame.setSize(640, 480); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); frame.setVisible(true); @@ -361,15 +203,6 @@ public class GameLogic extends JPanel { this.aiLogic = new Logic(); this.aiLogic.setMatchfield(this.matchfield); - // show player own matchfield - gui.updateField(this.matchfield); - - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - // enter game loop this.gameState = GameLogic.RUNNING; this.nextMove(true); @@ -379,29 +212,17 @@ public class GameLogic extends JPanel { this.playerMove = playerMove; if (this.playerMove) { - gui.updateField(this.enemyMatchfield); + gui.updateFields(); // waiting for mouse click event.... } else { - gui.updateField(this.matchfield); - - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } Coordinate aiChosenField = this.aiLogic.chooseField(); + aiChosenField.print(); aiChosenField.shoot(); - gui.updateField(this.matchfield); + gui.updateFields(); boolean aiHasNextMove = aiChosenField.getState() == Coordinate.HIT; - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - this.nextMove(aiHasNextMove); } diff --git a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java index 87786d4..07e2aea 100644 --- a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test; class LogicTest { - GameLogic guiLogic = new GameLogic(0, 0, 0); + GameLogic guiLogic = new GameLogic(0, 0, 0, 0); @BeforeEach void setup() { From 53bc0d9064f52f10343557b67c4da81bffab3a59 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Mon, 17 Jan 2022 13:47:48 +0100 Subject: [PATCH 82/93] chooseField still works after returning null --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index e5c923d..3b82500 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -35,7 +35,7 @@ public class Logic { if (!clearedAbove) { target = matchfield.getAbove(target); out = target; - if (target.getState() == Coordinate.EMPTY) { + if (target.getState() == Coordinate.EMPTY || target == null) { clearedAbove = true; target = lastShot; } @@ -45,7 +45,7 @@ public class Logic { if (!clearedBelow) { target = matchfield.getBelow(target); out = target; - if (target.getState() == Coordinate.EMPTY) { + if (target.getState() == Coordinate.EMPTY || target == null) { clearedBelow = true; target = lastShot; } @@ -57,7 +57,7 @@ public class Logic { if (!clearedRight) { target = matchfield.getRight(target); out = target; - if (target.getState() == Coordinate.EMPTY) { + if (target.getState() == Coordinate.EMPTY || target == null) { clearedRight = true; target = lastShot; } @@ -67,7 +67,7 @@ public class Logic { if (!clearedLeft) { target = matchfield.getLeft(target); out = target; - if (target.getState() == Coordinate.EMPTY) { + if (target.getState() == Coordinate.EMPTY || target == null) { clearedLeft = true; target = lastShot; } From dd2ddc5811868e8943ca05b92e5fbdebd72b24d3 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 13:59:56 +0100 Subject: [PATCH 83/93] Fixed chooseField returns null --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 3b82500..a20c9e0 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -35,7 +35,7 @@ public class Logic { if (!clearedAbove) { target = matchfield.getAbove(target); out = target; - if (target.getState() == Coordinate.EMPTY || target == null) { + if (target == null || target.getState() == Coordinate.EMPTY) { clearedAbove = true; target = lastShot; } @@ -45,7 +45,7 @@ public class Logic { if (!clearedBelow) { target = matchfield.getBelow(target); out = target; - if (target.getState() == Coordinate.EMPTY || target == null) { + if (target == null || target.getState() == Coordinate.EMPTY) { clearedBelow = true; target = lastShot; } @@ -57,7 +57,7 @@ public class Logic { if (!clearedRight) { target = matchfield.getRight(target); out = target; - if (target.getState() == Coordinate.EMPTY || target == null) { + if (target == null || target.getState() == Coordinate.EMPTY) { clearedRight = true; target = lastShot; } @@ -67,7 +67,7 @@ public class Logic { if (!clearedLeft) { target = matchfield.getLeft(target); out = target; - if (target.getState() == Coordinate.EMPTY || target == null) { + if (target == null || target.getState() == Coordinate.EMPTY) { clearedLeft = true; target = lastShot; } From 629063b5ffcffa6b4ae9b148f1333b60ae89ef00 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 14:11:57 +0100 Subject: [PATCH 84/93] Fixed that AI has next move when ship was hit --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 6 ++++-- src/main/java/de/tims/fleetstorm/gui/GameLogic.java | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index a20c9e0..5c4be84 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -93,8 +93,10 @@ public class Logic { } public void isShipOnYAxis() { - if ((clearedAbove && clearedBelow) && ((matchfield.getAbove(lastShot).getState() == Coordinate.HIT) - || (matchfield.getBelow(lastShot).getState() == Coordinate.HIT))) { + if ((clearedAbove && clearedBelow) && ((matchfield.getAbove(lastShot) == null + || (matchfield.getAbove(lastShot).getState() == Coordinate.HIT)) + || (matchfield.getBelow(lastShot) == null + || (matchfield.getBelow(lastShot).getState() == Coordinate.HIT)))) { clearedLeft = true; clearedRight = true; sinkShip(); diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 414fd36..0bf44d3 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -216,12 +216,16 @@ public class GameLogic extends JPanel { // waiting for mouse click event.... } else { - Coordinate aiChosenField = this.aiLogic.chooseField(); + Coordinate aiChosenField = null; + while (aiChosenField == null) { + aiChosenField = this.aiLogic.chooseField(); + } + aiChosenField.print(); aiChosenField.shoot(); gui.updateFields(); - boolean aiHasNextMove = aiChosenField.getState() == Coordinate.HIT; + boolean aiHasNextMove = aiChosenField.getState() != Coordinate.HIT; this.nextMove(aiHasNextMove); } From 5d039543b7ffe8de6b8aae6b1a0788b264797a49 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 14:20:27 +0100 Subject: [PATCH 85/93] GUI Refactoring --- .../de/tims/fleetstorm/gui/GameLogic.java | 68 ++++++++----------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 0bf44d3..557ce00 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -35,6 +35,7 @@ public class GameLogic extends JPanel { public GameLogic(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween) { this.playerFields = new ArrayList<>(); this.enemyFields = new ArrayList<>(); + setSize(640, 480); JPanel fieldWrapper = new JPanel(); @@ -64,8 +65,9 @@ public class GameLogic extends JPanel { private void generateFieldsForGUI(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween, JPanel panel, ArrayList fields, boolean registerMouseListener) { - for (int x = 0; x < 10; x++) { - for (int y = 0; y < 10; y++) { + + for (int x = 0; x < this.matchfieldSize; x++) { + for (int y = 0; y < this.matchfieldSize; y++) { JPanel field = new JPanel(); int xPos = gapToFrameBorderX + x * fieldWidth + (x * spaceBetween); @@ -74,47 +76,40 @@ public class GameLogic extends JPanel { field.setBounds(xPos, yPos, fieldWidth, fieldWidth); field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); field.setName(x + ":" + y); + fields.add(field); panel.add(field); - if (registerMouseListener) { - field.addMouseListener(new MouseAdapter() { + if (!registerMouseListener) + continue; - public void mouseClicked(MouseEvent e) { - System.out.println("mouse clicked"); - if (!playerMove) - return; + field.addMouseListener(new MouseAdapter() { - int[] coords = getCoordinateFromLabel(field); - int coordX = coords[0]; - int coordY = coords[1]; + public void mouseClicked(MouseEvent e) { + int[] coords = getCoordinateFromLabel(field); + int coordX = coords[0]; + int coordY = coords[1]; - Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); + Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); - boolean shotSuccess = chosenField.shoot(); - gui.updateFields(); - - if (shotSuccess && chosenField.getState() != Coordinate.HIT) { - nextMove(false); - } else { - nextMove(true); - } + boolean shotSuccess = chosenField.shoot(); + if (shotSuccess && chosenField.getState() != Coordinate.HIT) { + nextMove(false); + } else { + nextMove(true); } - }); - } + + } + }); + } } } - public ArrayList getFields() { - return playerFields; - } - private JLabel getXLabel() { JLabel x = new JLabel("x"); x.setForeground(Color.WHITE); - return x; } @@ -185,7 +180,7 @@ public class GameLogic extends JPanel { this.gameState = GameLogic.PREPARATION; this.playerMove = true; - // create player matchfield + // create player matchfield and set ships this.matchfield = new Matchfield(matchfieldSize); this.matchfield.createMatchfield(); this.matchfield.setShip(new Coordinate(4, 6), 2, 0); @@ -193,15 +188,17 @@ public class GameLogic extends JPanel { this.matchfield.setShip(new Coordinate(9, 1), 4, 1); this.matchfield.setShip(new Coordinate(0, 0), 5, 0); - // create enemy matchfield and initialize AI (with player's matchfield) + this.aiLogic = new Logic(); + this.aiLogic.setMatchfield(this.matchfield); + + // create enemy matchfield, set ships and initialize AI (with player's + // matchfield) this.enemyMatchfield = new Matchfield(matchfieldSize); this.enemyMatchfield.createMatchfield(); this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); - this.aiLogic = new Logic(); - this.aiLogic.setMatchfield(this.matchfield); // enter game loop this.gameState = GameLogic.RUNNING; @@ -213,7 +210,7 @@ public class GameLogic extends JPanel { if (this.playerMove) { gui.updateFields(); - // waiting for mouse click event.... + // waiting here for mouse click event.... } else { Coordinate aiChosenField = null; @@ -221,12 +218,11 @@ public class GameLogic extends JPanel { aiChosenField = this.aiLogic.chooseField(); } - aiChosenField.print(); aiChosenField.shoot(); + gui.updateFields(); boolean aiHasNextMove = aiChosenField.getState() != Coordinate.HIT; - this.nextMove(aiHasNextMove); } @@ -235,8 +231,4 @@ public class GameLogic extends JPanel { public int getGameState() { return gameState; } - - public boolean isPlayerMove() { - return playerMove; - } } From 805c233108b2c56332bc7a7e9b7dbb5efd8b0be4 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 14:37:39 +0100 Subject: [PATCH 86/93] Fixed GameLogicTest with new test parameter in start() function --- src/main/java/de/tims/fleetstorm/gui/GameLogic.java | 7 ++++--- src/test/java/de/tims/fleetstorm/gui/LogicTest.java | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 557ce00..865e59c 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -173,10 +173,10 @@ public class GameLogic extends JPanel { frame.setResizable(true); frame.setVisible(true); - gui.start(); + gui.start(false); } - public void start() { + public void start(boolean test) { this.gameState = GameLogic.PREPARATION; this.playerMove = true; @@ -202,7 +202,8 @@ public class GameLogic extends JPanel { // enter game loop this.gameState = GameLogic.RUNNING; - this.nextMove(true); + if (!test) + this.nextMove(true); } public void nextMove(boolean playerMove) { diff --git a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java index 07e2aea..2b1e357 100644 --- a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -11,7 +11,7 @@ class LogicTest { @BeforeEach void setup() { - guiLogic.start(); + guiLogic.start(true); } @Test From abeaff8d7b9fada908b7c1aff6249d28ea194f58 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 14:59:00 +0100 Subject: [PATCH 87/93] Implemented areAllShipsHit() with first test case if no ship was hit --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 5 ++++- ...PositioningTest.java => MatchfieldShipTest.java} | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) rename src/test/java/de/tims/fleetstorm/matchfield/{MatchfieldPositioningTest.java => MatchfieldShipTest.java} (91%) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index f9fcd5e..a3e4abe 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -41,7 +41,6 @@ public class Matchfield { } public Coordinate getField(int x, int y) { - return this.matchfield[x][y]; } @@ -97,4 +96,8 @@ public class Matchfield { return true; } + public boolean areAllShipsHit() { + return false; + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java similarity index 91% rename from src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java rename to src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 6ad6bb7..5052bf3 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -11,7 +11,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -class MatchfieldPositioningTest { +class MatchfieldShipTest { @ParameterizedTest(name = "ship was set on the correct positions") @MethodSource("testShipPositioning") @@ -82,4 +82,15 @@ class MatchfieldPositioningTest { assertTrue(matchfield.setShip(new Coordinate(1, 1), 4, 0)); assertFalse(matchfield.setShip(new Coordinate(4, 1), 2, 1)); } + + @Test + void testIfAllShipsOnMatchfieldHit() { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + matchfield.setShip(new Coordinate(0, 0), 5, 1); + + boolean calulatedResult = matchfield.areAllShipsHit(); + assertFalse(calulatedResult); + } } From 27e7a96ba73467d5c48fd89d85e4b713d7fd1bc9 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 15:02:25 +0100 Subject: [PATCH 88/93] Added second test case for areAllShipsHit() - all ships hit test --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 12 +++++++++++- .../fleetstorm/matchfield/MatchfieldShipTest.java | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index a3e4abe..cca91c9 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -97,7 +97,17 @@ public class Matchfield { } public boolean areAllShipsHit() { - return false; + int shipCounter = 0; + + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + int fieldState = this.matchfield[i][j].getState(); + if (fieldState == Coordinate.SHIP) + shipCounter++; + } + } + + return shipCounter == 0; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 5052bf3..0d08687 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -92,5 +92,13 @@ class MatchfieldShipTest { boolean calulatedResult = matchfield.areAllShipsHit(); assertFalse(calulatedResult); + + matchfield.getField(0, 0).setState(Coordinate.HIT); + matchfield.getField(0, 1).setState(Coordinate.HIT); + matchfield.getField(0, 2).setState(Coordinate.HIT); + matchfield.getField(0, 3).setState(Coordinate.HIT); + matchfield.getField(0, 4).setState(Coordinate.HIT); + calulatedResult = matchfield.areAllShipsHit(); + assertTrue(calulatedResult); } } From aae87e50e163a6d9db0e7b5b5749ec35ec0f89d6 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 15:11:38 +0100 Subject: [PATCH 89/93] Refactoring MatchfieldShipTest class: added description for tests and outsourced multiple tests into more functions --- .../matchfield/MatchfieldShipTest.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 0d08687..9948134 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -1,6 +1,7 @@ package de.tims.fleetstorm.matchfield; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -28,7 +29,7 @@ class MatchfieldShipTest { shipSetCounter++; } - assertTrue(shipSetCounter == length); + assertThat(shipSetCounter).describedAs(testName).isEqualTo(length); } static Stream testShipPositioning() { @@ -57,10 +58,10 @@ class MatchfieldShipTest { boolean calculatedResult = matchfield.setShip(coordinate, length, direction); if (expectedResult) - assertTrue(calculatedResult); + assertThat(calculatedResult).describedAs(testName).isEqualTo(true); if (!expectedResult) - assertFalse(calculatedResult); + assertThat(calculatedResult).describedAs(testName).isEqualTo(false); } static Stream testShipPositioningFailed() { @@ -77,28 +78,38 @@ class MatchfieldShipTest { matchfield.createMatchfield(); matchfield.setShip(new Coordinate(0, 0), 5, 1); - assertFalse(matchfield.setShip(new Coordinate(0, 1), 5, 1)); + assertThat(matchfield.setShip(new Coordinate(0, 1), 5, 1)).describedAs("set ship on coordinate with ship") + .isEqualTo(false); - assertTrue(matchfield.setShip(new Coordinate(1, 1), 4, 0)); - assertFalse(matchfield.setShip(new Coordinate(4, 1), 2, 1)); + assertThat(matchfield.setShip(new Coordinate(1, 1), 4, 0)).describedAs("set ship on coordinate without ship") + .isEqualTo(true); + assertThat(matchfield.setShip(new Coordinate(4, 1), 2, 1)) + .describedAs("set second ship on coordinate without ship").isEqualTo(false); } @Test - void testIfAllShipsOnMatchfieldHit() { + void testIfAllShipsHitReturnsFalseWhenNoShipIsHit() { Matchfield matchfield = new Matchfield(10); matchfield.createMatchfield(); matchfield.setShip(new Coordinate(0, 0), 5, 1); - boolean calulatedResult = matchfield.areAllShipsHit(); assertFalse(calulatedResult); + } + @Test + void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + matchfield.setShip(new Coordinate(0, 0), 5, 1); matchfield.getField(0, 0).setState(Coordinate.HIT); matchfield.getField(0, 1).setState(Coordinate.HIT); matchfield.getField(0, 2).setState(Coordinate.HIT); matchfield.getField(0, 3).setState(Coordinate.HIT); matchfield.getField(0, 4).setState(Coordinate.HIT); - calulatedResult = matchfield.areAllShipsHit(); + + boolean calulatedResult = matchfield.areAllShipsHit(); assertTrue(calulatedResult); } } From 8e92a1caee506910809d395516da3697fb6b6ef4 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 15:14:18 +0100 Subject: [PATCH 90/93] Added third test for areAllShipsHit() when a ship is not fully hit --- .../matchfield/MatchfieldShipTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 9948134..cdeb007 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -112,4 +112,23 @@ class MatchfieldShipTest { boolean calulatedResult = matchfield.areAllShipsHit(); assertTrue(calulatedResult); } + + @Test + void testIfAllShipsHitReturnsFalseWhenTwoShipsAreNotFullyHit() { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + matchfield.setShip(new Coordinate(0, 0), 5, 1); + matchfield.getField(0, 0).setState(Coordinate.HIT); + matchfield.getField(0, 1).setState(Coordinate.HIT); + matchfield.getField(0, 2).setState(Coordinate.HIT); + matchfield.getField(0, 3).setState(Coordinate.HIT); + matchfield.getField(0, 4).setState(Coordinate.HIT); + + matchfield.setShip(new Coordinate(3, 4), 2, 0); + matchfield.getField(4, 4).setState(Coordinate.HIT); + + boolean calulatedResult = matchfield.areAllShipsHit(); + assertFalse(calulatedResult); + } } From bed8f7187929d1473d1530b2904e6ce4a5341e81 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 15:32:58 +0100 Subject: [PATCH 91/93] Implemented gameOver() function in GUI with test case --- .../de/tims/fleetstorm/gui/GameLogic.java | 24 ++++++++++++++++--- .../de/tims/fleetstorm/gui/LogicTest.java | 10 ++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 865e59c..04c82ee 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -26,7 +26,7 @@ public class GameLogic extends JPanel { public static final int PREPARATION = 1; public static final int RUNNING = 2; - public static final int OVER = 3; + public static final int GAME_OVER = 3; // GUI stuff private ArrayList playerFields; @@ -38,13 +38,13 @@ public class GameLogic extends JPanel { setSize(640, 480); - JPanel fieldWrapper = new JPanel(); + fieldWrapper = new JPanel(); fieldWrapper.setBounds(10, 11, 305, 458); fieldWrapper.setLayout(null); setLayout(null); add(fieldWrapper); - JPanel enemyFieldWrapper = new JPanel(); + enemyFieldWrapper = new JPanel(); enemyFieldWrapper.setLayout(null); enemyFieldWrapper.setBounds(326, 11, 305, 458); add(enemyFieldWrapper); @@ -94,6 +94,12 @@ public class GameLogic extends JPanel { boolean shotSuccess = chosenField.shoot(); + boolean areAllShipsHit = enemyMatchfield.areAllShipsHit(); + if (areAllShipsHit) { + gameOver(true); + return; + } + if (shotSuccess && chosenField.getState() != Coordinate.HIT) { nextMove(false); } else { @@ -163,6 +169,8 @@ public class GameLogic extends JPanel { * This function is only for testing */ private static GameLogic gui; + private JPanel fieldWrapper; + private JPanel enemyFieldWrapper; public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); @@ -223,12 +231,22 @@ public class GameLogic extends JPanel { gui.updateFields(); + boolean areAllShipsHit = this.matchfield.areAllShipsHit(); + if (areAllShipsHit) { + this.gameOver(false); + return; + } + boolean aiHasNextMove = aiChosenField.getState() != Coordinate.HIT; this.nextMove(aiHasNextMove); } } + protected void gameOver(boolean playerWinner) { + this.gameState = GameLogic.GAME_OVER; + } + public int getGameState() { return gameState; } diff --git a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java index 2b1e357..4b02637 100644 --- a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -23,4 +23,14 @@ class LogicTest { assertEquals(expectedState, calculatedState); } + @Test + void testIfGameStateIsGameOverAfterGameOverFunction() { + int expectedState = GameLogic.GAME_OVER; + + guiLogic.gameOver(true); + int calculatedState = guiLogic.getGameState(); + + assertEquals(expectedState, calculatedState); + } + } From d5d6af193e886d31ddcba08b50dfbe95c7915e15 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 15:38:43 +0100 Subject: [PATCH 92/93] Beautified GUI Won Screen --- .../java/de/tims/fleetstorm/gui/GameLogic.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 04c82ee..2bc34d5 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -1,6 +1,8 @@ package de.tims.fleetstorm.gui; +import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Font; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; @@ -8,6 +10,7 @@ import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.SwingConstants; import javax.swing.border.MatteBorder; import de.tims.fleetstorm.ai.Logic; @@ -245,6 +248,18 @@ public class GameLogic extends JPanel { protected void gameOver(boolean playerWinner) { this.gameState = GameLogic.GAME_OVER; + + this.removeAll(); + this.setLayout(new BorderLayout()); + + JLabel wonLabel = new JLabel(playerWinner ? "Du hast gewonnen!" : "Du hast verloren!"); + wonLabel.setHorizontalAlignment(SwingConstants.CENTER); + wonLabel.setVerticalAlignment(SwingConstants.CENTER); + wonLabel.setFont(new Font("Tahoma", Font.BOLD, 18)); + this.add(wonLabel, SwingConstants.CENTER); + + this.revalidate(); + this.repaint(); } public int getGameState() { From 353c68c35867e57d918907c9d8d86edbeb0890e1 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Tue, 18 Jan 2022 18:07:55 +0100 Subject: [PATCH 93/93] Fixed JUnit Imports (Maven didn't compile correctly) --- .../java/de/tims/fleetstorm/matchfield/CoordinateTest.java | 4 +--- .../de/tims/fleetstorm/matchfield/MatchfieldShipTest.java | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 1b9a560..196f4c2 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -1,9 +1,7 @@ package de.tims.fleetstorm.matchfield; +import static org.junit.jupiter.api.Assertions.*; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.stream.Stream; diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index cdeb007..665f367 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -1,9 +1,8 @@ package de.tims.fleetstorm.matchfield; +import static org.junit.jupiter.api.Assertions.*; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import java.util.stream.Stream;