0

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;
    }
  • Could you clarify what you mean by "adjacent" because the common understanding of the term means right next to, and thus its not clear by that term why recursion would be even needed? I suppose the better question is what are the "game rules" and how does your current code fit into that? – GetSet Mar 28 '20 at 02:14
  • Adjacent means next to in this case as well, including diagonally adjacent. However, I am trying to see if two pieces are connected as in whether or not they have adjacent pieces of the same turn() between them. I am using recursion to search all adjacent squares of 'from' to eventually see if there are pieces of the same turn() that connect 'from' to 'to'. – hohorocks Mar 28 '20 at 02:39

1 Answers1

0

I think I've solved my problem by creating an ArrayList that stores the answers to each recursive call. Then at the end instead of returning false, I return if the ArrayList contains true.

    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())) {
                    contain.add(isContiguous(curr, to, visited));
                }
            }
            if (row - 1 >= 0) {
                Square curr = sq(col, row - 1);
                if (isAdjacentColor(from, curr, get(from))) {
                    contain.add(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))) {
                    contain.add(isContiguous(curr, to, visited));
                }
            }
            if (col - 1 >= 0) {
                Square curr = sq(col - 1, row);
                if (isAdjacentColor(from, curr, get(from))) {
                    contain.add(isContiguous(curr, to, visited));
                }
            }
            if (col + 1 < BOARD_SIZE) {
                Square curr = sq(col + 1, row);
                if (isAdjacentColor(from, curr, get(from))) {
                    contain.add(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))) {
                    contain.add(isContiguous(curr, to, visited));
                }
            }
            if (row + 1 < BOARD_SIZE) {
                Square curr = sq(col, row + 1);
                if (isAdjacentColor(from, curr, get(from))) {
                    contain.add(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))) {
                    contain.add(isContiguous(curr, to, visited));
                }
            }
        }
        return contain.contains(true);
    }
  • I think the code can be simplified, For more help you'll need to post [mre] including hard coded test data – c0der Mar 28 '20 at 13:15