Java – MyBoggle

This is the game of Boggle, where you find words on the playing board.
This app uses a recursion search to find if the letters are on the board.

 

MyBoggle

MyBoggle--UPDATED

 


/*
 * @(#)MyBoggle.java (UTF-8)
 * 
 * MyBoggle application
 * 
 * @author John Smith <Smith058.com>
 * @version 1.00  Jun 10, 2015
 */
package myboggle;

import java.util.Scanner;

/**
 *
 * @author John Smith <Smith058.com>
 */
public class MyBoggle {
    
    /**
     * DEBUG mode
     * true = on
     * false = off
     * @return 
     */
    public static boolean DEBUG() {
        final boolean DEBUG = false;
        return(DEBUG);
    }
    
    /**
     * setup a random boggle board with blank edges
     * @param array 
     */
    public static void randomArray(char[][] array){
        for (int row=0;row<array.length;row++) {
            for (int col=0;col<array[0].length;col++) {
                int num = (int)(26*Math.random()+65);
                if (row==0 || col==0 || row==array[0].length-1 || col==array[1].length-1) {
                    array[row][col] = ' ';
                } else {
                    array[row][col] = (char)num;
                }
            }
        }
    }
    
    /**
     * display the boggle board
     * @param array 
     */
    public static void displayArray(char[][] array) {
        for (int row=0;row<array.length;row++) {
            for (int col=0;col<array[0].length;col++) {
                    System.out.print(array[row][col]+"  ");
            }
            System.out.println();
        }
    }
    
    /**
     * check if word is one board
     * (part 1 of 2: find first letter)
     * @param array
     * @param word
     * @return 
     */
    public static int checkWord(char[][] array, String word) {
        int numOfWords = 0;
        int index = 0;
        char[][] arrayTemp = array;
        word = word.toUpperCase();
        for (int row=0;row<array.length;row++) {
            for (int col=0;col<array[0].length;col++) {
                char testIndex = array[row][col];
                if (word.charAt(index) == testIndex) {
//                if (word.indexOf(String.valueOf(testIndex)) == index) {
                    if (DEBUG()) System.out.println("HIT---- 1");
                    numOfWords += checkAround(arrayTemp, row, col, word, index);
                }   
            }
        }
        return(numOfWords);
    }
    
    /**
     * check if word is one board
     * (part 2 of 2: find the rest of the word)
     * @param arrayTemp
     * @param row
     * @param col
     * @param word
     * @param index
     * @return 
     */
    private static int checkAround(char[][] arrayTemp, int row, int col, String word,int index) {
        final char USED = '*';
        char testIndex = arrayTemp[row][col];
        if (DEBUG()) System.out.print("bug1: "+word.charAt(index));
        if (DEBUG()) System.out.print("    bug2: "+index);
        if (DEBUG()) System.out.print("    bug3: "+testIndex);
        if (DEBUG()) System.out.println("    bug4: "+word);
        if (word.charAt(index) == testIndex) {
            if (DEBUG()) System.out.println("HIT----------- 2 "+testIndex);
            if ((row < 0) || (row >= arrayTemp.length) || (col < 0) || (col >= arrayTemp[0].length)
                || (testIndex == USED)) {
                if (DEBUG()) System.out.println("miss-1  (row<0): "+(row<0)+
                        "  (row>=arrayTemp.length): "+(row>=arrayTemp.length)+
                        "  (col<0): "+(col<0)+"  (col>=arrayTemp[0].length): "+
                        (col>=arrayTemp[0].length)+"  (testIndex!=USED)"+
                        (testIndex != USED));
                return(0);
            } else {
                if (index+1 == word.length()) {
                    if (DEBUG()) System.out.println("--------------------------"+
                            "------------------- HIT-3 "+testIndex);
                    System.out.println("That word is there!");
                    return(1);
                } else {
                    if (DEBUG()) System.out.println("--------------------------"+
                            "------------------- HIT-3 "+testIndex);
                    arrayTemp[row][col] = USED;
                    return(0+
                            checkAround(arrayTemp, row+1, col, word, index+1)+
                            checkAround(arrayTemp, row-1, col, word, index+1)+
                            checkAround(arrayTemp, row, col+1, word, index+1)+
                            checkAround(arrayTemp, row, col-1, word, index+1)+
                            checkAround(arrayTemp, row+1, col-1, word, index+1)+
                            checkAround(arrayTemp, row-1, col-1, word, index+1)+
                            checkAround(arrayTemp, row+1, col-1, word, index+1)+
                            checkAround(arrayTemp, row-1, col-1, word, index+1));
                }
            }
        } else {
            if (DEBUG()) System.out.println("miss-2");
            return(0);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final String QUIT = "999";
        int numOfWords = 0;
        String word;
        Scanner input = new Scanner(System.in);
        char[][] testArray = new char[7][7];
        randomArray(testArray);
        displayArray(testArray);
//        System.out.println();
        
        /* Play game */ 
        do {
            // Get input from user
            System.out.print("Enter a word (999 to quit): ");
            word = input.nextLine();
            if (!word.equalsIgnoreCase(QUIT)){
                numOfWords += checkWord(testArray, word);
            }
        } while (!word.equalsIgnoreCase(QUIT));
        System.out.println("You found "+numOfWords+" words");
    }
}