From ed7474228a523caf7150def32a72f491f7eb4412 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Fri, 14 Jan 2022 13:28:10 +0100 Subject: [PATCH] Refactoring: add Constructor to set DefaultValues --- .../java/de/tims/fleetstorm/ai/Logic.java | 33 +++++++++++-------- .../java/de/tims/fleetstorm/ai/LogicTest.java | 24 ++++++++++---- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index b05558e..98db585 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -11,20 +11,28 @@ public class Logic { private Matchfield matchfield; private Coordinate lastShot; private Coordinate target; - private boolean foundShip = false; - private boolean clearedAbove = false; - private boolean clearedBelow = false; - private boolean clearedRight = false; - private boolean clearedLeft = false; + private boolean foundShip; + private boolean clearedAbove; + private boolean clearedBelow; + private boolean clearedRight; + private boolean clearedLeft; + + public Logic() { + foundShip = false; + clearedAbove = false; + clearedBelow = false; + clearedRight = false; + clearedLeft = false; + + } + + public Coordinate chooseField(ArrayList coordinates) { - public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); - for (int x = 0; x < Math.sqrt(this.matchfield.getSize()); x++) { - for (int y = 0; y < Math.sqrt(this.matchfield.getSize()); y++) { - if (this.matchfield.getState(x, y) == Coordinate.EMPTY - || this.matchfield.getState(x, y) == Coordinate.SHIP) { - possibleFields.add(new Coordinate(x, y)); - } + + 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)); } } Random randy = new Random(); @@ -68,7 +76,6 @@ public class Logic { foundShip = true; } - System.out.println("Do this"); } public void setFoundShip(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 dbce6ea..b89fe84 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -21,7 +21,9 @@ class LogicTest { matchfield = new Matchfield(size); matchfield.createMatchfield(); logic.setMatchfield(matchfield); - Coordinate calcResult = logic.chooseField(); + ArrayList everySecondField = logic.getEverySecondField(); + + Coordinate calcResult = logic.chooseField(everySecondField); assertNotNull(calcResult); } @@ -34,14 +36,13 @@ class LogicTest { matchfield.createMatchfield(); logic.setMatchfield(matchfield); - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - matchfield.setState(x, y, Coordinate.SHOT); - } + ArrayList everySecondField = logic.getEverySecondField(); + for (int i = 0; i < everySecondField.size(); i++) { + everySecondField.get(i).setState(Coordinate.SHOT); } matchfield.setState(2, 2, Coordinate.EMPTY); - Coordinate choosenField = logic.chooseField(); + Coordinate choosenField = logic.chooseField(everySecondField); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); } @@ -191,4 +192,15 @@ class LogicTest { assertEquals(logic.getClearedLeft(), true); } + + @Test + void testConstructor() { + Logic logic = new Logic(); + + assertEquals(logic.getFoundShip(), false); + assertEquals(logic.getClearedAbove(), false); + assertEquals(logic.getClearedBelow(), false); + assertEquals(logic.getClearedLeft(), false); + assertEquals(logic.getClearedRight(), false); + } }