如何创建基于Java文本的Hangman游戏等问题

时间:2014-05-22 06:10:06

标签: java

我正在用Java创建一个Hangman游戏,它几乎完美无缺。所以我有两个问题。第一个是:

当用户输入一个字母并且该单词有重复的字母时,如何让它打印这两个字母的实例。

  • 我创建了一个while循环但是这个循环直到下一次之后才输出Modified字。如果这有道理? 第二个问题: 我需要能够阻止用户两次输入相同的字母
  • 我尝试过列表,数组和哈希集。各种各样,但似乎没有。

我的代码如下:

可能有其他线程有相同的问题,但似乎没有任何帮助,因为我无法将其实现到此人的代码中。

import java.util.Scanner;
import java.util.Arrays;

public class Hangman{

public static void main(String []args){
Scanner Input = new Scanner(System.in);

    String[] CollectionOfWords = {"","gravity","banana","gate","processor","momentum","earth","star","light","television","pan","cupboard"};

    int radmNumber = (int) Math.ceil (Math.random() * CollectionOfWords.length);
    int counter = 10;
    String radmWord = CollectionOfWords[radmNumber];

    char[] genRadmLetter = radmWord.toCharArray();
    char[] genRadmLetter2 = radmWord.toCharArray();
    for (int x = 0; x<genRadmLetter.length; x++){
        genRadmLetter[x]='?';
    }

    System.out.println(String.valueOf(genRadmLetter));
    System.out.println("Hello. Guess a letter.");
    char guessedLetter = Input.next().charAt(0);
    int RW = radmWord.indexOf(guessedLetter);

    if (RW >= 0 ){

    genRadmLetter[RW] = guessedLetter;
    System.out.println(genRadmLetter);

    }

    if (RW == -1){
        System.out.println("Wrong letter, try again.");
        counter = counter - 1; 
        System.out.println("Lives left: " + counter);
    }

    while (counter != 0) {

        System.out.println("Guess a letter.");
        guessedLetter = Input.next().charAt(0);
        RW = radmWord.indexOf(guessedLetter);


        if (RW >= 0 ){

        genRadmLetter[RW] = guessedLetter;
        System.out.println(genRadmLetter);
    }

        if (RW == -1){
            System.out.println("Wrong letter, try again.");
            counter = counter - 1; 
            System.out.println("Lives left: " + counter);
    } else {
            System.out.println(genRadmLetter);
            while (RW >= 0 ){
                genRadmLetter[RW] = guessedLetter;
                RW = radmWord.indexOf(guessedLetter, RW+1); 
            }
    }

        boolean result = Arrays.equals(genRadmLetter, genRadmLetter2);
        if (result == true){
            break;
        }

        if (counter == 0){
            break;
        }

    }

    if (counter == 0){
        System.out.println("You lose. The word was: " + radmWord);
    }

    else {
        System.out.println("Well done, you have guessed the word.");
        System.out.println("Your final score is: " + counter);
    }

}

}

3 个答案:

答案 0 :(得分:1)

而不是使用......

int RW = radmWord.indexOf(guessedLetter);

要确定输入的值是否与仅返回第一个索引的字符匹配,您应该使用某种循环来检查每个字符

boolean found = false;
for (int rw = 0; rw < genRadmLetter2.length; rw++) {

    if (genRadmLetter2[rw] == guessedLetter) {

        genRadmLetter[rw] = guessedLetter;
        found = true;

    }

}

现在,因为您依靠RW的值来确定是否找到了匹配项,所以我对其进行了更改,以便可以使用boolean found标记,例如......

if (!found) {
    System.out.println("Wrong letter, try again.");
    counter = counter - 1;
    System.out.println("Lives left: " + counter);
}

您还有一些重复的代码集,可以简化为单个do-while循环,这样可以更容易阅读和更改,例如......

do {

    //...

} while (counter != 0);

对于你的第二个问题,某种Set将是最简单的解决方案......

Set<Character> guesses = new HashSet<Character>();
//...
char guessedLetter = Input.next().charAt(0);

