Browse Source

chooseField returns correekt Field cae:Up has Ship

Max Wenzel 3 years ago
parent
commit
dd1a7a34af
  1. 18
      src/main/java/de/tims/fleetstorm/ai/Logic.java
  2. 26
      src/test/java/de/tims/fleetstorm/ai/LogicTest.java

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

@ -28,16 +28,28 @@ public class Logic {
public Coordinate chooseField(ArrayList<Coordinate> coordinates) {
ArrayList<Coordinate> possibleFields = new ArrayList<Coordinate>();
if (foundShip) {
if (!clearedAbove) {
target = matchfield.getAbove(lastShot);
return target;
}
}
// If ship is sinked
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));
}
}
Random randy = new Random();
return possibleFields.get(randy.nextInt(possibleFields.size()));
lastShot = possibleFields.get(randy.nextInt(possibleFields.size()));
if (lastShot.getState() == Coordinate.SHIP) {
foundShip = true;
}
return lastShot;
}
public ArrayList<Coordinate> getEverySecondField() {

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

@ -1,12 +1,17 @@
package de.tims.fleetstorm.ai;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.ArrayList;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import de.tims.fleetstorm.matchfield.Coordinate;
import de.tims.fleetstorm.matchfield.Matchfield;
@ -256,4 +261,25 @@ class LogicTest {
}
@ParameterizedTest(name = "choose the right Coordinate after Hit")
@MethodSource("getCorrectNext")
void testNextChooseFieldAfterHit(String testName, Coordinate center, Coordinate expectedResult) {
Logic logic = new Logic();
int size = 4;
Matchfield matchfield = new Matchfield(size);
matchfield.createMatchfield();
matchfield.setState(center, Coordinate.HIT);
matchfield.setState(expectedResult, Coordinate.HIT);
logic.setMatchfield(matchfield);
logic.setLastShot(center);
logic.setFoundShip(true);
Coordinate result = logic.chooseField(logic.getEverySecondField());
assertThat(result).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> getCorrectNext() {
return Stream
.of(Arguments.of("center (2/2) - target should be (2,3)", new Coordinate(2, 2), new Coordinate(2, 3)));
}
}
Loading…
Cancel
Save