I am desperate and I need your help! I am working on a crossword puzzle solver where the puzzle and the words are given to us and we need to format the puzzle so each index in the array has it's own letter. Right now I have the puzzle index split and the words split so I can go in and compare them letter by letter but is there a better way? Is it possible to, lets say, have a string array that contains the word I'm looking for and have an array of chars locate that word?
My array looks like this:
[W, V, E, R, T, I, C, A, L, L]
[R, O, O, A, F, F, L, S, A, B]
[A, C, R, I, L, I, A, T, O, A]
[N, D, O, D, K, O, N, W, D, C]
[D, R, K, E, S, O, O, D, D, K]
[O, E, E, P, Z, E, G, L, I, W]
[M, S, I, I, H, O, A, E, R, A]
[A, L, R, K, R, R, I, R, E, R]
[K, O, D, I, D, E, D, R, C, D]
[H, E, L, W, S, L, E, U, T, H]
And lets say I'm looking for "Vertical". How can I set up a loop to look for the chars that make up the string "Vertical". Or do I have to compare it letter by letter?
This is my code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class WordSearch {
public static void main(String[] args) throws Exception{
File file = new File("puzzle.txt");
Scanner sc = new Scanner(file);
int row = sc.nextInt();
int col = sc.nextInt();
sc.nextLine();
//Make an array to hold the puzzle
char[][] puzzle = new char[row][col];
//Read in the strings from the file into the array.
for (int i=0; i<row; i++){
String getChar = (new String(sc.next()));
for(int j = 0;j<col; j++){
puzzle[i][j] = getChar.charAt(j);
}
}
//Test Print
for (int i=0; i<puzzle.length; i++){
System.out.print(Arrays.toString(puzzle[i]));
System.out.println("");
}
//Read the number of words and move to the next line
int numwords = sc.nextInt();
sc.nextLine();
//Make an array to hold the words and read in the words from a file
String[] words = new String[numwords];
for(int i=0; i<numwords; i++){
words[i] = sc.nextLine();
}
//look for each word
for(int i=0; i<numwords; i++){
String currentword = words[i];
String[] strings1 = currentword.split("");
int range = currentword.length()+1;
String[] strings2 = Arrays.copyOfRange(strings1, 1, range);
int range2 = strings2.length;
char[] charwords = new char[range2];
}
//Close the scanner
sc.close();
}
}
Would this work to test for the diagonals:
private boolean checkDiagonals(int row, int col, String word, char[][] puzzle) {
//Checking diagonals direction
for(int letter = 1; letter < word.length(); letter++) {
if(puzzle[row + letter][col + letter] != word.charAt(letter)) {
return false;
}
else if(puzzle[row + letter][col - letter] != word.charAt(letter)) {
return false;
}
else if(puzzle[row - letter][col - letter] != word.charAt(letter)) {
return false;
}
else if(puzzle[row - letter][col + letter] != word.charAt(letter)) {
return false;
}
}
return true;
}