package comp1110.ass1;
import org.junit.jupiter.api.*;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Timeout(value = 1000, unit = MILLISECONDS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ConflictsWithLeashTest {
private void test(WalkTheDog game, Location loc, boolean expected) {
boolean output = game.conflictsWithLeash(loc);
assertEquals(expected, output, “Expected conflictsWithLeash to return ” + expected +
” for objective state ‘” + game.getObjective().getInitialState() +
“‘, and current piece placements string ‘” + game.getPlacements() +
“‘, for location (” + loc.getX() + “, ” + loc.getY() +
“) but got ” + output + “.”);
}
@Test
public void testSimple() {
// Placement of length 2
Objective obj = new Objective(“”, 1);
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
WalkTheDog game = new WalkTheDog(obj);
Piece dog = game.getPiece(0);
dog.setDogLoc(new Location(x, y));
game.setState(dog.getDogLoc(), State.DOG);
dog.setLeash(new Location[]{dog.getDogLoc()});
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
boolean expected = x == i && y == j;
test(game, new Location(i, j), expected);
}
}
}
}
}
@Test
public void testConflictSingleLeash() {
Objective obj = new Objective("", 1);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
for (int x_diff = 1; x_diff + x < 4; x_diff++) {
WalkTheDog game = new WalkTheDog(obj);
Piece piece = game.getPiece(x_diff);
piece.setDogLoc(new Location(x,y));
piece.setOwnerLoc(new Location(x+x_diff, y));
game.setState(piece.getDogLoc(), State.DOG);
game.setState(piece.getOwnerLoc(), State.OWNER);
Location[] leash = new Location[x_diff + 1];
for (int i = 0; i <= x_diff; i++) {
leash[i] = new Location(x + i, y);
}
piece.setLeash(leash);
for (int x_test = 0; x_test < 4; x_test++) {
for (int y_test = 0; y_test < 4; y_test++) {
boolean expected = containsLoc(leash, new Location(x_test, y_test));
test(game, new Location(x_test, y_test), expected);
}
}
}
}
}
}
@Test
public void testConflictMultipleLeashes() {
Objective obj = new Objective("", 1);
for (int y_offset = 0; y_offset < 4; y_offset++) {
for (int x_offset = 0; x_offset < 3; x_offset ++) {
WalkTheDog game = new WalkTheDog(obj);
// Add leashes
for (int leashLength = 1; leashLength <= 3; leashLength++) {
int y = (y_offset + leashLength) % 4;
int x = Math.min(x_offset, 3-leashLength);
if (checkSurroundings(game, new Location(x + leashLength, y))) {
Piece piece = game.getPiece(leashLength);
piece.setDogLoc(new Location(x + leashLength, y));
piece.setOwnerLoc(new Location(x, y));
game.setState(piece.getDogLoc(), State.DOG);
game.setState(piece.getOwnerLoc(), State.OWNER);
Location[] leash = new Location[leashLength + 1];
for (int i = 0; i <= leashLength; i++) {
leash[i] = new Location(x + i, y);
}
piece.setLeash(leash);
}
}
// Test board
for (int x_test = 0; x_test < 4; x_test++) {
for (int y_test = 0; y_test < 4; y_test++) {
boolean expected = false;
for (int p = 1; p < 4; p++) {
expected = expected || containsLoc(game.getPiece(p).getLeash(), new Location(x_test, y_test));
}
test(game, new Location(x_test, y_test), expected);
}
}
}
}
}
public static boolean containsLoc(Location[] array, Location loc){
for (Location l : array) {
if (loc.equals(l))
return true;
}
return false;
}
public static boolean checkSurroundings(WalkTheDog game, Location loc){
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j ++) {
if (game.getState(new Location(i, j)) == State.DOG){
if (Math.abs(i - loc.getX()) + Math.abs(j - loc.getY()) == 1)
return false;
}
}
}
return true;
}
}