Skip to content

Boggle Word Checker in Java

The challenge

Write a function that determines whether a string is a valid guess in a Boggle board, as per the rules of Boggle. A Boggle board is a 2D array of individual characters, e.g.:

[ ['I','L','A','W'], ['B','N','G','E'], ['I','U','A','O'], ['A','S','R','L'] ]
Code language: JSON / JSON with Comments (json)

Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without re-using any previously used cells.

For example, in the above board “BINGO”, “LINGO”, and “ILNBIA” would all be valid guesses, while “BUNGIE”, “BINS”, and “SINUS” would not.

Your function should take two arguments (a 2D array and a string) and return true or false depending on whether the string is found in the array as per Boggle rules.

Test cases will provide various array and string sizes (squared arrays up to 150×150 and strings up to 150 uppercase letters). You do not have to check whether the string is a real word or not, only if it’s a valid guess.

The solution in Java code

Option 1:

public class Boggle { private final boolean result; private static boolean check(String guess, char[][] board, int i, int j, int k){ if (k>=guess.length()) return true; if ((i<)||(j<)||(i>=board.length)||(j>=board[].length))return false; if (board[i][j]==guess.charAt(k)){ board[i][j] = ; k++; boolean r = check(guess, board, i-1, j-1,k)| check(guess, board, i-1, j,k)|| check(guess, board, i-1, j+1,k)|| check(guess, board, i, j-1,k)|| check(guess, board, i, j+1,k)|| check(guess, board, i+1, j-1,k)|| check(guess, board, i+1, j,k)|| check(guess, board, i+1, j+1,k); board[i][j] = guess.charAt(--k); return r; }else return false; } static boolean validGuess(String guess, char[][] board){ for (int i=; i<board.length;i++) for (int j=; j<board[].length;j++) if (check(guess, board, i, j, )) return true; return false; } public Boggle(final char[][] board, final String word) { result = validGuess(word, board); } public boolean check() { return result; } }
Code language: Java (java)

Option 2:

See also  And then there was Google Instant!
public class Boggle { char[][] board; String word; int[][] dir = {{,1},{,-1},{1,},{-1,},{1,1},{-1,1},{-1,-1},{1,-1}}; int m, n; public Boggle(final char[][] board, final String word) { this.board = board; this.word = word; m = board.length; n = m == ? : board[].length; } public boolean check() { for (int x = ; x < m; ++x) for (int y = ; y < n; ++y) if (dfs(, x, y)) return true; return false; } private boolean dfs(int idx, int x, int y) { if (idx == word.length()) return true; if (x < || x >= m || y < || y >= n || board[x][y] == '*') return false; if (board[x][y] == word.charAt(idx)) { board[x][y] = '*'; for (int i = ; i < dir.length; ++i) { if (dfs(idx+1, x+dir[i][], y+dir[i][1])) { board[x][y] = word.charAt(idx); return true; } } board[x][y] = word.charAt(idx); } return false; } }
Code language: Java (java)

Option 3:

import java.util.*; public class Boggle { private char[][] board; private final String word; public Boggle(final char[][] board, final String word) { this.board = board; this.word = word; } public boolean check() { for (int i=; i<board.length; i++) for (int j=; j<board[i].length; j++) if (board[i][j]==word.charAt()) if (around(i, j, 1)) return true; return false; } public boolean around(int x, int y, int letter) { if (letter==word.length()) return true; char c = board[x][y]; board[x][y]='!'; for (int i=x-1; i<=x+1; i++) for (int j=y-1; j<=y+1; j++) { try { if (board[i][j]==word.charAt(letter)) if (around(i,j,letter+1)) return true; } catch (IndexOutOfBoundsException e) { continue; } } board[x][y]=c; return false; } }
Code language: Java (java)

Test cases to validate our solution

import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.util.Arrays; public class SolutionTest { final private static char[][] board = { {'E','A','R','A'}, {'N','L','E','C'}, {'I','A','I','S'}, {'B','Y','O','R'} }; private static String[] toCheck = {"C", "EAR","EARS","BAILER","RSCAREIOYBAILNEA" ,"CEREAL" ,"ROBES"}; private static boolean[] expecteds = {true, true, false, true, true, false, false }; @Test public void sampleTests() { for (int i= ; i < toCheck.length ; i++) { assertEquals( expecteds[i], new Boggle(deepCopy(board), toCheck[i]).check() ); } } private char[][] deepCopy(char[][] arr) { return .map( a -> Arrays.copyOf(a, a.length) ) .toArray(char[][]::new); } }
Code language: Java (java)

See also  How to Fix: package org.junit.jupiter.api does not exist
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x