package comp1110.ass1;
import org.junit.jupiter.api.*;
import static comp1110.ass1.ConflictsWithLeashTest.checkSurroundings;
import static comp1110.ass1.ConflictsWithLeashTest.containsLoc;
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 IsDogLocationValidTest {
private void test(WalkTheDog game, String placement, boolean expected) {
boolean output = game.isDogLocationValid(placement);
assertEquals(expected, output, “Expected isValidDogLoc to be ” + expected +
” for objective state ‘” + game.getObjective().getInitialState() +
“‘, and current piece placements string ‘” + game.getPlacements() +
“‘, and dog placement string ‘” + placement +
“‘ but got ” + output + “.”);
}
@Test
public void testSimple() {
// Invalid placement string and empty board
Objective obj = new Objective(“”, 1);
WalkTheDog game = new WalkTheDog(obj);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
test(game, i + "" + j, true);
}
for (int i = 0; i < 4; i++) {
test(game, i + "", false);
test(game, i + "" + i + i, false);
test(game, i + "" + 'a', false);
}
}
@Test
public void testConflictWithPiece() {
// Test tree
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
Objective obj = new Objective("T" + x + "" + y, 1);
WalkTheDog game = new WalkTheDog(obj);
for (int testx = 0; testx < 4; testx++) {
for (int testy = 0; testy < 4; testy++) {
boolean expected = testx != x || testy != y;
test(game, testx + "" + testy, expected);
}
}
}
}
// Test cats and required dog and owner positions
for (char c : new char[]{'C', 'D'}) {
for (int x1 = 0; x1 < 4; x1++) {
for (int y1 = 0; y1 < 4; y1++) {
for (int x2 = 0; x2 < 4; x2++) {
for (int y2 = 0; y2 < 4 && !(x1 == x2 && y1 == y2) && Math.abs(x1 - x2) + Math.abs(y1 - y2) != 1; y2++) {
Objective obj = new Objective(c + "" + x1 + "" + y1 + "" + x2 + "" + y2, 1);
WalkTheDog game = new WalkTheDog(obj);
for (int testx = 0; testx < 4; testx++) {
for (int testy = 0; testy < 4; testy++) {
boolean expected = !(testx == x1 && testy == y1) && !(testx == x2 && testy == y2) || c == 'D';
expected = expected && Math.abs(x1 - testx) + Math.abs(y1 - testy) != 1;
expected = expected && Math.abs(x2 - testx) + Math.abs(y2 - testy) != 1;
test(game, testx + "" + testy, expected);
}
}
}
}
}
}
}
char c = 'O';
for (int x1 = 0; x1 < 4; x1++) {
for (int y1 = 0; y1 < 4; y1++) {
for (int x2 = 0; x2 < 4; x2++) {
for (int y2 = 0; y2 < 4 && !(x1 == x2 && y1 == y2); y2++) {
Objective obj = new Objective(c + "" + x1 + "" + y1 + "" + x2 + "" + y2, 1);
WalkTheDog game = new WalkTheDog(obj);
for (int testx = 0; testx < 4; testx++) {
for (int testy = 0; testy < 4; testy++) {
boolean expected = !(testx == x1 && testy == y1) && !(testx == x2 && testy == y2);
test(game, testx + "" + testy, expected);
}
}
}
}
}
}
}
@Test
public void testConflictsWithLeash() {
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++) {
Location loc = new Location(x_test, y_test);
boolean expected = true;
for (int p = 1; p < 4; p++) {
expected = expected && !containsLoc(game.getPiece(p).getLeash(), loc);
}
expected = expected && checkSurroundings(game, loc);
test(game, x_test + "" + y_test, expected);
}
}
}
}
}
@Test
public void testAll() {
for (int i = 0; i < objectives.length; i++) {
Objective obj = objectives[i];
WalkTheDog game = new WalkTheDog(obj);
for (int j = 1; j <= 3; j++) {
Piece piece = game.getPiece(j);
piece.setLeash(leashes[3 * i + j - 1]);
piece.setDogLoc(piece.getLeash()[0]);
piece.setOwnerLoc(piece.getLeash()[j]);
game.setState(piece.getDogLoc(), State.DOG);
game.setState(piece.getOwnerLoc(), State.OWNER);
}
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
Location loc = new Location(x, y);
boolean expected = containsLoc(valid_locs[i], loc);
test(game, x + "" + y, expected);
}
}
}
}
public static Objective[] objectives = {
new Objective("C0131D20120333O103223", 1),
new Objective("T33D01122132O00", 21),
new Objective("T02D0022O1120", 41),
new Objective("T00C1233O30", 61),
};
public static Location[][] leashes = {
new Location[]{new Location(3, 3), new Location(3, 2)},
new Location[]{new Location(1, 2), new Location(1, 1), new Location(1, 0)},
new Location[]{new Location(2, 0), new Location(2, 1), new Location(2, 2), new Location(2, 3)},
new Location[]{new Location(0, 1), new Location(0, 0)},
new Location[]{new Location(1, 2), new Location(1, 1), new Location(1, 0)},
new Location[]{new Location(3, 2), new Location(3,3), new Location(2,3), new Location(1, 3)},
new Location[]{new Location(3, 0), new Location(2, 0)},
new Location[]{new Location(1, 3), new Location(1, 2), new Location(1, 1)},
new Location[]{new Location(0,0), new Location(0,1), new Location(0,2), new Location(0,3)},
new Location[]{new Location(3, 1), new Location(3, 0)},
new Location[]{new Location(0, 1), new Location(0, 0), new Location(1, 0)},
new Location[]{new Location(2,0), new Location(2,1), new Location(2,2), new Location(2,3)}
};
public static Location[][] valid_locs = {
new Location[]{new Location(0, 3)},
new Location[]{new Location(0, 3), new Location(3, 0), new Location(2, 1)},
new Location[]{new Location(2,2), new Location(3, 3)},
new Location[]{new Location(0,3)}
};
}