0

我正在尝试遍历二维数组。我目前在 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;
}
}

任何帮助将不胜感激!

4

2 回答 2

2

您需要一个适用于各个方向的通用函数。也就是说,对于当前位置,您可以通过添加一个offsetxandoffsety来计算您的邻居的位置 range [-1,1]。看看这段代码几乎可以满足您的需求:

//This is your generic-direction function
public static boolean check(int row, int col, String word, char[][] puzzle, int offsetx, int offsety)

    //start with the current position
    int x = row;
    int y = col;

    for (int i = 0; i < word.lenth(); i++){
        //Is not equal
        if (puzzle[x][y] != word.charAt(i)) return false;

        //Calculate the next position
        x += offsetx;
        y += offsety;

        //check the boundaries, if we go out then we didn't find the word;
        if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false;
    }

    return true;
}

这就是我们要求每个方向的方式;

//check right
if(check(row, col, word, puzzle, 1, 0)) return true;

//check left
if(check(row, col, word, puzzle, -1, 0)) return true;

//check top
if(check(row, col, word, puzzle, 0, -1)) return true;

...

但更简单的方法是在数组中定义偏移量,像这样

//These are the offset you add to the current position
int [] movx ={-1, -1, -1, 0, 0, 1, 1, 1};
int [] movy ={-1, 0, 1, -1, 1, -1, 0, 1};

//this variable will hold if we found or not the string in the puzzle
boolean found = false;

//check all directions
for (int i = 0; i < 8; i++){
    //found the string, change the flag 'found' and break
    if(check(row, col, word, puzzle, movx[i], movy[i])){
        found = true;
        break;
    }
}

//string not found
if (!found){
    System.out.println("String not found");
}
于 2013-10-01T19:11:28.050 回答
0

这可以通过限制对您的检查功能的调用来解决。

例如:如果您开始的字母不在最左边,您可以检查左边的单词,斜上和左,斜上和右......因为否则字母没有任何东西。这同样适用于顶部、底部和右侧。

此外,如果您的拼图在 char[][] 拼图中,puzzle[0].length 和拼图.length 将获得拼图的尺寸,用于检查循环等作为上限或起点。

于 2013-10-01T19:10:14.397 回答