我很绝望,我需要你的帮助!我正在研究一个填字游戏求解器,其中给出了谜题和单词,我们需要格式化谜题,以便数组中的每个索引都有自己的字母。现在我有拼图索引拆分和单词拆分所以我可以进去逐个字母比较它们但是有更好的方法吗?比方说,是否有可能有一个包含我要查找的单词的字符串数组,并有一个字符数组来定位该单词?
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]
假设我正在寻找“垂直”。如何设置一个循环来查找构成字符串“垂直”的字符。还是我必须逐字比较?
这是我的代码:
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();
}
}
这是否可以测试对角线:
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;
}