2017-05-03 68 views
1

我有一個在學校編程課的項目,我們應該製作一個邪惡的hang子手遊戲,這是計算機在必要之前不會選擇一個詞的地方。爲了愚弄用戶認爲公平遊戲,計算機只考慮具有相同字母圖案的單詞。爲什麼我的數組獲取ArrayIndexOutOfBoundsException?

我得到的東西,我認爲應該工作,但只是猜測字母「a」和「e」我的數組有一個ArrayIndexOutOfBoundsException,我一直試圖找出過去幾個小時使用調試器,但我仍然沒有看到問題,我的意思是整個班級爲第一封信起作用,但第二封只是打破了。當我在HangmanManager類的末尾返回patternArr [indexOfMax]時會發生這種情況。爲什麼它不像我期望的那樣工作?

這是我一直在使用(你必須使用一個文本文件和一幫在裏面的話爲遊戲工作)的字典文件的鏈接:http://www-personal.umich.edu/~jlawler/wordlist.html

現在,這裏是我的程序:

HangmanManager類

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 
import java.util.Set; 
import java.util.SortedSet; 
import java.util.TreeSet; 

public class HangmanManager { 
    private SortedSet<String> wordset; 
    private SortedSet<Character> guessSet; 
    private String wordPattern; 
    private int guessesLeft; 



    public HangmanManager(List<String> dictionary, int length, int max){ 
     if(length < 1 || max < 0) { 
      throw new IllegalArgumentException(); 
     } 
     wordset = new TreeSet<String>(); 
     for(String words: dictionary) { 
      if(words.length() == length) { 
       wordset.add(words); 
      } 
     } 
     wordPattern = ""; 
     for (int i = 0; i < length; i++) { 
      wordPattern += "- "; 
     } 
     guessesLeft = max; 
     guessSet = new TreeSet<Character>(); 

    } 
    //Returns the managers words 
    public Set<String> words() { 
     return wordset; 
    } 

    //Returns the number of guesses left 
    public int guessesLeft() { 
     return guessesLeft - guessSet.size(); 
    } 

    //Returns the guessed letters 
    public SortedSet<Character> guesses() { 
     return guessSet; 
    } 

    //Returns String pattern 
    public String pattern() { 
     return wordPattern; 
    } 
    public int record(char guess) { 
     guessSet.add(guess); 
     return comparePatterns(wordset, guess); 
    } 

    private String makePattern(char guess, String word) { 
     String position = ""; 
     char[] letters = word.toCharArray(); 
     for (int i = 0; i < word.length(); i ++) { 
      if (letters[i] == guess) { 
       position = position + guess + " "; 
      } else { 
       position = position + "- "; 
      } 
     } 
     return position; 
    } 
    //This method is supposed to take out all the patterns that don't match 
    // the String commonPattern 
    private int comparePatterns(SortedSet<String> mySet, char guess) { 
     String[] wordArray = new String[mySet.size()]; 
     wordArray = (String[]) mySet.toArray(wordArray); 
     String[] patternArray = new String[wordArray.length]; 
     List<String> tempList = new ArrayList<String>(); 
     for(int i = 0; i < wordArray.length; i++) { 
      patternArray[i] = makePattern(guess, wordArray[i]); 
     } 
     String commonPattern = getMostCommonPattern(guess, patternArray); 

     int rightGuess = 0; 
     for(int i = 0; i > commonPattern.length(); i ++) { 
      if(commonPattern.charAt(i) == guess) { 
       rightGuess++; 
      } 
     } 
     for(int j = 0; j < patternArray.length; j++) { 
      if(commonPattern.equals(patternArray[j])) { 
       tempList.add(wordArray[j]); 
      } 
     } 
     wordset.removeAll(wordset); 
     wordset.addAll(tempList); 
     return rightGuess; 
    } 
    //This method gets the most common pattern 

    //THIS METHOD BREAKS 
    private String getMostCommonPattern(char guess, String[] patternArr) { 
     List<String> patternList = Arrays.asList(patternArr); 
     int[] countArray = new int[patternArr.length]; 
     for(int i = 0; i < patternArr.length; i++) { 
      countArray[i] = Collections.frequency(patternList, patternArr[i]); 
     } 


     int max = 0; 
     int indexOfMax = 0; 
     for (int j = 0; j < countArray.length; j++) { 
      if(max < countArray[j]) { 
       max = countArray[j]; 
       indexOfMax = j; 
      } else if (max > countArray[j]) { 

      }else if (max == countArray[j]) { 

      } 
     } 

     return patternArr[indexOfMax]; 
    } 
} 

HangmanMain類

import java.util.*; 
import java.io.*; 

