Boggle Board (Recursion Example)

Here is an app I made in class. ??It is a boggle board game using recursion.??

Check the attachments area to download the source code

??

Here is some of the source code:

// Finds if the first letter of the word is in the Boggle Board.

// If it is found, then it will 'findRestOfWord' in hopes that the whole word will be in the boggle board.

class WordValidity {

private char[][] theBoggleArray;

private char[] theWordArray;

public boolean entireWordFound = false;

public char A_MASK = (char)126;

public WordValidity(char[][] boggleArray, char[] wordArray) {

theBoggleArray = boggleArray;

theWordArray = wordArray;

}

public void findWordStart () {

int startRow = -1;

int startCol = -1;



getOut: {

for (int i=0; i<theBoggleArray.length; i++) {

for (int j=0; j<theBoggleArray[0].length; j++) {

if (theBoggleArray[i][j] == theWordArray[0]) {

startRow = i;

startCol = j;

findRestOfWord(startRow, startCol, 0);

if ((entireWordFound == true) || ((i == 4) && (j == 4))) {

break getOut;

}

}

}

}

?? ?? }

}

public void findRestOfWord(int row, int col, int letterIndex) {

getOut2: {

// If the whole word has been found, set 'entireWordFound' to true and exit out of the method and return to the main method

if (theBoggleArray[row][col] == theWordArray[letterIndex] && letterIndex == theWordArray.length -1 ) {

entireWordFound = true;

break getOut2;

} else if ((theBoggleArray[row][col] == theWordArray[letterIndex]) && (letterIndex < theWordArray.length)) {

theBoggleArray[row][col] = A_MASK;

// Checks letters around the previous letter that was found in the word

if ((row+1) < theBoggleArray.length) findRestOfWord(row+1, col, letterIndex + 1);

if ((row-1) > -1) findRestOfWord(row-1, col, letterIndex + 1);

if ((col+1) < theBoggleArray[0].length) findRestOfWord(row, col+1, letterIndex + 1);

if ((col-1) > -1) findRestOfWord(row, col-1, letterIndex + 1);

if (((row+1) < theBoggleArray.length) && ((col+1) < theBoggleArray[0].length)) findRestOfWord(row+1, col+1, letterIndex + 1);

if (((col-1) > -1) && ((row+1) < theBoggleArray.length)) findRestOfWord(row+1, col-1, letterIndex + 1);

if (((row-1) > -1) && ((col+1) < theBoggleArray[0].length)) findRestOfWord(row-1, col+1, letterIndex + 1);

if (((row-1) > -1) && ((col-1) > -1)) findRestOfWord(row-1, col-1, letterIndex + 1);

}

}

}

}
More in this category: « Calculator Challenge

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

© 2015-2016 Evan Sevy. All Rights Reserved. Designed By Evan Sevy