I wrote the code below as well as the isAdjacentColor method. The isAdjacentColor method returns true if two squares are adjacent (e.g. one square's queen move away from each other) and have the same color or turn().
I think I have implemented a breadth-first search.
I was wondering how to use the recursive call, as currently returning the recursive call will return false before all adjacent options have been explored.
boolean isContiguous(Square from, Square to, boolean[][] visited) {
if (get(from) != get(to)) {
return false;
}
if (from == to) {
return true;
}
int col = from.col();
int row = from.row();
if (!visited[col][row]) {
visited[col][row] = true;
if (col - 1 >= 0 && row - 1 >= 0) {
Square curr = sq(col - 1, row - 1);
if (isAdjacentColor(from, curr, turn())) {
return isContiguous(curr, to, visited);
}
}
if (row - 1 >= 0) {
Square curr = sq(col, row - 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col + 1 < BOARD_SIZE && row - 1 >= 0) {
Square curr = sq(col + 1, row - 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col - 1 >= 0) {
Square curr = sq(col - 1, row);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col + 1 < BOARD_SIZE) {
Square curr = sq(col + 1, row);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col - 1 >= 0 && row + 1 < BOARD_SIZE) {
Square curr = sq(col - 1, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (row + 1 < BOARD_SIZE) {
Square curr = sq(col, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
if (col + 1 < BOARD_SIZE && row + 1 < BOARD_SIZE) {
Square curr = sq(col + 1, row + 1);
if (isAdjacentColor(from, curr, get(from))) {
return isContiguous(curr, to, visited);
}
}
}
return false;
}