if (guesses.contains(guessedLetter)) {

    System.out.println("You've used this guess, guess again");

} else {

    guesses.add(guessedLetter);

例如......

因为翻译代码片段并不总是那么容易......这是我的测试代码......

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Hangman {

    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);

        String[] CollectionOfWords = {"", "gravity", "banana", "gate", "processor", "momentum", "earth", "star", "light", "television", "pan", "cupboard"};

        int radmNumber = (int) Math.ceil(Math.random() * CollectionOfWords.length);
        int counter = 10;
        String radmWord = "banana"; //CollectionOfWords[radmNumber];

        char[] genRadmLetter = radmWord.toCharArray();
        char[] genRadmLetter2 = radmWord.toCharArray();
        for (int x = 0; x < genRadmLetter.length; x++) {
            genRadmLetter[x] = '?';
        }

        Set<Character> guesses = new HashSet<Character>();
        do {

            System.out.println("Guess a letter.");

            System.out.println(String.valueOf(genRadmLetter));
            System.out.println("Hello. Guess a letter.");
            char guessedLetter = Input.next().charAt(0);

            if (guesses.contains(guessedLetter)) {

                System.out.println("You've used this guess, guess again");

            } else {

                guesses.add(guessedLetter);
                boolean found = false;
                for (int rw = 0; rw < genRadmLetter2.length; rw++) {

                    if (genRadmLetter2[rw] == guessedLetter) {

                        genRadmLetter[rw] = guessedLetter;
                        found = true;

                    }

                }

                if (!found) {
                    System.out.println("Wrong letter, try again.");
                    counter = counter - 1;
                    System.out.println("Lives left: " + counter);
                }

            }

            boolean result = Arrays.equals(genRadmLetter, genRadmLetter2);
            if (result == true) {
                break;
            }

            if (counter == 0) {
                break;
            }

        } while (counter != 0);

        if (counter == 0) {
            System.out.println("You lose. The word was: " + radmWord);
        } else {
            System.out.println("Well done, you have guessed the word.");
            System.out.println("Your final score is: " + counter);
        }

    }

}

答案 1 :(得分:0)

代码存在多个问题:

  1. 长度与最大元素数的典型初学者问题
  2. 不必要的重复代码
  3. 输出的逻辑问题
  4. 至于1.:

    你正在使用这个:

    int radmNumber = (int) Math.ceil (Math.random() * CollectionOfWords.length)
    

    如果您使用

    int radmNumber = (int) Math.ceil (Math.random() * CollectionOfWords.length-1)
    

    你可以在没有空字符串的情况下启动arrey并且它不会随机崩溃

    到2。

    如果你使用它,你不需要复制输入代码:

    System.out.println(String.valueOf(genRadmLetter));
    System.out.print("Hello.");
    char guessedLetter;
    int RW; 
    while (counter != 0)
    {
        System.out.println("Guess a letter.");
    ...
    

    最后3.(你的主要问题)

    在更改之前执行输出。所以这解决了你的问题:

    ... 
    else
    {
      while (RW >= 0)
      {
          genRadmLetter[RW] = guessedLetter;
          RW = radmWord.indexOf(guessedLetter, RW + 1);
      }
      System.out.println(genRadmLetter);
    }
    

    所以只需将输出移到while。

答案 2 :(得分:0)

我做了一个可以帮助你的小帮助班......

static class GuessString {
   private char[] mask;
   private String solution;
   private boolean lastGuessResult;
   GuessString(String word) {
      this.solution = word;
      this.mask=word.toCharArray();
      Arrays.fill(mask, '?');   // Build a mask like: ??????    
   }

    public String guess(char guess) {
        char c = Character.toLowerCase(guess); // case insensitive
        int i = solution.indexOf(c); 
        lastGuessResult = i != -1; // -1 means "not found)
        if (lastGuessResult)
        while (i != -1) { // this will loop till c is replaced everywhere.
            mask[i] = c;
            i = solution.indexOf(c, i+1); 
        }
        return new String(mask); // return the updated mask.
    }
    public boolean lastGuessIsRight() {
        return lastGuessResult;
    }
    public String getCurrent() {
        return new String(mask);
    }
    public boolean isSolved() {
        return getCurrent().equals(solution);
    }
}