Browse Source

chooseField returns correekt Field case:Left has Ship

fleetstorm
Max Wenzel 3 years ago
committed by Lorenz Hohmann
parent
commit
4a02c0b7cc
  1. 10
      src/main/java/de/tims/fleetstorm/ai/Logic.java
  2. 23
      src/test/java/de/tims/fleetstorm/ai/LogicTest.java

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

@ -56,6 +56,16 @@ public class Logic {
} }
return target; return target;
} }
target = lastShot;
if (!clearedLeft) {
target = matchfield.getLeft(target);
if (target.getState() == Coordinate.EMPTY) {
clearedLeft = true;
}
return target;
}
} }
// If ship is sinked // If ship is sinked

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

@ -316,4 +316,27 @@ class LogicTest {
assertEquals(result, expectedResult); assertEquals(result, expectedResult);
} }
@Test
void testChooseFieldLeftAfterHit() {
Logic logic = new Logic();
int size = 4;
Coordinate center = new Coordinate(2, 2);
Coordinate expectedResult = new Coordinate(1, 2);
Matchfield matchfield = new Matchfield(size);
matchfield.createMatchfield();
matchfield.setState(center, Coordinate.HIT);
matchfield.setState(expectedResult, Coordinate.HIT);
logic.setMatchfield(matchfield);
logic.setLastShot(center);
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());
assertEquals(result, expectedResult);
}
} }
Loading…
Cancel
Save