将'隐形'字符改为字母,刽子手游戏

时间:2013-03-28 21:39:11

标签: java methods character hidden

所以在我的程序中我试图将几个不同的类别合并到我的主程序中,我想出了代码。

我得到了什么

    Dictionary() {
        dictionary = new String[NUMBER_OF_WORDS];
        Scanner inputStream = null;
        try {
            inputStream = new Scanner(new File(FILE_NAME));
        }catch (FileNotFoundException e) {
            System.out.println("Dictionary class cannot find file \"dictionaryData.txt\".");
            System.out.println("Please make sure that the file is in the project folder.");
            System.exit(0);
        }
        for (int i = 0; i < NUMBER_OF_WORDS; i++) {
            dictionary[i] = inputStream.next();
        }
    }

    public String getRandomWord(){
        Random generator = new Random();
        String temp = new String();
        temp += dictionary[generator.nextInt(NUMBER_OF_WORDS)];
        return temp;
    }

    public boolean find(String word) {
        int count = 0;
        int lowerIndex = 0;
        int upperIndex = NUMBER_OF_WORDS - 1;
        int middleIndex;
        while(lowerIndex <= upperIndex){
            middleIndex = (lowerIndex + upperIndex) / 2;
            count++;
            if(word.equalsIgnoreCase(dictionary[middleIndex])) { // found it
                return true;
            }
            else if (word.compareToIgnoreCase(dictionary[middleIndex]) < 0) { // word smaller than middle
                upperIndex = middleIndex - 1;
            }
            else { // word is larger than middle
                lowerIndex = middleIndex + 1;
            }
        }
        return false;
    }
}

以及另一个WordHider类

     WordHider() {
        secretWord = new String();
        wordMask = new String();
    }

    public String getWordMask() {
        return wordMask;
    }

    public String getSecretWord() {
        return secretWord;
    }

    public void setSecretWord(String newSecretWord) {
        secretWord = newSecretWord.toLowerCase();
        if (secretWord.length() > 0) {
            wordMask = HIDE_CHAR;
            for (int i = 1; i < secretWord.length(); i++) {
                wordMask += HIDE_CHAR;
            }
        }
    }

    public boolean isHiddenWordFound() {
        for (int i = 0; i < wordMask.length(); i++) {
            if(wordMask.charAt(i) == HIDE_CHAR.charAt(0)) {
                return false;
            }
        }
        return true;
    }
    public int revealLetter(String letter) {
        int count = 0;
        String newFoundWord = "";
        if (letter.length() == 1) {
            for (int i = 0; i < secretWord.length(); i++) {
                if ((secretWord.charAt(i) == letter.charAt(0))
                        && (wordMask.charAt(i) == HIDE_CHAR.charAt(0))) {
                    count++;
                    newFoundWord += letter;
                }
                else {
                    newFoundWord += wordMask.charAt(i);
                }
            }
        }
        wordMask = newFoundWord;
        return count;
    }
}

并使用这些类我必须提出如下代码:

Word: **********   Guesses Left: 5
Enter your guess: a
Miss!
Word: **********   Guesses Left: 4
Enter your guess: e
Miss!
Word: **********   Guesses Left: 3
Enter your guess: i
Word: i**i*i****   Guesses Left: 3
Enter your guess: o
Word: i**i*i*o**   Guesses Left: 3

我有几个问题,

  

1)我有一个给出并且必须实现的dictionaryData.text   进入我的代码。它包含一个81,000字的列表,我不是   确定如何让我的程序认识到它。字典类   找不到文件“dictionaryData.txt”。请确保该文件   在项目文件夹中。我尝试打印时遇到错误   随机单词

     

2)如何让我的程序将单词的字母更改为   星星(隐藏这个词)

     

3)把它全部放在一个循环中?

1 个答案:

答案 0 :(得分:0)

缺少Dictionary类和WordHider类的部分内容。不过,我会尽力回答你的问题。

1)就像我说的那样,你错过了Dictionary类的一部分。你合并这样的类:

Dictionary dictionary = new Dictionary();
String word = dictionary.getRandomWord();

2)像这样:

wordHider.setSecretWord(word);

3)我不确定“它”是什么,但是,你的用户必须猜一封信。然后你必须检查字母是否在单词中。像这样:

wordHider.revealLetter(letter);

然后你必须显示单词并让用户猜出另一个字母。这个猜测/检查/显示必须处于循环中。