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