java - 仅在猜错时如何减少刽子手游戏中的尝试次数?
问题描述
我在 Youtube 的指导下用 Java 开发了一个 Hangman 游戏,即使玩家猜对了单词,尝试次数也会减少。只有当玩家猜错时,我应该在我的代码中添加什么来减少尝试次数?
我曾尝试使用布尔 letterIsGuessed,但我不能将它放在一个循环中,它会检查正确的字母,因为它还会检查字母不在的位置,并且仍然会输出一个错误值。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package project_hangman;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author NoSwear
*/
public class Project_Hangman {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[] guesses = {"yoko", "michael", "slovakia", "shimura ken", "yuuta", "sunshine", "shiritori", "yokohama", "kyoto", "programming", "smartphone", "shinzo abe", "katakana", "kaomoji", "iron man", "shogi", "anime", "kendo", "kyudo", "kenjutsu"};
boolean Playing = true; // Separates the game over and the game itself
while (Playing) {
System.out.println("Welcome to the Hangman game!");
System.out.println("Developed by NoSwear");
char[] randomWordGuess = guesses[random.nextInt(guesses.length)].toCharArray(); // Takes a random position of the word in the "guesses" field and grabs it, turns them into char
int amountOfGuesses = 10;
char[] playerGuess = new char[amountOfGuesses];
for (int i = 0; i < playerGuess.length; i++) {
playerGuess[i] = '_';
}
boolean wordIsGuessed = false; // Whole word guessed
int tries = 0; // Goes into println, informs the player about the amount of guesses he/she has made
while (!wordIsGuessed && tries != amountOfGuesses) {
System.out.println("Current guesses : ");
printArray(playerGuess);
System.out.printf("You have %d tries left.\n", amountOfGuesses - tries); // See : String conversion
System.out.println("Enter a single character");
char input = scanner.nextLine().charAt(0); // Takes only the first letter in the whole sentence the user will input
if (input == '-') {
Playing = false;
wordIsGuessed = true;
System.out.println("Thank you for playing");
} else {
for (int i = 0; i < randomWordGuess.length; i++) {
if (randomWordGuess[i] == input) {
playerGuess[i] = input;
}
}
tries++;
if (isTheWordGuessed(playerGuess)) {
wordIsGuessed = true;
System.out.println("Congratulations, you won the game!");
}
}
}
if (!wordIsGuessed) System.out.println("You ran out of guesses :(");
System.out.println("Do you want to play another game? (yes/no)");
String anotherGame = scanner.nextLine();
if (anotherGame.equals("no")) Playing = false;
}
System.out.println("Game over");
}
public static void printArray(char[] array) { // Prints the current state of the word guess
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
public static boolean isTheWordGuessed(char[] array) { // Checks if all empty spaces are filled
for (int i = 0; i < array.length; i++) {
if (array[i] == '_') return false;
}
return true;
}
}
猜出正确的字母后:
(示例)预期结果 =
当前猜测:
_ _ _ _ a _ a _
你还有 10 次尝试。
输入单个字符
实际结果 =
当前猜测:
_ _ _ _ a _ a _
您还剩 9 次尝试。
输入单个字符
解决方案
看看你的这部分代码
} else {
for (int i = 0; i < randomWordGuess.length; i++) {
if (randomWordGuess[i] == input) {
playerGuess[i] = input;
}
}
tries++;
您需要添加一个条件,tries
但每次迭代都会增加tries
。
在 if 语句中声明boolean isMatch=false;
并使用这个变量来知道你的控件有一个正匹配
} else {
isMatch = false;//-------------------------------------------
for (int i = 0; i < randomWordGuess.length; i++) {
if (randomWordGuess[i] == input) {
isMatch=true;//--------------------------------------
playerGuess[i] = input;
}
}
if(!isMatch)//------------------------------------------------
tries++;
还要确保在主循环的每次迭代之后或之前分配isMatch
一个值。false
推荐阅读
- python - Python for 循环乘法列表
- java - 在 Service 类的构造函数中使用 JPA 存储库访问
- python - Python heapq.heappop() 与 pop(0)
- javascript - TypeError 错误:无法读取未定义的属性“过滤器”
- amazon-web-services - 我应该为 Amazon Keyspaces(托管 Cassandra)使用哪个用户名和密码?
- android - 片段中未显示操作栏图标
- javascript - 对象作为 React 子对象无效(发现:对象与键 {$$typeof、render、propTypes、displayName、post、Naked、options、useStyles})
- python - Python多处理释放共享数组使用的内存
- angular - 如何在 mat-select 中设置模型?
- android - 如何从外部更新可组合状态?