Browse Source

Refactoring: chooseField doesent need Params now

fleetstorm
Max Wenzel 3 years ago
committed by Lorenz Hohmann
parent
commit
3c1e3d19e5
  1. 13
      src/main/java/de/tims/fleetstorm/ai/Logic.java
  2. 25
      src/test/java/de/tims/fleetstorm/ai/LogicTest.java

13
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<Coordinate> everySecondField;
private Coordinate lastShot;
private Coordinate target;
private boolean foundShip;
@ -26,7 +27,7 @@ public class Logic {
}
public Coordinate chooseField(ArrayList<Coordinate> 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<Coordinate> possibleFields = new ArrayList<Coordinate>();
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() {

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

@ -23,7 +23,7 @@ class LogicTest {
logic.setMatchfield(matchfield);
ArrayList<Coordinate> 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);
}
}
Loading…
Cancel
Save