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;
@Timeout(value = 1000, unit = MILLISECONDS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class IsLeashValidTest {
private void test(WalkTheDog game, String placement, int leashLength, boolean expected) {
boolean output = game.isLeashValid(placement, leashLength);
assertEquals(expected, output, “Expected isLeashValid to return ” + expected +
” for objective state ‘” + game.getObjective().getInitialState() +
“‘, and current piece placements string ‘” + game.getPlacements() +
“‘, placement string ‘” + placement +
“‘, and leash length ” + leashLength +
” but got ” + output + “.”);
}
@Test
public void testSimple() {
Objective obj = new Objective(“”, 1);
WalkTheDog game = new WalkTheDog(obj);
test(game, “”, 0, false);
test(game, “1”, 0, false);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
test(game, i + "" + j, 0, true);
}
}
}
@Test
public void testHorizontalLeash() {
Objective empty_obj = new Objective("", 1);
WalkTheDog empty_game = new WalkTheDog(empty_obj);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
for (int x_diff = 1; x_diff + x <= 3; x_diff++) {
String placement1 = x + "" + y + "" + (x + x_diff) + "" + y;
String placement2 = (x + x_diff) + "" + y + "" + x + "" + y;
for (int tree_x = 0; tree_x < 3; tree_x++) {
for (int tree_y = 0; tree_y < 3; tree_y++) {
if (!(tree_y == y && (tree_x == x || tree_x == x + x_diff))) {
Objective tree_obj = new Objective("T" + tree_x + "" + tree_y, 1);
WalkTheDog tree_game = new WalkTheDog(tree_obj);
for (int i = 0; i < 4; i++) {
boolean expected = i == x_diff;
test(empty_game, placement1, i, expected);
test(empty_game, placement2, i, expected);
test(tree_game, placement1, i, expected);
test(tree_game, placement2, i, expected);
}
}
}
}
}
}
}
}
@Test
public void testVerticalLeash() {
Objective empty_obj = new Objective("", 1);
WalkTheDog empty_game = new WalkTheDog(empty_obj);
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 4; x++) {
for (int y_diff = 1; y_diff + y <= 3; y_diff++) {
String placement1 = x+""+y+""+x + "" + (y+y_diff);
String placement2 = x+""+(y+y_diff)+""+x+""+y;
for (int tree_x = 0; tree_x < 3; tree_x++) {
for (int tree_y = 0; tree_y < 3; tree_y++){
if (!(tree_x == x && (tree_y == y || tree_y == y + y_diff))) {
Objective tree_obj = new Objective("T"+tree_x + ""+tree_y, 1);
WalkTheDog tree_game = new WalkTheDog(tree_obj);
for (int i = 0; i < 4; i ++) {
boolean expected = i == y_diff;
test(empty_game, placement1, i, expected);
test(empty_game, placement2, i, expected);
test(tree_game, placement1, i, expected);
test(tree_game, placement2, i, expected);
}
}
}
}
}
}
}
}
@Test
public void testAroundTree() {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
for (int x_diff = 1; x_diff + x <= 3; x_diff++) {
for (int y_diff = 1; x_diff + y_diff <= 3 && y + y_diff <=3; y_diff++) {
String placement1 = x + "" + y + "" + (x + x_diff) + "" + (y+y_diff);
String placement2 = (x + x_diff) + "" + (y+y_diff) + "" + x + "" + y;
String placement3 = (x + x_diff) + "" + y + "" + x + "" + (y+y_diff);
String placement4 = x + "" + (y+y_diff) + "" + (x + x_diff) + "" + y;
// Invalid, no tree
Objective no_tree = new Objective("",1);
WalkTheDog no_tree_game = new WalkTheDog(no_tree);
test(no_tree_game, placement1, x_diff+y_diff, false);
test(no_tree_game, placement2, x_diff+y_diff, false);
// Tree at all 4 corners
Objective obj1 = new Objective("T"+x +""+(y+y_diff),1);
WalkTheDog game1 = new WalkTheDog(obj1);
Objective obj2 = new Objective("T"+(x+x_diff) +""+y,1);
WalkTheDog game2 = new WalkTheDog(obj2);
Objective obj3 = new Objective("T"+x +""+y,1);
WalkTheDog game3 = new WalkTheDog(obj3);
Objective obj4 = new Objective("T"+(x+x_diff) +""+(y+y_diff),1);
WalkTheDog game4 = new WalkTheDog(obj4);
for (int i = 0; i < 4; i++) {
boolean expected = i == x_diff + y_diff;
for (String placement : new String[]{placement1, placement2}) {
test(game1, placement, i, expected);
test(game2, placement, i, expected);
test(game3, placement, i, false);
test(game4, placement, i, false);
}
for (String placement : new String[]{placement3, placement4}) {
test(game1, placement, i, false);
test(game2, placement, i, false);
test(game3, placement, i, expected);
test(game4, placement, i, expected);
}
}
}
}
}
}
}
@Test
public void conflictsWithPiece() {
// Does the leash conflict with a cat
for (int i = 0; i < leashStrings.length; i++) {
Location[] leash = leashes[i];
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (!treeLocs[i].equals("T"+x + "" + y)) {
Objective obj = new Objective(treeLocs[i] + "C" + x + "" + y, 1);
WalkTheDog game = new WalkTheDog(obj);
test(game, leashStrings[i], leash.length + 1, !containsLoc(leash, new Location(x, y)));
}
}
}
}
// Does the leash conflict with a placed dog or owner
for (int i = 0; i < leashStrings.length; i++) {
Location[] leash = leashes[i];
Objective obj = new Objective(treeLocs[i], 1);
WalkTheDog game = new WalkTheDog(obj);
Piece green = game.getPiece(0);
Piece blue = game.getPiece(1);
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (!treeLocs[i].equals("T"+x+""+y)) {
green.placePiece(game, x + "" + y);
test(game, leashStrings[i], leash.length + 1, !containsLoc(leash, new Location(x, y)));
green.removePiece(game);
if (x != 3 && !treeLocs[i].equals("T"+(x+1)+""+y)) {
blue.placePiece(game, x + "" + y + "" + (x + 1) + "" + y);
test(game, leashStrings[i], leash.length + 1, !containsLoc(leash, new Location(x, y)) && !containsLoc(leash, new Location(x + 1, y)));
blue.removePiece(game);
}
}
}
}
}
}
@Test
public void conflictsWithRequiredPiece() {
for (int i = 0; i < leashStrings.length; i++) {
Location[] leash = leashes[i];
for (char c : new char[]{'O', 'D'}) {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (!treeLocs[i].equals("T"+x+""+y)) {
Objective obj = new Objective(treeLocs[i]+c+""+x+""+y, 1);
WalkTheDog game = new WalkTheDog(obj);
test(game, leashStrings[i], leash.length + 1, !containsLoc(leash, new Location(x, y)));
}
}
}
}
}
}
@Test
public void conflictsWithLeash() {
for (int i = 0; i < leashStrings.length; i++) {
Location[] leash = leashes[i];
Objective obj = new Objective(treeLocs[i], 1);
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
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);
for (int leashLength = 1; leashLength <= 3; leashLength++) {
if (leashLength != leash.length + 1) {
int owner_y = (y_offset + leashLength) % 4;
int owner_x = Math.min(x_offset, 3 - leashLength);
// is this a valid placement
if (checkSurroundings(game, new Location(owner_x + leashLength, owner_y))
&& !treeLocs[i].equals("T" + owner_x + "" + owner_y)
&& !treeLocs[i].equals("T" + (owner_x + leashLength) + "" + owner_y)) {
Piece piece = game.getPiece(leashLength);
piece.setDogLoc(new Location(owner_x + leashLength, owner_y));
piece.setOwnerLoc(new Location(owner_x, owner_y));
game.setState(piece.getDogLoc(), State.DOG);
game.setState(piece.getOwnerLoc(), State.OWNER);
Location[] leashToAdd = new Location[leashLength + 1];
for (int j = 0; j <= leashLength; j++) {
leashToAdd[j] = new Location(owner_x + j, owner_y);
}
piece.setLeash(leashToAdd);
}
}
}
boolean expected = true;
for (Location loc : leash) {
for (int p = 1; p < 4; p++) {
expected = expected && !containsLoc(game.getPiece(p).getLeash(), loc);
}
}
test(game, leashStrings[i], leash.length + 1, expected);
for (int leashLength = 1; leashLength <= 3; leashLength++) {
Piece piece = game.getPiece(leashLength);
piece.removePiece(game);
}
}
}
}
}
}
}
public static String[] leashStrings = {
"1011",
"0001",
"2223",
"1202",
"0002",
"3313",
"0011",
"0011",
"0003",
"3202",
"0012",
"1123"
};
// Intermediate leash positions
public static Location[][] leashes = {
new Location[]{},
new Location[]{},
new Location[]{},
new Location[]{},
new Location[]{new Location(0, 1)},
new Location[]{new Location(2, 3)},
new Location[]{new Location(0, 1)},
new Location[]{new Location(1, 0)},
new Location[]{new Location(0, 1), new Location(0, 2)},
new Location[]{new Location(2, 2), new Location(1, 2)},
new Location[]{new Location(1, 0), new Location(1, 1)},
new Location[]{new Location(1, 2), new Location(1, 3)},
};
public static String[] treeLocs = {
"T22",
"",
"",
"",
"",
"T00",
"T01",
"T10",
"",
"",
"T10",
"T13",
};
}