From 2ace6f42a9e2ccc592b18d6b60afbd0e8258ad86 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Thu, 13 Jan 2022 13:54:27 +0100 Subject: [PATCH] Refactoring: chooseField() doesn't need param matchfield anymore --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 9 +++++---- src/test/java/de/tims/fleetstorm/ai/LogicTest.java | 6 ++++-- 2 files changed, 9 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 e80125d..668984b 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -11,11 +11,12 @@ public class Logic { private Matchfield matchfield; private Coordinate lastShot; - public Coordinate chooseField(Matchfield matchfield) { + public Coordinate chooseField() { ArrayList possibleFields = new ArrayList(); - for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { - for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { - if (matchfield.getState(x, y) == Coordinate.EMPTY || matchfield.getState(x, y) == Coordinate.SHIP) { + 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)); } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 0c47f54..440710d 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -20,7 +20,8 @@ class LogicTest { int size = 5; matchfield = new Matchfield(size); matchfield.createMatchfield(); - Coordinate calcResult = logic.chooseField(matchfield); + logic.setMatchfield(matchfield); + Coordinate calcResult = logic.chooseField(); assertNotNull(calcResult); } @@ -31,6 +32,7 @@ class LogicTest { int size = 5; matchfield = new Matchfield(size); matchfield.createMatchfield(); + logic.setMatchfield(matchfield); for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { @@ -39,7 +41,7 @@ class LogicTest { } matchfield.setState(2, 2, Coordinate.EMPTY); - Coordinate choosenField = logic.chooseField(matchfield); + Coordinate choosenField = logic.chooseField(); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); }