Browse Source

choosenField returns only Fields with state != shot

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

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

@ -1,9 +1,24 @@
package de.tims.fleetstorm.ai; package de.tims.fleetstorm.ai;
import java.util.ArrayList;
import java.util.Random;
import de.tims.fleetstorm.matchfield.Matchfield;
public class Logic { public class Logic {
public int[] chooseField() {
return new int[] {};
public int[] chooseField(Matchfield matchfield) {
ArrayList<int[]> possibleFields = new ArrayList<int[]>();
for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) {
for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) {
if (matchfield.getState(x, y) == Matchfield.EMPTY || matchfield.getState(x, y) == Matchfield.SHIP) {
possibleFields.add(new int[] { x, y });
}
}
}
Random randy = new Random();
return possibleFields.get(randy.nextInt(possibleFields.size() - 1));
} }
} }

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

@ -1,16 +1,35 @@
package de.tims.fleetstorm.ai; package de.tims.fleetstorm.ai;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import de.tims.fleetstorm.matchfield.Matchfield;
class LogicTest { class LogicTest {
Logic logic = new Logic(); Logic logic = new Logic();
int size = 3;
private Matchfield matchfield = new Matchfield(size);
@Test @Test
void testFielIsNotNull() {
int[] calcResult = logic.chooseField();
void testFieldIsNotNull() {
int[] calcResult = logic.chooseField(matchfield);
assertNotNull(calcResult); assertNotNull(calcResult);
} }
@Test
void testChoosenFieldHasNotStateShot() {
matchfield.createMatchfield();
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
// matchfield.setState(x, y, Matchfield.SHOT);
}
}
// matchfield.setState(0, 0, Matchfield.EMPTY);
int[] choosenField = logic.chooseField(matchfield);
int calcState = matchfield.getState(choosenField[0], choosenField[1]);
assertNotEquals(calcState, Matchfield.SHOT);
}
} }
Loading…
Cancel
Save