我在学校的编程课程中有一个项目,我们应该制作一个邪恶的刽子手游戏,这是计算机在必须之前不会选择一个单词的地方。为了欺骗用户认为它正在公平地玩,计算机只考虑具有相同字母模式的单词。
我得到的东西我认为应该可以工作但是在猜测字母“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");
}
}
感谢您抽出时间帮助像我这样的初学者。
答案 0 :(得分:-1)
输出到控制台或调试文件的行,这样您就可以准确地跟踪代码中违反数组维度的位置,或者处理异常(在try catch上读取)。
之所以想要这样做是因为它允许在没有开发人员工具的情况下进行调试,因为它是程序的一部分,并允许最终用户提交错误报告。识别这些类型的问题也意味着您可以添加代码以在内部解决它,而无需用户了解它。虽然这只是一个需要内部解决方案的相对简单的问题,但未来可能会出现系统,环境或用户特定的问题,因此最好早点适应它。
这些是遇到的烦人的错误,但是当你找到它们时真的很容易解决。这几乎就像忘记一个支架;很容易被忽视,一个[审查]跟踪,当你找到它时你会打自己,或者至少,这是我的经验; - )
https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html