2016-04-08 29 views
0

単語検索ゲームを生成するプログラムを作成していますが、まだ初期段階です。私はテキストファイルからすべての入力を受け取り、単語、その最初の行と列の開始点、およびその水平か垂直かを示す配列に変換しました。私は、隠される単語だけを含む基本的なパズル配列を作成する新しいメソッドを開始しました。ArrayIndexOutOfBoundsExceptionの原因

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 
    at WordSearch.createPuzzle(WordSearch.java:50) 
    at WordSearch.main(WordSearch.java:25) 

次のような問題があるのコードのスニペット:

public static char[][] createPuzzle(int rows, int cols, Word[] words) { 
    char[][] puzzle = new char[rows][cols]; 
    for (int i = 0; i <= 9; i++) { 
     int xcord = words[i].getRow(); 
     int ycord = words[i].getCol(); 
     if (words[i].isHorizontal() == true) { 
      String word = words[i].getWord(); 
      for (int j = 0; j < word.length(); j++) {     
       puzzle[xcord + j][ycord] = word.charAt(j); 
      } 
     } else { 
      String word = words[i].getWord(); 
      for (int k = 0; k < word.length(); k++) { 
       puzzle[xcord][ycord + k] = word.charAt(k); 
      } 
     }  
    } 
    return puzzle; 
} 

public static void displayPuzzle(String title, char[][] puzzle) { 
    System.out.println("" + title); 
    for(int i = 0; i < puzzle.length; i++) { 
     for(int j = 0; j < puzzle[i].length; j++) { 
      System.out.print(puzzle[i][j] + " "); 
     } 
     System.out.println(); 
    } 
} 

をして:

displayPuzzle(title, createPuzzle(11, 26, words)); 

コードと一緒に私の主な方法で私の問題は、イムは一貫してきていることです単語配列を作成します。

+0

これは50行目ですか?あなたの言葉が10項目あると確信していますか? – njzk2

+0

行50はpuzzle [xcord + j] [ycord] = word.charAt(j)です。 私の言葉の配列はちょうど10項目あります –

答えて

1
puzzle[xcord + j][ycord] = word.charAt(j); 

xcordが8でjが> 1の場合、パズルボードには9行しかないため、インデックスから範囲外のエラーが発生します。

あなたの言葉がパズルの境界を越えないようにする必要があります。

関連する問題