邪恶的Hangman Java程序

时间:2016-03-09 03:54:40

标签: java maps

我正在制作一个邪恶的Hangman程序由于某些原因我收到了这个错误:

What length word do you want to use? 4
How many wrong answers allowed? 4

guesses : 4
guessed : []
current :  ----
Your guess? e
Exception in thread "main" java.lang.NullPointerException
    at Game.HangmanManager.record(HangmanManager.java:142)
    at Game.HangmanMain.playGame(HangmanMain.java:62)
    at Game.HangmanMain.main(HangmanMain.java:42)

这是我的计划:

package Game;

import java.util.*;

public class HangmanManager
{

    private String pattern;
    private int max;
    private int length;
    private SortedSet<Character> guessesMade;
    private Set<String> currentWords;
    private Map<String, Set<String>> patternMap;

    public HangmanManager(List<String> dictionary , int length, int max)
    {
        this.max = max;
        this.length = length;


        if( length < 1 && max < 0)
            {
                throw new IllegalArgumentException();

            }

        words = new TreeSet<String>(); 
        guessesMade = new TreeSet<Character>();
        currentWords = new TreeSet<String>(); // current words =(words)
        patternMap = new TreeMap<String, Set<String>>(); // patternMAP = < pattern, words>

        for (String word : dictionary)
        {
            if (word.length() == length)
            {
            words.add(word); // if length of the word matches a word with the same length it will  be added
            }

        }

    }


    public Set<String> words()
    {
        return words;
    }


    public int guessesLeft()
    {
        return max - guessesMade.size();
    }



    public SortedSet<Character> guesses()
    {
        return guessesMade;
    }


    public String pattern()
    {
        if (words.isEmpty())
        {
            throw new IllegalArgumentException("Invalid No Words");
        }
        pattern = " "; // blank for now
        for (int i = 0; i < length; i++)
        {
            pattern += "-"; // will have a "-" for how long the length of the word is 
        }

        return pattern; // will return the number of lines
    }


    public int record(char guess)
    {
          if (guessesLeft() < 1 || words.isEmpty())
          {
                throw new IllegalStateException();  
          }

          if (!words.isEmpty() && guessesMade.contains(guess)) 
          {
                throw new IllegalArgumentException();
          }

          guessesMade.add(guess); // guess
          int occurences = 0;

          for( String word: words)
          {           
              if( patternMap.containsKey (pattern))
              {

                    occurences = generatePattern(word, guess); // the word including the guess letter will fill in the blank spots
                    currentWords.add(word); // the word will be added to the possibilities 
                    currentWords = patternMap.get(pattern); // the word will be able to fill once the guesses are made
//                  if(patternMap.get(pattern)!=null)
//                      {
//                          currentWords = patternMap.get(pattern);
//                          
//                      }
                    patternMap.put(pattern, currentWords);  
              }
              else
              {
                 currentWords.add(word);
                 patternMap.put(pattern, currentWords);
              } 
          }

         words = find();
         return occurences;
    }


    private Set<String> find()
    {
        int maxSize = 0;

        Map <String, Integer> patternCount = new TreeMap<String, Integer>();

        for (String key : patternMap.keySet()) // keyset equals word
        {
            patternCount.put(key, patternMap.get(key).size()); // size of the word

                if (patternMap.get(key).size() > maxSize)
                {
                    maxSize = patternMap.get(key).size();
                    pattern = key; // pattern will becomes based on the word
                } else if (patternMap.get(key).size() == maxSize)
                {
                    if (key.length() >= pattern.length())
                    {
                        pattern = key;
                        maxSize = patternMap.get(key).size(); // the pattern is now the word key 
                    }
                }
            }
        System.out.println("Current pattern: " + pattern);

        return patternMap.get(pattern); // the pattern that will becomes now that the word was picked
    }

    private int generatePattern(String s, char guess) 
    {
        int count = 0;
        pattern = "";
            for (int i = 0; i < length; i++)
            {
                if (s.charAt(i) == guess)
                {
                    pattern += guess + " ";
                    count++;
                } else 
                {
                    pattern += "- ";
                }
            }
        return count;
    }

}

错误似乎发生在:

的记录方法中 第149行

patternMap.put(pattern, currentWords);

我多次运行调试器,我注意到如果你按照程序进行操作,你会发现当一个程序在一段时间后运行单词后,currentWords变为Null,即使我实例化它并且我为patternMap创建了一个Map。

如果有人能告诉我该做什么或改变什么,我会非常感激,因为我对此感到很失落

1 个答案:

答案 0 :(得分:0)

patternMapTreeMap。正如您所说,问题在于第149行patternMap.put(pattern, currentWords);。根据TreeMap的JavaDocs,put()抛出NullPointerException ...

  

如果指定的键为null并且此映射使用自然排序,或者其比较器不允许空键

由于TreeMap<String,Object>使用其密钥的自然排序(即String),问题是patternnull。为什么不直接将pattern初始化为默认值:

private String pattern = " ";