Given a arraylist of words, and user input. How to map them into “Families”? in Java
我试图用Java和TreMaPS创建一个邪恶的刽子手游戏。我正在努力想办法把语言融入家庭。我有一个数组列表,它只是一个单词列表和一个表示用户输入/猜测的字符串。由此,我必须创建一个它们生成的模式的映射,以及每个模式匹配的单词数。为了做到这一点,我需要根据用户的猜测将单词列表分成不同的模式和单词。
例如,假设我有一个列表:
Ally,beta,cool,deal,else,flead,good,hope,ibex_
用户猜测一个e。
根据e的位置,每一个词都属于少数几个族中的一个:
"----"是[盟友,酷,好]的模式
"-e——",是[beta,deal]的模式
"--e-",是[fleed,ibex]的模式
"e--e"是[其他]的模式
"——e"是[希望]的模式
我还应该提到,用户也会选择他猜测的单词的长度,因此在这种特定情况下,它只考虑四个字母的单词。
有没有一种方法可以使用treemap对象来帮助绘制哪些单词属于哪些族?例如。。以treemap
我很难弄清楚这一点,所以这是非常不完整的,但到目前为止的代码是?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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; } } |
到目前为止,您取得了良好的进展,请参阅下面的两种方法,这将帮助您填补这些裂缝。
这个方法根据你的猜测和诸如ally、beta、cool、deal、else、flead、good、hope、ibex之类的词获得模式。
1 2 3 4 5 6 7 8 | 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; } |
此方法返回模式映射到它们的单词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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; } |
顺便说一下,我注意到您总是将自己局限于
例如:
和
我不会在这里详细介绍,您可以通过以下链接了解更多信息:1、2、3。