public class HangmanMain { 
    public static final String DICTIONARY_FILE = "C:\\Users\\Zoratu\\Desktop\\EvilHangman\\dictionary.txt"; 
    public static final boolean DEBUG = false; // show words left 

    public static void main(String[] args) throws FileNotFoundException { 
     System.out.println("Welcome to the cse143 hangman game."); 
     System.out.println(); 

     // open the dictionary file and read dictionary into an ArrayList 
     Scanner input = new Scanner(new File(DICTIONARY_FILE)); 
     List<String> dictionary = new ArrayList<String>(); 
     while (input.hasNext()) { 
      dictionary.add(input.next().toLowerCase()); 
     } 

     // set basic parameters 
     Scanner console = new Scanner(System.in); 
     System.out.print("What length word do you want to use? "); 
     int length = console.nextInt(); 
     System.out.print("How many wrong answers allowed? "); 
     int max = console.nextInt(); 
     System.out.println(); 

     // set up the HangmanManager and start the game 
     List<String> dictionary2 = Collections.unmodifiableList(dictionary); 
     HangmanManager hangman = new HangmanManager(dictionary2, length, max); 
     if (hangman.words().isEmpty()) { 
      System.out.println("No words of that length in the dictionary."); 
     } else { 
      playGame(console, hangman); 
      showResults(hangman); 
     } 
    } 

    // Plays one game with the user 
    public static void playGame(Scanner console, HangmanManager hangman) { 
     while (hangman.guessesLeft() > 0 && hangman.pattern().contains("-")) { 
      System.out.println("guesses : " + hangman.guessesLeft()); 
      if (DEBUG) { 
       System.out.println(hangman.words().size() + " words left: " 
         + hangman.words()); 
      } 
      System.out.println("guessed : " + hangman.guesses()); 
      System.out.println("current : " + hangman.pattern()); 
      System.out.print("Your guess? "); 
      char ch = console.next().toLowerCase().charAt(0); 
      if (hangman.guesses().contains(ch)) { 
       System.out.println("You already guessed that"); 
      } else { 
       int count = hangman.record(ch); 
       if (count == 0) { 
        System.out.println("Sorry, there are no " + ch + "'s"); 
       } else if (count == 1) { 
        System.out.println("Yes, there is one " + ch); 
       } else { 
        System.out.println("Yes, there are " + count + " " + ch 
          + "'s"); 
       } 
      } 
      System.out.println(); 
     } 
    } 

    // reports the results of the game, including showing the answer 
    public static void showResults(HangmanManager hangman) { 
     // if the game is over, the answer is the first word in the list 
     // of words, so we use an iterator to get it 
     String answer = hangman.words().iterator().next(); 
     System.out.println("answer = " + answer); 
     if (hangman.guessesLeft() > 0) { 
      System.out.println("You beat me"); 
     } else { 
      System.out.println("Sorry, you lose"); 
     } 
    } 

感謝您花時間幫助像我這樣的初學者。

+4

閱讀堆棧跟蹤。在哪一行你會得到異常?你使用什麼索引,你的數組有多大? – Simulant

+0

'ArrayIndexOutOfBoundsException'就是您試圖訪問數組中不存在的索引。就那麼簡單。例如。你有'array {「A」,「B」,「C」}'這是一個長度爲3的數組,這意味着你只能訪問索引0,1和2.如果對於那個數組,你嘗試訪問索引3會得到這個錯誤。所以你必須弄清楚你試圖訪問陣列上不存在的索引的位置。按照@Simulant給出的建議,你會發現。 –

+1

也在'for(int i = 0; i> commonPattern.length(); i ++)'循環永遠不會執行。 –

回答

-1

將行輸出到控制檯或調試文件,因此您可以精確地跟蹤代碼中數據維度被侵犯或處理異常(在try catch上讀取)的代碼位置。

人們想要這樣做的原因是因爲它允許在沒有開發人員工具的情況下進行調試,因爲它是程序的一部分,並且允許最終用戶提交錯誤報告。認識到這些問題還意味着您可以添加代碼以在內部解決問題,而無需用戶意識到它。雖然這只是一個相對簡單的問題,需要內部解決方案,但未來可能會出現一些系統,環境或用戶特定的問題,因此最好早點習慣。

這些都是令人討厭的錯誤,但遇到它們時很容易解決。這幾乎就像忘了托架;簡單的忽視,一個[審查]跟蹤,你會拍自己,當你找到它,或者至少,這是我的經驗;-)

https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

+0

歡迎來到Stack Overflow!在嘗試回答更多問題之前,請閱讀[我如何寫出一個好答案?](http://stackoverflow.com/help/how-to-answer)。 –

相關問題