From 79a7556286964000cd4798cc2b1a5f21d673c9aa Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Sat, 15 Jan 2022 12:12:16 +0100 Subject: [PATCH] if ShipIsOnYAxis, ai doesent check XAxis --- .../java/de/tims/fleetstorm/ai/Logic.java | 46 ++++++++++++------ .../fleetstorm/matchfield/Coordinate.java | 4 ++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 47 ++++++++++++++++--- 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 7accfde..e5c923d 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -10,6 +10,7 @@ public class Logic { private Matchfield matchfield; private ArrayList everySecondField; + private Coordinate lastShot; private Coordinate target; private boolean foundShip; @@ -24,55 +25,57 @@ public class Logic { clearedBelow = false; clearedRight = false; clearedLeft = false; - } public Coordinate chooseField() { + Coordinate out; if (foundShip) { + // Clear Y-Axis if (!clearedAbove) { target = matchfield.getAbove(target); + out = target; if (target.getState() == Coordinate.EMPTY) { clearedAbove = true; + target = lastShot; } - return target; + return out; } - target = lastShot; - if (!clearedBelow) { target = matchfield.getBelow(target); + out = target; if (target.getState() == Coordinate.EMPTY) { clearedBelow = true; + target = lastShot; } - return target; + isShipOnYAxis(); + return out; } - // if() - - target = lastShot; - + // Clear x-Axis if (!clearedRight) { target = matchfield.getRight(target); + out = target; if (target.getState() == Coordinate.EMPTY) { clearedRight = true; + target = lastShot; } - return target; + return out; } - target = lastShot; - if (!clearedLeft) { target = matchfield.getLeft(target); + out = target; if (target.getState() == Coordinate.EMPTY) { clearedLeft = true; + target = lastShot; } - return target; + sinkShip(); + return out; } } - sinkShip(); - ArrayList possibleFields = new ArrayList(); for (int i = 0; i < everySecondField.size(); i++) { if (everySecondField.get(i).getState() != Coordinate.SHOT @@ -89,6 +92,15 @@ public class Logic { return lastShot; } + public void isShipOnYAxis() { + if ((clearedAbove && clearedBelow) && ((matchfield.getAbove(lastShot).getState() == Coordinate.HIT) + || (matchfield.getBelow(lastShot).getState() == Coordinate.HIT))) { + clearedLeft = true; + clearedRight = true; + sinkShip(); + } + } + public ArrayList getEverySecondField() { ArrayList out = new ArrayList(); for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) { @@ -147,6 +159,10 @@ public class Logic { // Getter And Setter + public void setEverySecondField(ArrayList everySecondField) { + this.everySecondField = everySecondField; + } + public void setLastShot(Coordinate coordinate) { lastShot = this.matchfield.getField(coordinate); } diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index 705c77c..a11c666 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -40,4 +40,8 @@ public class Coordinate { return this.x == ((Coordinate) obj).getX() && this.y == ((Coordinate) obj).getY(); } + public void print() { + System.out.println("X = " + this.x + ", Y = " + this.y + ", State = " + this.state); + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index f1d44b3..79935b5 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -21,7 +21,7 @@ class LogicTest { matchfield = new Matchfield(size); matchfield.createMatchfield(); logic.setMatchfield(matchfield); - ArrayList everySecondField = logic.getEverySecondField(); + // ArrayList everySecondField = logic.getEverySecondField(); Coordinate calcResult = logic.chooseField(); assertNotNull(calcResult); @@ -41,9 +41,9 @@ class LogicTest { everySecondField.get(i).setState(Coordinate.SHOT); } matchfield.setState(2, 2, Coordinate.EMPTY); + logic.setEverySecondField(everySecondField); - Coordinate choosenField = logic.chooseField(); - int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); + int calcState = logic.chooseField().getState(); assertNotEquals(calcState, Coordinate.SHOT); } @@ -288,8 +288,7 @@ class LogicTest { logic.setLastShot(center); logic.setTarget(center); logic.setFoundShip(true); - - logic.chooseField(); // first Shot + logic.chooseField();// first Shot Coordinate result = logic.chooseField(); // second Shot assertEquals(result, expectedResult); @@ -334,10 +333,44 @@ class LogicTest { logic.chooseField(); // first Shot logic.chooseField(); // second Shot - logic.chooseField(); // third Shot + logic.chooseField(); // third Shot Coordinate Coordinate result = logic.chooseField(); - assertEquals(result, expectedResult); } + @Test + void testShipIsOnYAxis() { + int size = 10; + Logic logic = new Logic(); + Matchfield matchfield = new Matchfield(size); + Coordinate s1 = new Coordinate(5, 4); + Coordinate s2 = new Coordinate(5, 5); + Coordinate s3 = new Coordinate(5, 6); + + matchfield.createMatchfield(); + matchfield.setState(s1, Coordinate.SHIP); + matchfield.setState(s2, Coordinate.HIT); + matchfield.setState(s3, Coordinate.SHIP); + + logic.setMatchfield(matchfield); + logic.setLastShot(s2); + logic.setTarget(s2); + logic.setFoundShip(true); + + logic.chooseField(); // First shot (s3) + logic.getMatchfield().setState(s3, Coordinate.HIT); // + logic.chooseField(); // Second Shot + logic.getMatchfield().setState(new Coordinate(5, 7), Coordinate.SHOT); + + logic.chooseField(); // Third Shot (s1) + logic.getMatchfield().setState(s1, Coordinate.HIT); // + logic.chooseField(); // fourth Shot + + assertEquals(logic.getFoundShip(), false); + assertEquals(logic.getClearedAbove(), false); + assertEquals(logic.getClearedBelow(), false); + assertEquals(logic.getClearedLeft(), false); + assertEquals(logic.getClearedRight(), false); + + } }