From dd1a7a34af8a2fdba262a9ed11a32069c8862b4d Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 14:56:55 +0100 Subject: [PATCH] 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))); + } }