Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java wordsearch字符数组_Java_Arrays_Char - Fatal编程技术网

Java wordsearch字符数组

Java wordsearch字符数组,java,arrays,char,Java,Arrays,Char,我已经为这个wordsearch项目奋斗了几天了,只是想让横向搜索工作起来。它的意思是在所有8个可能的方向(水平,垂直,对角线)工作。这是我目前的代码 现在我只担心水平面,因为我怀疑如果我把比较做对了,剩下的会更简单 我打算编写代码,查找board数组中包含的字,并将这些字符输出到与board数组大小相同的另一个数组(因此,输出数组就是board数组的解决方案) 到目前为止,我的代码所做的只是遍历整个电路板,然后检查它是否与单词列表的第一个字符匹配,如果匹配,则将该字符分配到输出数组中,最后输出

我已经为这个wordsearch项目奋斗了几天了,只是想让横向搜索工作起来。它的意思是在所有8个可能的方向(水平,垂直,对角线)工作。这是我目前的代码

现在我只担心水平面,因为我怀疑如果我把比较做对了,剩下的会更简单

我打算编写代码,查找board数组中包含的字,并将这些字符输出到与board数组大小相同的另一个数组(因此,输出数组就是board数组的解决方案)

到目前为止,我的代码所做的只是遍历整个电路板,然后检查它是否与单词列表的第一个字符匹配,如果匹配,则将该字符分配到输出数组中,最后输出到控制台供用户查看

我的问题是,我如何转发搜索以迭代单词列表?我的方法错了吗?例如,如果board char与wordlist中的char匹配,则继续按指定的方向(在我的例子中,我担心的是水平方向)查找单词

此外,方法“filter”用于处理异常自动边界,以防搜索失败


欢迎任何想法或方法。

考虑以下计划:

import java.util.ArrayList;

public class WordSearch {

    static char[][] board;
    static int board_x, board_y;
    static ArrayList<String> search_words;

    public static void main(String args[])
    {
        board = new char[][]{
            { 's', 't', 'a', 'c', 'k' },
            { 'x', 'f', 'l', 'o', 'w' },
            { 'x', 'x', 'x', 'v', 'x' },
            { 'x', 'x', 'x', 'e', 'x' },
            { 'x', 'x', 'x', 'r', 'x' },
        };
        // You could also get these from board.size, etc
        board_x = 5;    
        board_y = 5;

        search_words = new ArrayList<String>();
        search_words.add("stack");
        search_words.add("over");
        search_words.add("flow");
        search_words.add("not");

        for(String word : search_words){
            find(word);
        }
    }

    public static void find(String word)
    {
        // Search for the word laid out horizontally
        for(int r=0; r<board_y; r++){
            for(int c=0; c<=(board_x - word.length()); c++){
                // The pair (r,c) will always be where we start checking from
                boolean match = true;
                for(int i=0; i<word.length(); i++){
                    if(board[r][c + i] != word.charAt(i)){
                        match = false;
                        System.out.format("    '%s' not found starting at (%d,%d) -- first failure at %d\n", word, r, c, i);
                        break;
                    }
                }

                if(match){
                    System.out.format("Found match (horizontal) for '%s' starting at (%d,%d)\n", word, r, c);
                }
            }
        }
    }
}

下面是一个在网格中搜索不同方向单词的示例。我已经实现了其中的三个,并将其中的三个留给您完成。在我个人的偏好中,我会在单词列表中使用字符串数组而不是锯齿数组,但我选择了OP的实现方式。我用一个4 x 4的网格和一个3个单词的列表制作了一个简单的版本。请注意,我在输出板上调用了FillWithSpace()。这对于格式化是至关重要的

我有一个名为“board.txt”的文本文件

和一个文本文件“words.txt”

以下是程序的输出:

