From c3bb8f27b9c860e09218af301dde1d47e80bcf27 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 17:29:16 +0100 Subject: [PATCH] Refactoring: chooseField doesent need Params now --- .../java/de/tims/fleetstorm/ai/Logic.java | 13 +++++++--- .../java/de/tims/fleetstorm/ai/LogicTest.java | 25 ++++++++++--------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 36a3cc4..7accfde 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -9,6 +9,7 @@ import de.tims.fleetstorm.matchfield.Matchfield; public class Logic { private Matchfield matchfield; + private ArrayList everySecondField; private Coordinate lastShot; private Coordinate target; private boolean foundShip; @@ -26,7 +27,7 @@ public class Logic { } - public Coordinate chooseField(ArrayList coordinates) { + public Coordinate chooseField() { if (foundShip) { if (!clearedAbove) { @@ -47,6 +48,8 @@ public class Logic { return target; } + // if() + target = lastShot; if (!clearedRight) { @@ -71,9 +74,10 @@ public class Logic { sinkShip(); ArrayList possibleFields = new ArrayList(); - for (int i = 0; i < coordinates.size(); i++) { - if (coordinates.get(i).getState() != Coordinate.SHOT && coordinates.get(i).getState() != Coordinate.HIT) { - possibleFields.add(coordinates.get(i)); + for (int i = 0; i < everySecondField.size(); i++) { + if (everySecondField.get(i).getState() != Coordinate.SHOT + && everySecondField.get(i).getState() != Coordinate.HIT) { + possibleFields.add(everySecondField.get(i)); } } Random randy = new Random(); @@ -201,6 +205,7 @@ public class Logic { public void setMatchfield(Matchfield matchfield) { this.matchfield = matchfield; + this.everySecondField = getEverySecondField(); } public Matchfield getMatchfield() { diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index ac4e835..f1d44b3 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -23,7 +23,7 @@ class LogicTest { logic.setMatchfield(matchfield); ArrayList everySecondField = logic.getEverySecondField(); - Coordinate calcResult = logic.chooseField(everySecondField); + Coordinate calcResult = logic.chooseField(); assertNotNull(calcResult); } @@ -42,7 +42,7 @@ class LogicTest { } matchfield.setState(2, 2, Coordinate.EMPTY); - Coordinate choosenField = logic.chooseField(everySecondField); + Coordinate choosenField = logic.chooseField(); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); } @@ -270,7 +270,7 @@ class LogicTest { logic.setTarget(center); logic.setFoundShip(true); - Coordinate result = logic.chooseField(logic.getEverySecondField()); + Coordinate result = logic.chooseField(); assertEquals(result, expectedResult); } @@ -289,8 +289,8 @@ class LogicTest { logic.setTarget(center); logic.setFoundShip(true); - logic.chooseField(logic.getEverySecondField()); // first Shot - Coordinate result = logic.chooseField(logic.getEverySecondField()); // second Shot + logic.chooseField(); // first Shot + Coordinate result = logic.chooseField(); // second Shot assertEquals(result, expectedResult); } @@ -310,9 +310,9 @@ class LogicTest { logic.setTarget(center); logic.setFoundShip(true); - logic.chooseField(logic.getEverySecondField()); // first Shot - logic.chooseField(logic.getEverySecondField()); // second Shot - Coordinate result = logic.chooseField(logic.getEverySecondField()); + logic.chooseField(); // first Shot + logic.chooseField(); // second Shot + Coordinate result = logic.chooseField(); assertEquals(result, expectedResult); } @@ -332,11 +332,12 @@ class LogicTest { 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()); + logic.chooseField(); // first Shot + logic.chooseField(); // second Shot + logic.chooseField(); // third Shot + Coordinate result = logic.chooseField(); assertEquals(result, expectedResult); } + }