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