-1

単語検索を行い、その中の単語を見つけるこのコードがあります。BINARYに疲れてDRACを試してみたところうまくいきましたが、一部のもの、特に対角線では、範囲外エラーが発生し続けます。

import java.util.Scanner;

public class WordSearch { 

    public static void main(String[] args) {
        Scanner kboard = new Scanner(System.in);
        char[][] maze = {   {'A','P','B','I','N','A','R','Y','D','C','O','C','A','R','D'},
                            {'M','T','C','O','S','M','A','L','L','E','A','A','T','E','B'},
                            {'P','O','E','N','A','M','G','I','S','R','L','N','S','I','R'},
                            {'R','L','D','H','M','A','S','P','I','N','S','T','E','S','T'},
                            {'A','E','A','E','T','G','T','A','B','M','U','H','A','S','G'},
                            {'L','M','G','N','R','E','D','O','O','P','B','E','F','E','H'},
                            {'U','Y','L','O','E','N','O','K','A','L','L','R','A','M','T'},
                            {'C','I','E','V','E','T','E','C','U','N','A','C','D','Y','I'},
                            {'R','C','C','A','E','H','S','E','E','W','N','U','E','T','A'},
                            {'I','I','I','S','O','N','W','D','D','O','O','L','O','H','N'},
                            {'C','T','N','L','E','H','S','O','H','R','L','E','U','O','J'},
                            {'I','A','E','P','I','R','A','M','O','C','I','S','T','L','I'},
                            {'M','N','R','T','A','E','L','D','E','R','S','P','O','O','L'},
                            {'E','E','E','E','N','R','E','T','T','A','P','A','T','G','I'},
                            {'S','V','B','L','A','C','K','H','O','L','E','S','O','Y','N'},  };                      
        for(int i = 0; i <= 14; i++)
        {
             for(int j = 0; j <= 14; j++)
             {
                 System.out.print(maze[i][j] + " ");
             }
             System.out.println();
        }
        String input = kboard.nextLine();

        //checking the word search horizontally

        for(int r = 0; r <= maze.length - 1; r++)
        {
            for(int c = 0; c <= (maze.length - input.length()); c++)
            {
                boolean match = true;
                for(int i=0; i<input.length(); i++)
                {
                    if(maze[r][c + i] != input.charAt(i))
                    {
                        match = false;
                        break;
                    }
                }
                if(match)
                {
                    System.out.print("Found match (horizontal) for " + input + " starting at (" + r + ", " + c + ")");
                }
            }
        }

        //checking the word search backwards horizontally

        for(int r = 0; r < maze.length - 1; r++)
        {
            for(int c = 0; c <= (maze.length + 3 - input.length()); c++)
            {
                boolean match = true;
                for(int i = 0; i < input.length(); i++)
                {
                    if(maze[r][c - i] != input.charAt(i))
                    {
                        match = false;
                        break;
                    }
                }
                if(match)
                {
                    System.out.print("Found match (backwards horizontal) for " + input + " starting at (" + r + ", " + c + ")");
                }
            }
        }

        //checking the word search diagonally down

        for(int r = 0; r < maze.length - 1; r++)
        {
            for(int c = 0; c <= (maze.length - input.length()); c++)
            {
                boolean match = true;
                for(int i = 0; i < input.length(); i++)
                {
                    if(maze[r + i][c + i] != input.charAt(i))
                    {
                        match = false;
                        break;
                    }
                }
                if(match)
                {
                    System.out.print("Found match (diagonal down) for " + input + " starting at (" + r + ", " + c + ")");
                }
            }
        }

        //searching the word search diagonally up

        for(int r = 0; r < maze.length - 1; r++)
        {
            for(int c = 0; c <= (maze.length - input.length()); c++)
            {
                boolean match = true;
                for(int i = 0; i < input.length(); i++)
                {
                    if(maze[r - i][c - i] != input.charAt(i))
                    {
                        match = false;
                        break;
                    }
                }
                if(match)
                {
                    System.out.print("Found match (diagonal up) for " + input + " starting at (" + r + ", " + c + ")");
                }
            }
        }
    }
}

私は数字を試してみましたが、次のようなエラーが発生し続けます

Found match (horizontal) for BLACKHOLE starting at (14, 2)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at WordSearch.main(WordSearch.java:112)`

また、毎回同じ for ループを使うのは悪いことですか? それぞれの 3 番目の for ループの 1 行だけを変更する必要がありますか?

4

1 に答える 1