在java 2d数组中搜索单词

时间:2013-02-15 05:15:53

标签: java algorithm search iteration multidimensional-array

我正在尝试为类分配创建一个简单的单词搜索,我已经设法弄清楚如何搜索东(从左到右)和西(从右到左)。但我无法弄清楚如何搜索南方(从上到下)。

我为一个文件工作的代码我读了但第二个文件返回ArrayIndexOutOfBoundsException。我的代码中是否有任何特定的内容会使其无法扩展?

我的更正的代码如下所示:

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

import wordSeek.GameBoard;

public class WordGame {

    private char[][] letters;
    GameBoard gb;

    public static void main(String[] args) {
        WordGame wg = new WordGame();
        wg.play();

    }

    public WordGame() {
        letters = readLettersFromFile();
        gb = new GameBoard(letters);

    }

    private void play() {
        Scanner s = new Scanner(System.in);
        String word;

        do {
            System.out.println("Enter word to find: ");
            word = s.next();

            // reset all highlighted tiles
            gb.reset();

            search(word);

        } while (!word.equals("QUIT"));

        gb.dispose();
    }

    // Nothing to be done above
    // Complete all the methods below

    private char[][] readLettersFromFile() {
        // From the data in the file Letters.txt determine the size (number of
        // rows and number of columns) for the letters array

        int rowCount = 0;
        int colCount = 0;
        char c;

        File file = new File("resources/Places.txt");

        Scanner fileScanner = null;
        try {
            fileScanner = new Scanner(file);
            } 
        catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        ArrayList<String> data = new ArrayList<String>();

        while(fileScanner.hasNextLine())
        {
            String line = fileScanner.nextLine();
            data.add(line);
        }

        fileScanner.close();
        rowCount = data.size();
        colCount = data.get(0).trim().length()/2+1;

        // Instantiate a two dimensional array of characters of the appropriate
        // size

        letters = new char [rowCount][colCount];

        // Populate the array with the letters in Letters.txt
        for (int i = 0; i < rowCount; i++) {
            String line = data.get(i);
            int pos = 0;
            for (int j = 0; j < colCount; j++) {
                letters[i][j] = line.charAt(pos);
                pos += 2;
            }

        }

        // return the array

        return letters;

    }

    private void search(String word) {
        System.out.println("Searching for " + word);

        //Call the other search methods below as needed
        searchIterativeEast(word);
        searchIterativeWest(word);
        searchIterativeSouth(word);
        searchIterativeNorth(word);
    }

    //The following four methods must employ ITERATION to search the game board
    private boolean searchIterativeEast(String word) {
        int k = 0;

        for (int i = 0; i < letters.length; i++) 
        {
            for (int j = 0; j < letters[i].length; j++) {
                if (word.charAt(k) == letters[i][j]) {
                    k++;
                } 
                else {
                    k = 0;
                }
                if (k == word.length()) {
                    for (int col = j - k + 1; col <= j; col++) {
                        gb.highlight(i, col);
                    }

                    return true;
                }
            }
        }

        return false;
    }

    private boolean searchIterativeWest(String word) {

            String reversedWord = "";
            for (int i = word.length() - 1; i != -1; i--)
            {
                reversedWord += word.charAt(i);
            }

            int k = 0;

            for (int i = 0; i < letters.length; i++) 
            {
                for (int j = 0; j < letters[i].length; j++) {
                    if (reversedWord.charAt(k) == letters[i][j]) {
                        k++;
                    } 
                    else {
                        k = 0;
                    }
                    if (k == reversedWord.length()) {
                        for (int col = j - k + 1; col <= j; col++) {
                            gb.highlight(i, col);
                        }

                        return true;
                    }
                }
            }

            return false;

    }

    private boolean searchIterativeSouth(String word) {
        int k = 0;
        int store = letters[0].length;

        for (int j = 0; j < letters[store].length; j++)
        {
            for (int i = 0; i < letters.length; i++)
            {
                if (word.charAt(k) == letters[i][j])
                {
                    k++;
                }
                else
                {
                    k = 0;
                }
                if (k == word.length())
                {
                    for(int row = i-k+1 ; row <= i; row++)
                    {
                        gb.highlight(row, j);
                    }
                    return true;
                }
            }
        }

        return false;
    }

    private boolean searchIterativeNorth(String word) {

        String reversedWord = "";
        for (int i = word.length() - 1; i != -1; i--)
        {
            reversedWord += word.charAt(i);
        }

        int k = 0;
        int store = 0;

        for(int i = 0; i < letters.length; i++)
        {
            store = letters[i].length;
        }

        for (int j = 0; j < letters[store].length; j++)
        {
            for (int i = 0; i < letters.length; i++)
            {
                if (reversedWord.charAt(k) == letters[i][j])
                {
                    k++;
                }
                else
                {
                    k = 0;
                }
                if (k == reversedWord.length())
                {
                    for(int row = i-k+1 ; row <= i; row++)
                    {
                        gb.highlight(row, j);
                    }
                    return true;
                }
            }
        }

        return false;
    }

第一个文件(Animals.txt)的Gameboard看起来像:5X4 2d Array

X C A T
P A L Q
I R B U
G P X N
G O D W

输出突出显示CARP。

第二个文件(Places.txt)的Gameboard看起来像:11x15 2d Array

O M J G D A X V C S Q N K I F 
D A X V T Q O M J H A A H F C 
A Y W U R P N L F E I T A L Y
J N H N E T H E R L A N D S F 
D B I Z X V T O A R Q O A Y K 
M K I A H F K R N O D B N N I
N Z Y W P H T V C G C T A A N 
R A Q O T S N L E K K I C M G 
I H P U U F D C A Z D O X R D 
X W O A L E U Z E N E V N E O 
V S U S J R Q L I Z A R B G M 

2 个答案:

答案 0 :(得分:1)

for (int j = 0; j < letters[i].length; j++)

而不是

for (int j = 0; j < letters.length; j++)

答案 1 :(得分:1)

return语句超出for循环,否则你只会突出显示第一个字母。

...
for (int row = j-k+1; row <=i; row++ )
    {
        //gb.highlight() just calls a method that    highlights the letters.
        gb.highlight(row, j);
        //return true;
    }
return true;
...