import java.util.Scanner; public class P_WordSearch { public static void main(String[] args) { char[][] puzzle = { {'B','O','O','G','S','R','O','W','I','N','G'}, {'E','B','L','G','N','I','M','M','I','W','S'}, {'L','C','E','A','T','I','P','U','P','I','S'}, {'C','M','I','N','C','A','X','Y','O','S','N'}, {'Y','H','U','B','C','I','R','O','T','P','O'}, {'C','T','H','T','O','H','S','R','B','O','H'}, {'S','Y','E','E','N','R','E','Y','E','R','T'}, {'S','H','A','C','I','T','E','L','H','T','A'}, {'A','R','L','R','C','G','Y','A','L','P','R'}, {'L','S','T','H','G','I','E','W','E','V','A'}, {'C','E','H','L','S','E','L','C','S','U','M'} }; Scanner s = new Scanner(System.in); // System.out.println("Enter a word to search for:"); // String word = s.nextLine(); String word = "ODE"; System.out.println(containsWord(word, puzzle)); } public static boolean containsWord(String word, char[][] puzzle) { //Idea is to build all combinations of words the length of the String to see if any of them match it. //check horizontally to the right (left to right) String check = ""; //This is the String we build and match to word for(int r =0; r < puzzle.length; r++) //we want to check all rows { for(int c = 0; c <= puzzle[0].length-word.length(); c++) //start at the first column, but stop at the column that will cause our check to go out of bounds and create an error { int len = 0; //use to control the length of the String we build to match the length of the word sent in check = ""; //reset the variable before building a new word while(len < word.length()) //go as many characters as the length of the word we are comparing it to { check += puzzle[r][c+len]; //add the next character len++; //increase len by 1 so we grab the next character } if(check.equals(word)) return true; //if the word we build matches it, return true (it was found) } } //Check Row-backwards //Down //up //Diagonal # 1- Down & Right //Diagonal # 2 - Down & Left //Diagonal # 3 - UP & Right //Diagonal # 4 - Up & Left return false; } }