试图用Java实现基于文本的Hangman游戏 [英] Trying to implement text-based Hangman game in Java

查看:196
本文介绍了试图用Java实现基于文本的Hangman游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查用户输入的字母以及他们猜测的空格是否位于隐藏字的特定位置。

I need to check if the user-input letter, along with the spaces they guess, is at a specific spot on the hidden word.

变量1是等于用户猜测的空间索引..而letterGuess是他们猜到的字母。我的代码怎么错?

the variable one is equal to the space index the user guessed.. and letterGuess is the letter they guessed. How is my code wrong??

示例:秘密字是你好

hidden word is ==  -----

用户猜猜 h 0 1 2 3
因此,需要检查单词 hello 中索引0 1 2 3处的空格是否包含用户猜测h
如果是这样,需要将 ----- 替换为 h ----

user guesses h 0 1 2 3 so, it needs to check if the spaces at indexes 0 1 2 3 at the word hello contain the user guessed "h" if so, need to replace ----- to h---- .

                 String firstSpace = guessedSpaces.substring(0,1);
                String secondSpace = guessedSpaces.substring(2,3);
                String thirdSpace = guessedSpaces.substring(4,5);
                String fourthSpace = guessedSpaces.substring(6,7);

                int one = Integer.parseInt(firstSpace);


                int two = Integer.parseInt(secondSpace);
                int three = Integer.parseInt(thirdSpace);
                int four = Integer.parseInt(fourthSpace);



     (secretWord.charAt(one)==letterGuess)
     (secretWord.charAt(one)==letterGuess)
    (secretWord.charAt(one)==letterGuess)
    (secretWord.charAt(one)==letterGuess)


推荐答案

以下是一个完整的刽子手程序的简短代码:

Here's a brief amount of code that is a complete hangman program:

static int MAX_MISSES = 5;
static String[] WORDS = { "QUICK", "BROWN", "JUMPS" }; // etc

public static void main(String[] args) throws Exception {
    String word = WORDS[new Random().nextInt(WORDS.length)], guesses = " ";
    int misses = -1;
    for (Scanner in = new Scanner(System.in); !word.matches("[" + guesses + "]+") & (misses += word.contains(guesses.substring(0, 1)) ? 0 : 1) <= MAX_MISSES; guesses = in.nextLine().toUpperCase().charAt(0) + guesses)
        System.out.println(word.replaceAll("(?<=.)", " ").replaceAll("[^" + guesses + "]", "_"));
    System.out.println(word + (misses > MAX_MISSES ? " not" : "") + " solved with " + misses + " incorrect guesses");
}

一些注释:


  • 正则表达式用于检查解决方案并显示当前猜测状态

  • & 是在循环终止中使用而不是&& 来强制执行逻辑的两面 - 因此总是正确地更新未命中

  • 已经使用各种其他技巧来故意增加代码密度

  • Regex is used to both check for solution and display the current guess status
  • & is used instead of && in the loop termination to force both sides of the logic to be executed - thus always updating the misses correctly
  • Various other tricks have been used to deliberately increase code density

输入没有错误检查。如果用户输入了多个字母,则仅使用第一个字母。如果没有输入字母,它将会爆炸。可以在不添加更多代码的情况下添加错误检查,但是行的将是一个巨大的 - 我把它作为读者的练习。

There is no error checking for input. If more than one letter is input by the user, only the first is used. If no letters are input, it will explode. Error checking could be added without adding much more code, but the for line would be a whopper - I leave that as an exercise for the reader.

此外,没有检查多次错误地猜测同一个字母。使用这段代码,如果你猜两次正确的字母,没有什么不好的事情发生,但如果你猜两次错误的字母,那就算是一个单独的未命中,所以你将燃烧你的一个机会。

Also, there is no checking for erroneously guessing the same letter multiple times. With this code, if you guess a correct letter twice, nothing bad happens, but if you guess a wrong letter twice, it counts as a separate "miss", so you'll burn one of your chances.

这篇关于试图用Java实现基于文本的Hangman游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