给出一个单词的arraylist和用户输入。如何将它们映射到“家庭”?在Java中

时间:2014-07-01 01:26:50

标签: java

我正在尝试使用Java和TreeMaps创建一个邪恶的刽子手游戏。我正在试图弄清楚如何将文字放入家庭。我有一个ArrayList,它只是一个单词列表和一个表示用户输入/猜测的String。由此我必须创建一个他们生成的模式的映射以及每个模式匹配的单词数。为了做到这一点,我需要根据用户猜测将单词列表分成不同的模式和单词。

例如,假设我有一个列表:

{ALLY,BETA,COOL,DEAL,ELSE,FLEW,GOOD,HOPE,IBEX}

和用户猜测一个E.

根据E的位置,每个单词都属于少数几个家庭之一:

“----”,是[ALLY,COOL,GOOD]的模式

“ - E--”,是[BETA,DEAL]的模式

“ - E-”,是[FLEW,IBEX]

的模式

“E - E”,是[ELSE]

的模式

“--- E”,是[HOPE]

的模式

我还应该提到,用户也会选择他猜测的单词的长度,因此在这种特定情况下,它只会考虑四个字母单词。

有没有办法使用TreeMap对象帮助确定哪些词属于哪些家庭?例如,将它放在TreeMap< String,ArrayList<字符串>取代。

我在解决这个方面遇到了很多麻烦,所以这是非常不完整的,但到目前为止的代码是什么?

public class Hangman {


// instance vars
private ArrayList<String> list;
private boolean debugging;
private ArrayList<Character> guess;
private int numGuesses;
private String pattern;

// pre: words != null, words.size() > 0
// if debugOn = true, debuggin output is added
public HangmanManager(List<String> words, boolean debugOn) {
    list = new ArrayList<String>();
    debugging = debugOn;
    for(int i = 0; i < words.size(); i++){
        list.add(words.get(i));
    }
}

// pre: words != null, words.size() > 0
// debuggin output is not added
public HangmanManager(List<String> words) {
    list = new ArrayList<String>();
    for(int i = 0; i < words.size(); i++){
        list.add(words.get(i));
    }
}
public TreeMap<String, Integer> makeGuess(char guess) {
    if(alreadyGuessed(guess)){
        throw new IllegalStateException("Not valid imput.");
    }
    TreeMap<String, ArrayList<String>> newList = new TreeMap<String, ArrayList<String>>();

    newList.put(str, list);
    return null;
}
//helper method to generate an ArrayList that contains the letter that the user guesses
public ArrayList<String> getArrayList(char guess){
    String str = guess + "";
    ArrayList<String> newList = new ArrayList<String>();
    for(int i = 0; i < list.size(); i++){
        if(list.get(i).contains(str)){
            newList.add(list.get(i));
        }
    }
    return newList;
}

//helper method to break up the current word list into different patterns and words based on the user guess.
public TreeMap<String, ArrayList<String>> breakUp(char guess){
    Map<String, ArrayList<String>> newList = new TreeMap<String, ArrayList<String>>();
    String str = guess + "";
    newList.put(str, list);
    return null;
}

}

1 个答案:

答案 0 :(得分:2)

到目前为止,您取得了很好的进展,请参阅以下2种方法,以帮助您填补空白。

此方法根据您的猜测和{ALLY,BETA,COOL,DEAL,ELSE,FLEW,GOOD,HOPE,IBEX}等字词获取模式。

public String getPatternForWord(char guess, String word) {
    //regex to match all non-guess characters (ex: [^E] if guess was 'E')
    String replaceRegex = "[^" + guess + "]";

    //replace all non-guess characters with '-' (ex: replace all non-'E' with '-')
    String pattern = word.replaceAll(replaceRegex, "-");
    return pattern;
}

此方法会将模式地图返回到其单词Map<String, List<String>>
例如:{----=[ALLY, COOL, GOOD], ---E=[HOPE], --E-=[FLEW, IBEX], -E--=[BETA, DEAL], E--E=[ELSE]}

public Map<String, List<String>> getPatternMapForGuess(char guess) {
    Map<String, List<String>> newMap = new TreeMap<String, List<String>>();

    for (String word : list) {
        String pattern = getPatternForWord(guess, word);

        //get the list of words for this pattern from map
        List<String> wordList;
        if (newMap.containsKey(pattern)) {
            wordList = newMap.get(pattern);
        } else {
            wordList = new ArrayList<String>();
        }

        //add word to list if it isn't there already
        if (!wordList.contains(word)) {
            wordList.add(word);
        }

        //pattern : word list
        newMap.put(pattern, wordList);
    }

    return newMap;
}

顺便说一句,我注意到您始终将自己限制在ArrayList<String>的特定实施List<String>,而不仅仅是接口List<String>。这是一个很好的OOP实践,可以编程到接口而不是特定的实现,因为它可以提高您的灵活性。

例如:
List<String> newList = new ArrayList<String>(); 而不是
ArrayList<String> newList = new ArrayList<String>();

Map<String, List<String>> newList = new TreeMap<String, List<String>>(); 而不是
TreeMap<String, ArrayList<String>> newList = new TreeMap<String, ArrayList<String>>();

我在这里不会详细介绍,您可以通过以下链接了解更多信息:123