Recursively find a word in a grid
<p>I am working on an algorithm question in order to find a word on the grid.</p>

<p>My solution consists of first finding the first letter of the word in the grid, then if found, recursively go through the 8 directions until every index of the word is matched with the grid indices, and return the string. Attached is my code:</p>

<p>I keep track of my current x and y positions, and then increment the position of the string if and only if there is a match in the index of the word with the index within the grid. However, with this snippet of code, there is something wrong with my recursion causing a stack overflow: </p>

<pre><code>public static void findWord(int row, int col, char[][] grid, String w) {
    int rowLength = row;
    int colLength = col;

    char[] word = w.toCharArray();

    for(int j = 0; j &lt; colLength; j++) {
        for(int i = 0; i &lt; rowLength; i++) {
            // Check if first index of word is in this location
            if(word[0] == grid[j][i]) {
                // Iterate through each 8 directions to find the next word
                for(int dir = 0; dir &lt; 8; dir++) {
                    recursiveFind(i, j, i, j, dir, 0, word, grid, rowLength, colLength);
                }
            }
        }
    }
}

public static boolean recursiveFind(
        int initialX, 
        int initialY, 
        int currentX, 
        int currentY, 
        int dir, 
        int currentPos, 
        char[] word, 
        char[][] grid, 
        int rowLength, 
        int colLength) 
{
    // base case is if currentPos == length of word
    if(word.length == currentPos) {
        System.out.println("Initial: " + initialX + " " + initialY);
        System.out.println("Final: " + currentX + " " + currentY);
    }

    if(dir == 0) { // 1
        currentX = currentX; // 0
        currentY -= 1; // -1
    } else if(dir == 1) {
        currentX += 1; // 1
        currentY -= 1; // -1
    } else if(dir == 2) {
        currentX += 1; // 1
        currentY = 0; // 0
    } else if(dir == 3) {
        currentX += 1; // 1
        currentY += 1; // 1
    } else if(dir == 4) {
        currentX = currentX; // 0
        currentY += 1;  // 1
    } else if(dir == 5) {
        currentX -= 1; // -1
        currentY += 1; // 1
    } else if(dir == 6) {
        currentX -= 1; // -1
        currentY = currentY; // 0
    } else {
        currentX -= 1; // -1
        currentY -= 1; // -1
    }

    if(currentX &lt; 0 || 
            currentX == rowLength || 
            currentY &lt; 0 || 
            currentY == colLength || 
            grid[currentY][currentX] != word[currentPos]){
        return false;
    }
    return recursiveFind(initialX, initialY, currentX, currentY, dir, currentPos + 1, word, grid, rowLength, colLength);
}
</code></pre>

<p>Main.java</p>

<pre><code>    char[][] myGrid = new char[][]{
            {'H', 'Q', 'W', 'C', 'S'}, 
            {'E', 'S', 'P', 'K', 'D'}, 
            {'D', 'X', 'A', 'F', 'L'}, 
            {'O', 'C', 'H', 'K', 'H'}, 
            {'C', 'T', 'Y', 'C', 'A'}, 
    };

    String myWord = "CODE";

    findWord(5, 5, myGrid, myWord);
</code></pre>

<p>I am currently trying to put some debug statements to see what the issue is, however, if someone is willing to give me some assistance on this, it will be highly appreciated!</p>

<p>EDIT:</p>

<p>I fixed the Stack Overflow issue by <code>return true</code> in the base case. However, my results are the following, which do not return the expected values I wanted.</p>

<pre><code>Initial X: 3, Initial Y: 0, Dir: 0, Current X: 3, Current Y: 0
Initial X: 3, Initial Y: 0, Dir: 1, Current X: 3, Current Y: 0
Initial X: 3, Initial Y: 0, Dir: 2, Current X: 3, Current Y: 0
Initial X: 3, Initial Y: 0, Dir: 3, Current X: 3, Current Y: 0
Initial X: 3, Initial Y: 0, Dir: 4, Current X: 3, Current Y: 0
Initial X: 3, Initial Y: 0, Dir: 5, Current X: 3, Current Y: 0
Initial X: 3, Initial Y: 0, Dir: 6, Current X: 3, Current Y: 0
</code></pre>