如何限制Java中Hangman的猜测次数

时间:2018-03-24 20:58:56

标签: java exception-handling counter

所以我的代码工作得很好。我只需要帮助来限制猜测的数量。每当你做出错误的猜测时,都要有一个计数器来告诉你你剩下多少猜测。

所以我想把猜测的极限设置为15.所以每次你做错了一个坏的猜测计数器向用户显示(增加压力/压力)14猜测还是你输了。

这是我的代码:

    import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Hangman {
    private final static int maxGuesses = 5;


    static ArrayList<String> words = new ArrayList<>();

        static boolean isCorrect;

        public static void main(String[] args) {

            // getting file
            File filename = new File("hangman.txt");
            if (!filename.exists()) {
                System.out.println(filename.getAbsolutePath());
                System.out.println(filename + " does not exist.");
                System.exit(1);
            }
            // reading word file
            try {
                Scanner input = new Scanner(filename);
                while (input.hasNext()) {
                    words.add(input.next());
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
            // debug: display words
            //System.out.println(words);

            Scanner input = new Scanner(System.in);
            String playStats = "y";
            while (playStats.equals("y")) {
                String word = getWord();
                String hiddenWord = getHiddenWord(word);
                int missCount = 0;


                    while (true) {
                    System.out.print("(Guess) Enter a letter in word " + hiddenWord + " > ");
                    char ch = input.next().charAt(0);

                    if (!isAlreadyInWord(hiddenWord, ch)) {

                        hiddenWord = getGuess(word, hiddenWord, ch);
                        if (missCount>maxGuesses){
                           // Print info on max guesses reached
                         System.out.println("you have reached" + missCount + "you have" + maxGuesses + 
                                 " left");
                           break;
                          }
                        if(word.equals(hiddenWord)) {

                        if (!isCorrect) {
                            System.out.println(ch + " is not in the word.");
                            missCount++;
                        }   
                    else {
                        System.out.println(ch + " is already in word.");
                    }
                        break;
                        } 
                        }
                    }
                System.out.println("The word is " + hiddenWord + " You missed " + missCount + " times");
                System.out.println("Do you want to guess another word? Enter y or n >");
                playStats = input.next();
            }

        }

        public static String getWord() {
            return words.get((int) (Math.random() * words.size()));
        }

        public static String getHiddenWord(String word) {

            String hidden = "";
            for (int i = 0; i < word.length(); i++) {
                hidden += "*";
            }
            return hidden;
        }

        static public String getGuess(String word, String hiddenWord, char ch) {

            isCorrect = false;
            StringBuilder s = new StringBuilder(hiddenWord);
            for (int i = 0; i < word.length(); i++) {

                if (ch == word.charAt(i) && s.charAt(i) == '*') {
                    isCorrect = true;
                    s = s.deleteCharAt(i);
                    s = s.insert(i, ch);
                }
            }
            return s.toString();
        }

        public static boolean isAlreadyInWord(String hiddenWord, char ch) {

            for (int i = 0; i < hiddenWord.length(); i++) {

                if (ch == hiddenWord.charAt(i)) {
                    return true;
                }
            }
            return false;
        }
    }

我认为它与我的“missCount”作为计数器有关......但我怎么能在那里写下missCount不能超过15次尝试并显示尝试次数?

提前致谢。

1 个答案:

答案 0 :(得分:1)

请注意,我的建议不是一个优雅的解决方案。

  • 使用值(例如15)创建静态最终maxGuesses。
  • 将时间条件从!word.equals(hiddenWord)更改为true。这个循环现在永远不会终止 - 除非我们引入break语句。
  • 创建新的检查(if-condition)以查看是否超过了尝试次数(例如missCount>maxGuesses)。在if块中打印此信息,并添加break以退出内部while。
  • 使用word.equals(hiddenWord)创建另一个检查(if-condition) - 这用于确定是否找到了该单词。打印有关找到的单词的信息作为if-block的一部分,并添加break语句
  • 两个检查都应该是内部while循环的一部分。

while (true) { 
  if (missedCount>maxGuesses){
   // Print info on max guesses reached
   break;
  }
  // Does the read input stuff
  if (word.equals(hiddenWord)){
   // Print info on word was guessed
   break;
  }
}