繁体   English   中英

Java wordsearch Char数组

[英]Java wordsearch Char arrays

我已经为这个单词搜索项目苦苦挣扎了几天,只是试图使横向搜索正常工作。 它适用于所有8个可能的方向(水平,垂直,对角线)。 这是我目前的代码。

现在,我只担心水平,因为我怀疑如果比较正确,其余的将更简单。

我的意思是编写查找board数组中包含的单词的代码,并将这些字符输出到另一个与board数组相同的数组(因此,输出数组是board数组的解决方案)。

到目前为止,我的所有代码都是遍历整个板,然后检查它是否与wordlist的第一个字符匹配,如果是,那么这个字符在输出数组上分配,最后打印到最后控制台供用户查看。

我的问题是,如何转发我的搜索以迭代词表? 我的方法有误吗? 例如,如果board char与wordlist中的char匹配,则继续沿指定的方向(在我的情况下,我担心水平方向)并找到该单词。

此外,方法'过滤器'用于处理exceptionOutofBounds,以防搜索离开板。

欢迎任何想法或方法。

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

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

dcat
aoiq
eigk
snur

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

dog
cat
runs

这是程序的输出:

DCAT
-O--
--G-
SNUR

我的策略是在电路板上搜索单词的第一个字母。 一旦找到它,我就更新了静态字段foundRow和foundColumn。 当我使用不同的单词时,我更新静态字段currentWord。 当我找到匹配的字母时,我有六种不同的方法,checkForwards()checkBackwards()等等。 (还有其他方法可以做到这一点,但我正在尝试使示例尽可能清楚。

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

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] = '-';
            }
        }
    }
}

考虑以下程序:

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

面板是一个二维char数组,您要搜索的单词列表是一个名为search_words的ArrayList。

在对板和search_words列表进行一些简单的样本初始化之后,它遍历列表中的单词,搜索每个单词是否水平放置。

这个想法可以扩展到垂直或对角搜索以及一些调整。

这里的逻辑是您应该从示例程序中获取的,而不一定是结构。 如果我出于任何严重的考虑而这样做,则可能会有一个Board类,可能带有.find(word)方法。

最后,详细输出为:

Found match (horizontal) for 'stack' starting at (0,0)
    'stack' not found starting at (1,0) -- first failure at 0
    'stack' not found starting at (2,0) -- first failure at 0
    'stack' not found starting at (3,0) -- first failure at 0
    'stack' not found starting at (4,0) -- first failure at 0
    'over' not found starting at (0,0) -- first failure at 0
    'over' not found starting at (0,1) -- first failure at 0
    'over' not found starting at (1,0) -- first failure at 0
    'over' not found starting at (1,1) -- first failure at 0
    'over' not found starting at (2,0) -- first failure at 0
    'over' not found starting at (2,1) -- first failure at 0
    'over' not found starting at (3,0) -- first failure at 0
    'over' not found starting at (3,1) -- first failure at 0
    'over' not found starting at (4,0) -- first failure at 0
    'over' not found starting at (4,1) -- first failure at 0
    'flow' not found starting at (0,0) -- first failure at 0
    'flow' not found starting at (0,1) -- first failure at 0
    'flow' not found starting at (1,0) -- first failure at 0
Found match (horizontal) for 'flow' starting at (1,1)
    'flow' not found starting at (2,0) -- first failure at 0
    'flow' not found starting at (2,1) -- first failure at 0
    'flow' not found starting at (3,0) -- first failure at 0
    'flow' not found starting at (3,1) -- first failure at 0
    'flow' not found starting at (4,0) -- first failure at 0
    'flow' not found starting at (4,1) -- first failure at 0
    'not' not found starting at (0,0) -- first failure at 0
    'not' not found starting at (0,1) -- first failure at 0
    'not' not found starting at (0,2) -- first failure at 0
    'not' not found starting at (1,0) -- first failure at 0
    'not' not found starting at (1,1) -- first failure at 0
    'not' not found starting at (1,2) -- first failure at 0
    'not' not found starting at (2,0) -- first failure at 0
    'not' not found starting at (2,1) -- first failure at 0
    'not' not found starting at (2,2) -- first failure at 0
    'not' not found starting at (3,0) -- first failure at 0
    'not' not found starting at (3,1) -- first failure at 0
    'not' not found starting at (3,2) -- first failure at 0
    'not' not found starting at (4,0) -- first failure at 0
    'not' not found starting at (4,1) -- first failure at 0
    'not' not found starting at (4,2) -- first failure at 0

您可以尝试水平搜索String word =“”; for(String keyWord:words){

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

            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM