Browse Source

Refactoring: add Constructor to set DefaultValues

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

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

@ -11,20 +11,28 @@ public class Logic {
private Matchfield matchfield; private Matchfield matchfield;
private Coordinate lastShot; private Coordinate lastShot;
private Coordinate target; 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<Coordinate> coordinates) {
public Coordinate chooseField() {
ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>(); ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>();
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(); Random randy = new Random();
@ -68,7 +76,6 @@ public class Logic {
foundShip = true; foundShip = true;
} }
System.out.println("Do this");
} }
public void setFoundShip(boolean b) { public void setFoundShip(boolean b) {

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

@ -21,7 +21,9 @@ class LogicTest {
matchfield = new Matchfield(size); matchfield = new Matchfield(size);
matchfield.createMatchfield(); matchfield.createMatchfield();
logic.setMatchfield(matchfield); logic.setMatchfield(matchfield);
Coordinate calcResult = logic.chooseField();
ArrayList<Coordinate> everySecondField = logic.getEverySecondField();
Coordinate calcResult = logic.chooseField(everySecondField);
assertNotNull(calcResult); assertNotNull(calcResult);
} }
@ -34,14 +36,13 @@ class LogicTest {
matchfield.createMatchfield(); matchfield.createMatchfield();
logic.setMatchfield(matchfield); logic.setMatchfield(matchfield);
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
matchfield.setState(x, y, Coordinate.SHOT);
}
ArrayList<Coordinate> everySecondField = logic.getEverySecondField();
for (int i = 0; i < everySecondField.size(); i++) {
everySecondField.get(i).setState(Coordinate.SHOT);
} }
matchfield.setState(2, 2, Coordinate.EMPTY); matchfield.setState(2, 2, Coordinate.EMPTY);
Coordinate choosenField = logic.chooseField();
Coordinate choosenField = logic.chooseField(everySecondField);
int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY());
assertNotEquals(calcState, Coordinate.SHOT); assertNotEquals(calcState, Coordinate.SHOT);
} }
@ -191,4 +192,15 @@ class LogicTest {
assertEquals(logic.getClearedLeft(), true); 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);
}
} }
Loading…
Cancel
Save