[英]How to limit the number of guesses in Hangman in Java
So i have my code working great. 所以我的代码运行良好。 I just need help in limiting the number of guesses.
我只需要帮助来限制猜测的数量。 And every time you make a wrong guess, to have a counter to tell you how many guesses you have left.
每次您做出错误的猜测时,都要有一个计数器来告诉您还剩下多少个猜测。
So I want to set the limit of guesses to 15. So every time you make an incorrect a bad guess counter displays to the user to (increase pressure/stress) 14 guesses left, or you lose. 因此,我想将猜测的上限设置为15。因此,每次输入错误时,都会向用户显示一个错误的猜测计数器,以(增加压力/压力)留下14个猜测,否则您将输掉。
Here's my code: 这是我的代码:
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;
}
}
I'm assuming it has something to do with my "missCount" as the counter.... but how can i write in there that the missCount cannot exceed 15 tries and to display the number of tries? 我以为它与我的“ missCount”作为计数器有关系....但是我如何在其中写出missCount不能超过15次尝试并显示尝试次数?
Thanks in advance. 提前致谢。
Be aware that my suggestion is not an elegant solution. 请注意,我的建议不是一个明智的解决方案。
!word.equals(hiddenWord)
to true
. !word.equals(hiddenWord)
更改为true
。 This loop would now never terminate - unless we introduce break
statements. break
语句。 missCount>maxGuesses
). missCount>maxGuesses
)。 Print this information in the if-block and also add the break
to exit the inner while. break
以退出内部while。 word.equals(hiddenWord)
in - this is used to figure out if the word was found. word.equals(hiddenWord)
创建另一个检查(如果条件),用于检查是否找到了该词。 Print the information about the found word as part of the if-block and also add the break
statement break
语句 . 。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.