package comp1110.ass1;
import org.junit.jupiter.api.*;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static comp1110.ass1.ConflictsWithLeashTest.checkSurroundings;
import static comp1110.ass1.ConflictsWithLeashTest.containsLoc;
import static comp1110.ass1.IsDogLocationValidTest.objectives;
import static comp1110.ass1.IsDogLocationValidTest.leashes;
@Timeout(value = 1000, unit = MILLISECONDS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class IsOwnerLocationValidTest {
private void test(WalkTheDog game, String placement, boolean expected) {
boolean output = game.isOwnerLocationValid(placement);
assertEquals(expected, output, “Expected isValidOwnerLoc to be ” + expected +
” for objective state ‘” + game.getObjective().getInitialState() +
“‘, and current piece placements string ‘” + game.getPlacements() +
“‘, and owner 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', '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) && 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 == 'O';
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);
}
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 Location[][] valid_locs = {
new Location[]{
new Location(0, 0),
new Location(3, 0),
new Location(0, 2),
new Location(1, 3)},
new Location[]{
new Location(0, 3),
new Location(3, 0),
new Location(3, 1),
new Location(2, 0),
new Location(2, 2),
new Location(0, 2)},
new Location[]{
new Location(1,0),
new Location(2,1),
new Location(3,1),
new Location(3,2),
new Location(2,3),
new Location(3, 3)},
new Location[]{
new Location(0,3),
new Location(0,2),
new Location(1,3),
new Location(1,1),
new Location(3,2)}
};
}