我正在尝试遍历二维数组。我目前在 char 数组中设置了填字游戏,我需要能够检查字母周围的字母,以查看我当前正在寻找的单词是否与第一个字母相关。我已经设置了布尔方法来完成我需要的工作,但我想知道是否有办法检查我当前的计数器是否超出了我的数组范围。我的数组如下所示:
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
我目前的代码是这样的:
public static boolean check(int row, int col, String word, char[][] puzzle) {
if(checkRight(row, col, word, puzzle)) return true;
if(checkLeft(row, col, word, puzzle)) return true;
if(checkUp(row, col, word, puzzle)) return true;
if(checkDown(row, col, word, puzzle)) return true;
if(checkNW(row, col, word, puzzle)) return true;
if(checkNE(row, col, word, puzzle)) return true;
if(checkSW(row, col, word, puzzle)) return true;
if(checkSE(row, col, word, puzzle)) return true;
return false;
}
private static boolean checkRight(int row, int col, String word, char[][] puzzle) {
//Checking right direction
for(int letter = 1; letter < word.length(); letter++) {
if(puzzle[row][col + letter] != word.charAt(letter)) {
return false;
}
}
return true;
}
private static boolean checkLeft(int row, int col, String word, char[][] puzzle) {
//Checking left direction
for(int letter = 1; letter < word.length(); letter++) {
if(puzzle[row + letter][col] != word.charAt(letter)) {
return false;
}
}
return true;
}
private static boolean checkUp(int row, int col, String word, char[][] puzzle) {
//Checking up direction
for(int letter = 1; letter < word.length(); letter++) {
if(puzzle[row - letter][col] != word.charAt(letter)) {
return false;
}
}
return true;
}
private static boolean checkDown(int row, int col, String word, char[][] puzzle) {
//Checking down direction
for(int letter = 1; letter < word.length(); letter++) {
if(puzzle[row][col - letter] != word.charAt(letter)) {
return false;
}
}
return true;
}
private static boolean checkSE(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) && <row<) {
return false;
}
}
return true;
}
private static boolean checkSW(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;
}
}
return true;
}
private static boolean checkNW(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;
}
}
return true;
}
private static boolean checkNE(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;
}
}
return true;
}
}
任何帮助将不胜感激!