Browse Source

add getEverySecondField in ai-logic

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

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

@ -22,4 +22,17 @@ public class Logic {
return possibleFields.get(randy.nextInt(possibleFields.size())); return possibleFields.get(randy.nextInt(possibleFields.size()));
} }
public ArrayList<Coordinate> getEverySecondField(Matchfield matchfield) {
ArrayList<Coordinate> out = new ArrayList<Coordinate>();
for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) {
for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) {
if ((x % 2 == 0 && y % 2 == 0) || (x % 2 == 1 && y % 2 == 1)) {
out.add(matchfield.getField(x, y));
}
}
}
return out;
}
} }

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

@ -1,8 +1,11 @@
package de.tims.fleetstorm.ai; package de.tims.fleetstorm.ai;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; 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 java.util.ArrayList;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Coordinate;
@ -31,13 +34,39 @@ class LogicTest {
for (int x = 0; x < size; x++) { for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) { for (int y = 0; y < size; y++) {
// matchfield.setState(x, y, Matchfield.SHOT);
matchfield.setState(x, y, Coordinate.SHOT);
} }
} }
// matchfield.setState(0, 0, Matchfield.EMPTY);
matchfield.setState(2, 2, Coordinate.EMPTY);
Coordinate choosenField = logic.chooseField(matchfield); Coordinate choosenField = logic.chooseField(matchfield);
int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY());
assertNotEquals(calcState, Coordinate.SHOT); assertNotEquals(calcState, Coordinate.SHOT);
} }
@Test
void testGetEverySecondField() {
Logic logic = new Logic();
Matchfield matchfield;
int size = 4;
matchfield = new Matchfield(size);
matchfield.createMatchfield();
ArrayList<Coordinate> everySecondField = logic.getEverySecondField(matchfield);
ArrayList<Coordinate> expectedResult = new ArrayList<Coordinate>();
expectedResult.add(new Coordinate(0, 0));
expectedResult.add(new Coordinate(0, 2));
expectedResult.add(new Coordinate(1, 1));
expectedResult.add(new Coordinate(1, 3));
expectedResult.add(new Coordinate(2, 0));
expectedResult.add(new Coordinate(2, 2));
expectedResult.add(new Coordinate(3, 1));
expectedResult.add(new Coordinate(3, 3));
for (int i = 0; i < expectedResult.size(); i++) {
assertEquals(everySecondField.get(i).getX(), expectedResult.get(i).getX());
assertEquals(everySecondField.get(i).getY(), expectedResult.get(i).getY());
}
}
} }
Loading…
Cancel
Save