如何在 Java 中限制 Hangman 中的猜测次数
How to limit the number of guesses in Hangman in Java
所以我的代码运行良好。我只需要帮助限制猜测的次数。每次你猜错时,都有一个计数器告诉你还剩多少次猜测。
所以我想将猜测的限制设置为 15。所以每次你做出错误的猜测计数器都会向用户显示(增加 pressure/stress)剩下 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 次尝试并显示尝试次数?
提前致谢。
请注意,我的建议不是一个优雅的解决方案。
- 创建一个带有值(例如 15)的静态最终 maxGuesses。
- 将 while-condition 从
!word.equals(hiddenWord)
更改为 true
。这个循环现在永远不会终止 - 除非我们引入 break
statements.
- 创建一个新检查 (if-condition) 以查看是否超过了尝试次数(例如
missCount>maxGuesses
)。在 if-block 中打印此信息,并添加 break
以退出内部 while。
- 在
word.equals(hiddenWord)
中创建另一个检查 (if-condition) - 这用于确定是否找到了该词。将找到的单词的信息打印为 if-block 的一部分,并添加 break
语句
- 两项检查都应该是内部 while-loop.
的一部分
.
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;
}
}
所以我的代码运行良好。我只需要帮助限制猜测的次数。每次你猜错时,都有一个计数器告诉你还剩多少次猜测。
所以我想将猜测的限制设置为 15。所以每次你做出错误的猜测计数器都会向用户显示(增加 pressure/stress)剩下 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 次尝试并显示尝试次数?
提前致谢。
请注意,我的建议不是一个优雅的解决方案。
- 创建一个带有值(例如 15)的静态最终 maxGuesses。
- 将 while-condition 从
!word.equals(hiddenWord)
更改为true
。这个循环现在永远不会终止 - 除非我们引入break
statements. - 创建一个新检查 (if-condition) 以查看是否超过了尝试次数(例如
missCount>maxGuesses
)。在 if-block 中打印此信息,并添加break
以退出内部 while。 - 在
word.equals(hiddenWord)
中创建另一个检查 (if-condition) - 这用于确定是否找到了该词。将找到的单词的信息打印为 if-block 的一部分,并添加break
语句 - 两项检查都应该是内部 while-loop. 的一部分
.
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;
}
}