Just for anyone's interest/input, I adapted the WordSearch task to be able to take any WordSearch Grid from a file and any list of words.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WordSearchSolver {
public static char[][] wordSearch;
public static List<String> wordsToSearch;
public static String firstFilename;
public static String secondFilename;
public static int gridSizeX;
public static int gridsizeY;
public static void main(String[] args) {
ReadFilenames();
char[][] wordSearch = populateWordSearch(firstFilename);
List<String> wordsToSearch = getWordsToSearch(secondFilename);
List<Word> foundWords = FindWords(wordSearch, wordsToSearch);
foundWords.forEach(System.out::println);
}
public static void ReadFilenames() {
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Enter the file with the search grid");
firstFilename = keyboardInput.nextLine();
System.out.println("Enter the file with the words to find");
secondFilename = keyboardInput.nextLine();
System.out.println("Enter the width of the search grid (number of columns)");
gridSizeX = Integer.parseInt(keyboardInput.nextLine());
System.out.println("Enter the height of the search grid (number of lines)");
gridsizeY = Integer.parseInt(keyboardInput.nextLine());
keyboardInput.close();
}
public static char[][] populateWordSearch(String filename) {
char[][] wordSearch = new char[gridSizeX][gridsizeY];
try {
BufferedReader readFile = new BufferedReader(new FileReader("/Users/davideaton/Desktop/Newsearch.txt"));
Scanner readLine = new Scanner(readFile);
int n = 0;
while (readLine.hasNext()) {
String wordSearchLine = readLine.nextLine();
char[] line = wordSearchLine.toCharArray();
wordSearch[n] = line;
n++;
}
readFile.close();
readLine.close();
} catch (IOException e) {
System.out.println("There was an exception reading the WordSearch Grid");
}
return wordSearch;
}
public static List<String> getWordsToSearch(String filename) {
ArrayList<String> wordsToSearch = new ArrayList<>();
try {
BufferedReader readFile = new BufferedReader(new FileReader("/Users/davideaton/Desktop/Word.txt"));
while (readFile.ready()) {
String currentWord = readFile.readLine();
currentWord = currentWord.replaceAll(" ", "");
currentWord = currentWord.trim();
wordsToSearch.add(currentWord);
}
readFile.close();
} catch (IOException e) {
System.out.println("There was an exception reading the word list");
}
return wordsToSearch;
}
public static List<Word> FindWords(char[][] wordSearch, List<String> words) {
int[] manipulateXCoord = {-1, 0, 1};
int[] manipulateYCoord = {-1, 0, 1};
List<Word> allWords = new ArrayList<>();
for (String word : words) {
TempWord newWord = new TempWord(word);
for (int xCoord = 0; xCoord < wordSearch.length; xCoord++) {
for (int yCoord = 0; yCoord < wordSearch[0].length; yCoord++) {
newWord.setTempValues(xCoord, yCoord);
for (int lookAroundX : manipulateXCoord) {
for (int lookAroundY : manipulateYCoord) {
if (lookAroundX == 0 && lookAroundY == 0) continue;
if (IsTheWordFound(wordSearch, newWord, word, lookAroundX, lookAroundY)) {
Word foundWord = new Word(word);
foundWord.setStartPoint(newWord.startX + 1, newWord.startY + 1);
foundWord.setEndPoint(newWord.endX + 1, newWord.endY + 1);
allWords.add(foundWord);
}
}
}
}
}
}
return allWords;
}
public static boolean IsTheWordFound(char[][] wordSearch, TempWord newWord, String passedWord, int manipulatedX, int manipulatedY) {
int x = newWord.tempX;
int y = newWord.tempY;
if (wordSearch[newWord.tempX][newWord.tempY] != passedWord.charAt(0)) {
return false;
}
for (int nextCharacterInWord = 1; nextCharacterInWord < passedWord.length(); nextCharacterInWord++) {
x += manipulatedX;
y += manipulatedY;
try {
if (wordSearch[x][y] != passedWord.charAt(nextCharacterInWord)) {
return false;
}
} catch (ArrayIndexOutOfBoundsException e) {
return false;
}
}
newWord.setStartPoint(newWord.tempX, newWord.tempY);
newWord.setEndPoint(x, y);
return true;
}
}