我正在尝试为刽子手游戏制作 AI,其中一部分需要计算单词列表中每个可能字符的所有出现次数。我计划在此计数之前剔除单词列表以使事情运行得更快(首先剔除与可猜测短语长度不同的所有单词,然后剔除与猜测字符不匹配的单词)。
我遇到的问题在下面的代码中。不知何故,它总是返回一个长度正确的 e 列表(匹配可能的字符数)。我不确定我在这里做错了什么,但问题肯定出在 countCharacters 的某个地方。
MethodicComputer(){
guessable = parseGuessable();
wordList = parseText();
priorities = countCharacters(guessable);
}
public char guessCharacter(String hint){
char guess = 0;
System.out.println(guessable);
System.out.println(priorities);
guess = priorities.charAt(0);
priorities = priorities.replaceAll("" + guess, "");
return guess;
}
private String countCharacters(String possibleChars){
charCount = new Hashtable();
String orderedPriorities = "";
char temp = 0;
char adding = 0;
int count = 0;
int max = 0;
int length = possibleChars.length();
for (int i = 0; i<length; i++){
temp = possibleChars.charAt(i);
count = wordList.length() - wordList.replaceAll("" + temp, "").length();
charCount.put(temp, count);
}
while (orderedPriorities.length() < length){
for (int i = 0; i < possibleChars.length(); i++){
temp = possibleChars.charAt(i);
if (max < (int) charCount.get(temp)){
max = (int) charCount.get(temp);
adding = temp;
}
}
orderedPriorities += adding;
possibleChars = possibleChars.replaceAll("" + adding, "");
}
return orderedPriorities;
}