Browse Source

Refactoring: chooseField doesent need Params now

Max Wenzel 3 years ago
parent
commit
c3bb8f27b9
  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 { public class Logic {
private Matchfield matchfield; private Matchfield matchfield;
private ArrayList<Coordinate> everySecondField;
private Coordinate lastShot; private Coordinate lastShot;
private Coordinate target; private Coordinate target;
private boolean foundShip; private boolean foundShip;
@ -26,7 +27,7 @@ public class Logic {
} }
public Coordinate chooseField(ArrayList<Coordinate> coordinates) {
public Coordinate chooseField() {
if (foundShip) { if (foundShip) {
if (!clearedAbove) { if (!clearedAbove) {
@ -47,6 +48,8 @@ public class Logic {
return target; return target;
} }
// if()
target = lastShot; target = lastShot;
if (!clearedRight) { if (!clearedRight) {
@ -71,9 +74,10 @@ public class Logic {
sinkShip(); sinkShip();
ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>(); 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(); Random randy = new Random();
@ -201,6 +205,7 @@ public class Logic {
public void setMatchfield(Matchfield matchfield) { public void setMatchfield(Matchfield matchfield) {
this.matchfield = matchfield; this.matchfield = matchfield;
this.everySecondField = getEverySecondField();
} }
public Matchfield getMatchfield() { public Matchfield getMatchfield() {

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

@ -23,7 +23,7 @@ class LogicTest {
logic.setMatchfield(matchfield); logic.setMatchfield(matchfield);
ArrayList<Coordinate> everySecondField = logic.getEverySecondField(); ArrayList<Coordinate> everySecondField = logic.getEverySecondField();
Coordinate calcResult = logic.chooseField(everySecondField);
Coordinate calcResult = logic.chooseField();
assertNotNull(calcResult); assertNotNull(calcResult);
} }
@ -42,7 +42,7 @@ class LogicTest {
} }
matchfield.setState(2, 2, Coordinate.EMPTY); matchfield.setState(2, 2, Coordinate.EMPTY);
Coordinate choosenField = logic.chooseField(everySecondField);
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);
} }
@ -270,7 +270,7 @@ class LogicTest {
logic.setTarget(center); logic.setTarget(center);
logic.setFoundShip(true); logic.setFoundShip(true);
Coordinate result = logic.chooseField(logic.getEverySecondField());
Coordinate result = logic.chooseField();
assertEquals(result, expectedResult); assertEquals(result, expectedResult);
} }
@ -289,8 +289,8 @@ class LogicTest {
logic.setTarget(center); logic.setTarget(center);
logic.setFoundShip(true); 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); assertEquals(result, expectedResult);
} }
@ -310,9 +310,9 @@ class LogicTest {
logic.setTarget(center); logic.setTarget(center);
logic.setFoundShip(true); 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); assertEquals(result, expectedResult);
} }
@ -332,11 +332,12 @@ class LogicTest {
logic.setTarget(center); logic.setTarget(center);
logic.setFoundShip(true); 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); assertEquals(result, expectedResult);
} }
} }
Loading…
Cancel
Save