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