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