1

所以我正在编写一个填字游戏求解器,除了 1 个故障外,我几乎 98% 的代码都在工作。我有一个循环,它从文件中读取 1 个单词,然后将该字符串与一个字符数组进行比较,以查找该字符串在数组中的位置。到目前为止,循环读取所有单词并找到它们的位置,但我的问题是它跳过了我列表中的 3 个单词。我知道这些词在 char 数组中,但由于某种原因,它们没有被拾取。有任何想法吗?

这是我的代码:

    import java.io.File;
    import java.util.Scanner;


    public class Test {

        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);
                    }
                }

            //Read the number of words and move to the next line    
            int numwords = sc.nextInt();
            sc.nextLine();  


            //look for each word
            for(int i=0; i<numwords; i++){
                String word = new String();
                word = sc.nextLine();
                System.out.printf("This is word: %s\n", word);

                //arrays to hold the direction.
                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;

                //find the words in the puzzle
                for(int m = 0; m < puzzle.length; m++) {
                    for(int n = 0; n < puzzle[0].length; n++) {
                        if(puzzle[m][n] == word.charAt(0)){
                            for (int o = 0; o < 8; o++){
                                if(check(m, n, word, puzzle, movx[o], movy[o])){
                                    System.out.printf("%s found at position (%d, %d)\n\n", word, m, n);
                                    found = true;
                                    break;
                                    }
                            }

                        }
                    }
                }
                if (!found){
                    System.out.printf("%s was not found\n\n", word);
                }
            }

        //Close the scanner     
            sc.close();         
        }   
        //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.length(); i++){
                char c = word.charAt(i);

                //Is not equal
                if (puzzle[x][y] != c) return false;

                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;
        }

    }

(编辑区)

根据我对打印语句的诊断,当 i 是 3、8 和 9 时,wordsearch 找不到这些词。我相信这是在这个循环之后:

    //find the words in the puzzle
for(int m = 0; m < puzzle.length; m++)

因为在这个循环之前,我之前的循环遍历了每个单词。并且单词被传递到这个循环内的循环中。所以它一直通过数组中每个点的所有检查,然后即使它在谜题中也只是传递为假。

(/编辑区)

在这个循环之前,我测试并

我正在阅读的文件:

    10 10
    WVERTICALL
    ROOAFFLSAB
    ACRILIATOA
    NDODKONWDC
    DRKESOODDK
    OEEPZEGLIW
    MSIIHOAERA
    ALRKRRIRER
    KODIDEDRCD
    HELWSLEUTH
    10
    WEEK
    FIND
    RANDOM
    SLEUTH
    BACKWARD
    VERTICAL
    DIAGONAL
    WIKIPEDIA
    HORIZONTAL
    WORDSEARCH

这是它打印的内容(WEEK 不在拼图中):

    This is word: WEEK and this is i 0
    WEEK was not found 

    This is word: FIND and this is i 1
    FIND found at position (1, 4)

    This is word: RANDOM and this is i 2
    RANDOM found at position (1, 0)

    This is word: SLEUTH and this is i 3
    SLEUTH was not found 

    This is word: BACKWARD and this is i 4
    BACKWARD found at position (1, 9)

    This is word: VERTICAL and this is i 5
    VERTICAL found at position (0, 1)

    This is word: DIAGONAL and this is i 6
    DIAGONAL found at position (8, 6)

    This is word: WIKIPEDIA and this is i 7
    WIKIPEDIA found at position (9, 3)

    This is word: HORIZONTAL and this is i 8
    HORIZONTAL was not found 

    This is word: WORDSEARCH and this is i 9
    WORDSEARCH was not found

这些是它找不到的词:

    SLEUTH is at position (9,4) 
    HORIZONTAL is at position (9,0)
    WORDSEARCH is at position (0,0)

非常感谢任何提示、技巧或想法!

4

2 回答 2

4

在您的检查方法中:

//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;

正在检查您是否在增加 x,y 值后超出了拼图边界。问题在于它找不到的三个单词,它找到最后一个字母,增加你的计数器,然后确定你已经离开了拼图的边缘并返回 false,即使它已经找到了最后一个字母。

尝试将条件移动到循环的开头,如下所示:

for (int i = 0; i < word.length(); i++){
    //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;

    char c = word.charAt(i);

    //Is not equal
    if (puzzle[x][y] != c) return false;

    x += offsetx;
    y += offsety;
} 
于 2013-10-02T01:51:39.203 回答
1

缺少的是您的check方法中的一个条件。当单词的最后一个字符位于某个边缘时,它会失败,因为它试图找到下一个邻居(边界检查)。if (i == word.length() - 1) return true;后添加此条件if (puzzle[x][y] != c) return false;

于 2013-10-02T01:54:27.897 回答