DCAT
-O--
--G-
SNUR
我的策略是在黑板上搜索单词的第一个字母。找到它后,我会更新静态字段foundRow和foundColumn。当我使用不同的单词时,我会更新静态字段currentWord。当我找到一个匹配的字母时,我有六种不同的方法,checkForwards()checkBackwards()等等。(有其他方法可以做到这一点,但我正试图尽可能清楚地说明这个例子

这是向后检查的方法。因为我已经知道第一个字母匹配,所以我从第二个字母(索引1)开始。对于每个新字符,我检查它是否在板上,然后再比较值。(还有更好的方法可以做到这一点)。如果任何操作失败,我返回。如果所有字符匹配,我一次复制每个字符

static void checkBackwards()
{
    for(int i = 1; i < wordList[currentWord].length; i++)
    {
        if(foundColumn - i < 0) return;
        if(wordList[currentWord][i] != board[foundRow][foundColumn - i]) return;
    }
    //if we got to here, update the output
    for(int i = 0; i < wordList[currentWord].length; i++)
    {
        output[foundRow][foundColumn - i] = wordList[currentWord][i];
    }
    return;
}
static void checkBackwards()
{
for(int i=1;i
以下是源代码:

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

public class Wordsearch 
{
    static Scanner input;
    static char[][] wordList;
    static char[][] board;
    static char[][] output;

    static int foundRow;
    static int foundColumn;
    static int currentWord;

    public static void main(String[] args) throws FileNotFoundException
    {
        File wordInput = new File("words.txt");
        File boardInput = new File("board.txt");
        if(!wordInput.exists() || !boardInput.exists())
        {
            System.out.println("Files do not exist.");
            System.exit(1);
        }

        wordList = new char[3][];   //word list matrix 
        board = new char[4][4]; //board or grid matrix
        output= new char[4][4]; //solved puzzle 

        fillWithSpaces(output);

        input = new Scanner(wordInput);
        for(int i = 0; i < wordList.length; i++)
        {
            wordList[i] = input.nextLine().toUpperCase().toCharArray();
        }

        input = new Scanner(boardInput);
        for(int i = 0; i < board[0].length; i++)
        {
            board[i] = input.nextLine().toUpperCase().toCharArray();
        }

        for(int i = 0; i < wordList.length; i++)
        {
            currentWord = i;
            if(findFirstLetter())
            {
                checkEachDirection();
            }
        }

        print(output);
    }

    static boolean findFirstLetter()
    {
        for(int r = 0; r < board.length; r++)
        {
            for(int c = 0; c < board.length; c++)
            {
                if(wordList[currentWord][0] == board[r][c])
                {
                    foundRow = r;
                    foundColumn = c;
                    return true;
                }
            }
        }
        return false;
    }

    static void checkEachDirection()
    {
        checkForwards();
        checkBackwards();
        //checkUp();
        //checkDown();
        checkDiagonalDown();
        //checkDiagonalUp();
    }

    static void checkForwards()
    {
        for(int i = 1; i < wordList[currentWord].length; i++)
        {
            if(foundColumn + i > board.length - 1) return;
            if(wordList[currentWord][i] != board[foundRow][foundColumn + i]) return;
        }
        //if we got to here, update the output
        for(int i = 0; i < wordList[currentWord].length; i++)
        {
            output[foundRow][foundColumn + i] = wordList[currentWord][i];
        }
        return;
    }

    static void checkBackwards()
    {
        for(int i = 1; i < wordList[currentWord].length; i++)
        {
            if(foundColumn - i < 0) return;
            if(wordList[currentWord][i] != board[foundRow][foundColumn - i]) return;
        }
        //if we got to here, update the output
        for(int i = 0; i < wordList[currentWord].length; i++)
        {
            output[foundRow][foundColumn - i] = wordList[currentWord][i];
        }
        return;
    }

    static void checkDiagonalDown()
    {
        for(int i = 1; i < wordList[currentWord].length; i++)
        {
            if(foundColumn + i > board.length - 1) return;
            if(foundRow + i > board.length - 1) return;
            if(wordList[currentWord][i] != board[foundRow + i][foundColumn + i]) return;
        }
        //if we got to here, update the output
        for(int i = 0; i < wordList[currentWord].length; i++)
        {
            output[foundRow + i][foundColumn + i] = wordList[currentWord][i];
        }
        return;
    }

    static void print(char[][] board)
    {
        for(int i = 0; i < board.length; i++)
        {
            for(int j = 0; j < board.length; j++)
            {
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }

    static void fillWithSpaces(char[][] board)
    {
        for(int i = 0; i < board.length; i++)
        {
            for(int j = 0; j < board.length; j++)
            {
                board[i][j] = '-';
            }
        }
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类词汇搜索
{
静态扫描仪输入;
静态字符[][]字列表;
静态字符[][]板;
静态字符[][]输出;
静态int foundRow;
静态int-foundColumn;
静态int-currentWord;
公共静态void main(字符串[]args)引发FileNotFoundException
{
File wordInput=新文件(“words.txt”);
文件boardInput=新文件(“board.txt”);
如果(!wordInput.exists()| |!boardInput.exists())
{
System.out.println(“文件不存在”);
系统出口(1);
}
wordList=新字符[3][];//单词列表矩阵
线路板=新字符[4][4];//线路板或网格矩阵
输出=新字符[4][4];//已解决的难题
填充空间(输出);
输入=新扫描仪(wordInput);
for(int i=0;iboard.length-1)返回;
if(wordList[currentWord][i]!=board[foundRow][foundColumn+i])返回;
}
//如果我们到了这里,更新输出
for(int i=0;iDCAT
-O--
--G-
SNUR
static void checkBackwards()
{
    for(int i = 1; i < wordList[currentWord].length; i++)
    {
        if(foundColumn - i < 0) return;
        if(wordList[currentWord][i] != board[foundRow][foundColumn - i]) return;
    }
    //if we got to here, update the output
    for(int i = 0; i < wordList[currentWord].length; i++)
    {
        output[foundRow][foundColumn - i] = wordList[currentWord][i];
    }
    return;
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Wordsearch 
{
    static Scanner input;
    static char[][] wordList;
    static char[][] board;
    static char[][] output;

    static int foundRow;
    static int foundColumn;
    static int currentWord;

    public static void main(String[] args) throws FileNotFoundException
    {
        File wordInput = new File("words.txt");
        File boardInput = new File("board.txt");
        if(!wordInput.exists() || !boardInput.exists())
        {
            System.out.println("Files do not exist.");
            System.exit(1);
        }

        wordList = new char[3][];   //word list matrix 
        board = new char[4][4]; //board or grid matrix
        output= new char[4][4]; //solved puzzle 

        fillWithSpaces(output);

        input = new Scanner(wordInput);
        for(int i = 0; i < wordList.length; i++)
        {
            wordList[i] = input.nextLine().toUpperCase().toCharArray();
        }

        input = new Scanner(boardInput);
        for(int i = 0; i < board[0].length; i++)
        {
            board[i] = input.nextLine().toUpperCase().toCharArray();
        }

        for(int i = 0; i < wordList.length; i++)
        {
            currentWord = i;
            if(findFirstLetter())
            {
                checkEachDirection();
            }
        }

        print(output);
    }

    static boolean findFirstLetter()
    {
        for(int r = 0; r < board.length; r++)
        {
            for(int c = 0; c < board.length; c++)
            {
                if(wordList[currentWord][0] == board[r][c])
                {
                    foundRow = r;
                    foundColumn = c;
                    return true;
                }
            }
        }
        return false;
    }

    static void checkEachDirection()
    {
        checkForwards();
        checkBackwards();
        //checkUp();
        //checkDown();
        checkDiagonalDown();
        //checkDiagonalUp();
    }

    static void checkForwards()
    {
        for(int i = 1; i < wordList[currentWord].length; i++)
        {
            if(foundColumn + i > board.length - 1) return;
            if(wordList[currentWord][i] != board[foundRow][foundColumn + i]) return;
        }
        //if we got to here, update the output
        for(int i = 0; i < wordList[currentWord].length; i++)
        {
            output[foundRow][foundColumn + i] = wordList[currentWord][i];
        }
        return;
    }

    static void checkBackwards()
    {
        for(int i = 1; i < wordList[currentWord].length; i++)
        {
            if(foundColumn - i < 0) return;
            if(wordList[currentWord][i] != board[foundRow][foundColumn - i]) return;
        }
        //if we got to here, update the output
        for(int i = 0; i < wordList[currentWord].length; i++)
        {
            output[foundRow][foundColumn - i] = wordList[currentWord][i];
        }
        return;
    }

    static void checkDiagonalDown()
    {
        for(int i = 1; i < wordList[currentWord].length; i++)
        {
            if(foundColumn + i > board.length - 1) return;
            if(foundRow + i > board.length - 1) return;
            if(wordList[currentWord][i] != board[foundRow + i][foundColumn + i]) return;
        }
        //if we got to here, update the output
        for(int i = 0; i < wordList[currentWord].length; i++)
        {
            output[foundRow + i][foundColumn + i] = wordList[currentWord][i];
        }
        return;
    }

    static void print(char[][] board)
    {
        for(int i = 0; i < board.length; i++)
        {
            for(int j = 0; j < board.length; j++)
            {
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }

    static void fillWithSpaces(char[][] board)
    {
        for(int i = 0; i < board.length; i++)
        {
            for(int j = 0; j < board.length; j++)
            {
                board[i][j] = '-';
            }
        }
    }
}
        // STEP1: Find *************IF ******************* The word is in
        // the Array
        // CODE HERE
        boolean found = false;
        for (int i = 0; i < puzzle.length; i++) {
            String rowString = "";
            for (int j = 0; j < puzzle[i].length; j++) {
                rowString += puzzle[i][j];
                if (rowString.contains(keyWord) && !found) {

                    System.out.println(keyWord);
                    int index = rowString.indexOf(keyWord);
                    rowString.indexOf(keyWord);
                    // int length = keyWord.length();
                    for (int ii = 0; ii < keyWord.length(); ii++) {
                        solutionArray[i][index + ii] = keyWord.charAt(ii);
                        rowString += puzzle[i][j];
                        System.out.println();
                        // length--;
                    }
                    found = true;
                }

            }
        }