Browse Source

Refactoring: chooseField() doesn't need param matchfield anymore

Max Wenzel 3 years ago
parent
commit
6a23fedd61
  1. 9
      src/main/java/de/tims/fleetstorm/ai/Logic.java
  2. 6
      src/test/java/de/tims/fleetstorm/ai/LogicTest.java

9
src/main/java/de/tims/fleetstorm/ai/Logic.java

@ -11,11 +11,12 @@ public class Logic {
private Matchfield matchfield; private Matchfield matchfield;
private Coordinate lastShot; private Coordinate lastShot;
public Coordinate chooseField(Matchfield matchfield) {
public Coordinate chooseField() {
ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>(); ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>();
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)); possibleFields.add(new Coordinate(x, y));
} }
} }

6
src/test/java/de/tims/fleetstorm/ai/LogicTest.java

@ -20,7 +20,8 @@ class LogicTest {
int size = 5; int size = 5;
matchfield = new Matchfield(size); matchfield = new Matchfield(size);
matchfield.createMatchfield(); matchfield.createMatchfield();
Coordinate calcResult = logic.chooseField(matchfield);
logic.setMatchfield(matchfield);
Coordinate calcResult = logic.chooseField();
assertNotNull(calcResult); assertNotNull(calcResult);
} }
@ -31,6 +32,7 @@ class LogicTest {
int size = 5; int size = 5;
matchfield = new Matchfield(size); matchfield = new Matchfield(size);
matchfield.createMatchfield(); matchfield.createMatchfield();
logic.setMatchfield(matchfield);
for (int x = 0; x < size; x++) { for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) { for (int y = 0; y < size; y++) {
@ -39,7 +41,7 @@ class LogicTest {
} }
matchfield.setState(2, 2, Coordinate.EMPTY); matchfield.setState(2, 2, Coordinate.EMPTY);
Coordinate choosenField = logic.chooseField(matchfield);
Coordinate choosenField = logic.chooseField();
int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY());
assertNotEquals(calcState, Coordinate.SHOT); assertNotEquals(calcState, Coordinate.SHOT);
} }

Loading…
Cancel
Save