Hangman检查单词中是否包含String并替换它

Hangman检查单词中是否包含String并替换它

本文介绍了Hangman检查单词中是否包含String并替换它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中创建一个刽子手游戏。我不知道如何检查和更换它。一切正常,String字是正确的,游戏板很好。所以游戏板给我的单词长度为_ _ _ _ _,例如。

I create a hangman game in java. I have no clue how to check and replace it. Everything works, the String word is correct and the gameboard is fine. So the game board give me the length of word as "_ _ _ _ _", for example.

我的问题就是如何才能获得用户输入检查字符串字的位置并转到战斗板并用位置中找到的单词更改下划线(_)。

public void gameStart(int topic) {
    String[] wordList = this.wordList.chooseTopicArray(topic);
    String word = this.wordList.pickRandom(wordList);
    String gameboard = spielbrettvorbereiten(word);
    Scanner userInput = new Scanner(System.in);

    for (int i = 0; i <= 16;) {

    System.out.println(gameboard);
    System.out.print("Write a letter ");

    String input = userInput.next();
    int length = input.length();
    boolean isTrue = letters.errateWortEingabe(length);

    if (isTrue == true) {
    if (word.contains(input)) {

    }


    } else {
       i = i - 1;
    }


    i++;
    }

我希望你们能帮助我,我很努力。

I hope you guys can help me I am struggeling so hard.

最好的问候
MichaelDev

Best RegardsMichaelDev

推荐答案

有几种方法可以实现刽子手。我将向您展示一种易于理解的方法,不关注效率。

There are several ways to implement hangman. I will show you an approach that is easy to understand, no focus on efficiency.

您需要知道最后的单词并记住用户猜到的所有字符:

You need to know the final word and remember all characters that the user has guessed:

final String word = ... // the random word
final Set<Character> correctChars = new HashSet<>();
final Set<Character> incorrectChars = new HashSet<>();

现在,如果用户猜到一个字符,你应该更新数据结构:

Now if a user guesses a character you should update the data-structures:

final char userGuess = ... // input from the user
if (correctChars.contains(userGuess) || incorrectChars.contains(userGuess) {
    System.out.println("You guessed that already!");
} else if (word.contains(userGuess)) {
    correctChars.add(userGuess);
    System.out.println("Correct!");
} else {
    incorrectChars.add(userGuess);
    System.out.println("Incorrect!");
}

最后你需要打印单词为 _ _的东西_ _ 等等。我们通过替换 correctChars 中包含的所有字符来实现:

And last you need something that prints the word as _ _ _ _ and so on. We do so by replacing all characters that are not contained in correctChars:

String replacePattern = "(?i)[^";
for (Character correctChar : correctChars) {
    replacePattern += correctChar;
}
replacePattern += "]";

final String wordToDisplay = word.replaceAll(replacePattern, "_");
System.out.println("Progress: " + wordToDisplay);

replacePattern 可能看起来像(?I)[^ aekqw] (?i)匹配不区分大小写, [...] 是一组要匹配的符号, ^ 否定该组。因此,所有未包含在 [...] 内的字符都会被替换。

The replacePattern may then look like (?i)[^aekqw]. The (?i) matches case insensitive, the [...] is a group of symbols to match and the ^ negates the group. So all characters that are not contained inside the [...] get replaced.

并检查是否小游戏结束:

And a small check if the game has finished:

if (wordToDisplay.equals(word)) {
    System.out.println("You won!");
} else if (incorrectChars.size() > 10) {
    System.out.println("You guessed wrong 10 times, you lost!");
} else {
    ... // Next round starts
}

这篇关于Hangman检查单词中是否包含String并替换它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:48