简体   繁体   中英

Finding a word in a 2D array recursively

I'm not sure if boggle is a game that is familiar with most people so here is a brief description:

Boggle is a word game where the objective is to find English words in a grid of random characters. Characters can be said to be part of the same word if they are left, down, up, right, diagonal from each other.

My task is to find an inputted word given a 2D array of random characters recursively. I feel like I'm almost at a solution, however I keep getting an arrayindexoutofbounds exception and I'm not sure why.

The ArrayIndexOutOfBoundsException is currently happening at the 4th else if statement according to the console

Here is where I'm at in my solution at the moment:

public static boolean letterIndex(String target, char[][] board, dictionary dictionary) {
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {
            if (board[i][j] == target.charAt(0)) {
                char[] temp = target.toCharArray();
                return findword(target, i, j, board, 1, dictionary, temp);
            }
        }
    }
    return false;
}

public static boolean findword(String target, int i, int j, char[][] board, int k,
                               dictionary dictionary, char[] temp) {
    if (k >= temp.length) {
        return true;
    } else if (i < board.length - 1 & board[i + 1][j] == target.charAt(k)) {
        //temp[k] = board[i + 1][j];
        return findword(target, i + 1, j, board, k + 1, dictionary, temp);
    } else if (j < board[0].length - 1 & board[i][j + 1] == target.charAt(k)) {
        //temp[k] = board[i][j + 1];
        return findword(target, i, j + 1, board, k + 1, dictionary, temp);
    } else if (i > 0 & board[i - 1][j] == target.charAt(k)) {
        //temp[k] = board[i - 1][j];
        return findword(target, i - 1, j, board, k + 1, dictionary, temp);
    } else if (j > 0 & board[i][j - 1] == target.charAt(k)) {
        //temp[k] = board[i][j - 1];
        return findword(target, i, j - 1, board, k + 1, dictionary, temp);
    } else if (i > 0 & j < board[0].length - 1 & board[i - 1][j + 1] == target.charAt(k)) {
        //temp[k] = board[i - 1][j + 1];
        return findword(target, i - 1, j + 1, board, k + 1, dictionary, temp);
    } else if (i < board.length - 1 & j > 0 & board[i + 1][j - 1] == target.charAt(k)) {
        //temp[k] = board[i + 1][j - 1];
        return findword(target, i + 1, j - 1, board, k + 1, dictionary, temp);
    } else if (i > 0 & j > 0 & board[i - 1][j - 1] == target.charAt(k)) {
        //temp[k] = board[i - 1][j - 1];
        return findword(target, i - 1, j - 1, board, k + 1, dictionary, temp);
    } else if (i < board.length - 1 & j < board[0].length - 1 & board[i + 1][j + 1] == target.charAt(k)) {
        //temp[k] = board[i + 1][j + 1];
        return findword(target, i + 1, j + 1, board, k + 1, dictionary, temp);
    }
    return false;
}

the letterIndex method searches for the intial letter of the word and finds it's location on the board. It then sets the first element of a temporary array to be this letter and calls the findword function which takes the location of the letter in the array and checks each direction for the next letter. It does this until the array matches the target string or all of the directions return false. I don't think there's anything wrong with what I did but I'm getting errors so apparently I have made an error. Any insight would be much appreciated!

I actually fixed the problem I needed i or j -1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM