java - 如何仅在猜错时减少刽子手游戏中的尝试次数?

标签 java

我在 Youtube 的指导下用 Java 开发了一个 Hangman 游戏,即使玩家猜对了单词,尝试次数也会减少。我应该在代码中添加什么,以仅在玩家猜错时减少尝试次数?

我尝试使用 boolean 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;
}


}

猜出正确的字母后:

(示例) 预期结果=

Current guesses :
_ _ _ _ a _ a _
You have 10 tries left.
Enter a single character

实际结果=

Current guesses :
_ _ _ _ a _ a _
You have 9 tries left.
Enter a single character

最佳答案

看看这部分代码

        } else {
            for (int i = 0; i < randomWordGuess.length; i++) {
                if (randomWordGuess[i] == input) {
                    playerGuess[i] = input;
                } 
            }

            tries++;  

您需要向tries添加一个条件但是您在每次迭代中都会增加tries

声明 boolean isMatch=false; 并在 if 语句中使用此变量来了解您的控件具有正匹配

        } 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 值。

关于java - 如何仅在猜错时减少刽子手游戏中的尝试次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55422978/

相关文章:

java - IntelliJ 中自动生成的 JAXB stub 的导入无法识别

java - 从另一个 JButton 获取 JButton

java - 销毁 Spring 中的前一个 session

java - JSF 2 使用 @ManagedProperty 注入(inject) Spring bean/service 而没有 xml

java - 设置一个 Fiveton 类,以供不同线程以循环方式访问

java - 如何重用 Spring Data JpaRepository 接口(interface)

Java 正则表达式 : need one regex to match all the formats specified

javascript - 如何在云函数中从 Firebase 数据库检索多个值?

java - 如何保持 "Hello World"类型服务器运行

java - 我的自定义控件应该依赖于 Controller 类还是接口(interface)?