【问题标题】:How to decrement the amount of tries in a Hangman game only when guessed incorrectly?仅在猜错时如何减少刽子手游戏中的尝试次数?
【发布时间】:2019-03-29 17:46:51
【问题描述】:

我在 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;
}


}

猜对一个字母后:

(示例) 预期结果 =

目前的猜测:
_ _ _ _ 一个 _ 一个 _
您还有 10 次尝试。
输入单个字符

实际结果=

目前的猜测:
_ _ _ _ 一个 _ 一个 _
您还剩 9 次尝试。
输入单个字符

【问题讨论】:

  • 指定该程序应接受的输入和该程序应正确打印的输出。

标签: java


【解决方案1】:

看看你的这部分代码

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

【讨论】:

  • 您现在只计算成功的答案。与 OP 想要的相反。
  • @Tom 哦,我的错。在这里用变量混淆游戏,通常人们会想到在此类游戏中减少numberOfChances
  • 非常感谢! :) 我尝试过这里是什么,但它没有用,所以我对其进行了一些更改,现在它可以完美运行了!谢谢
  • @NoSwearsk 请分享你的所作所为。
【解决方案2】:

您需要记住在循环中找到字符匹配的时间:

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

如果没有匹配,则只增加tries

if (!foundACharacter)
    tries++;

【讨论】:

    猜你喜欢
    • 2017-03-26
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多