无法实现java hangman以使用正确的字母返回空格(例如" _ _ r _")

时间:2015-06-02 01:32:15

标签: java bluej

我一直在Java BlueJ的一个基于文本的刽子手游戏中为一个学校项目工作并且大部分时间都取得了成功,但是我遇到了这个部分的问题。

我试图这样做,所以每当玩家正确猜出一个字母时,它会打印出插入了正确字母的空格(星号)。现在,我打印出你猜到的字母,并用正确数量的空白过去正确的猜测。 我现在唯一的问题是它在游戏开始时打印的空白数量不正确。

如何使用空格修复此问题?

import java.util.*;

public class Hangman {
    Word w = new Word();
    private String word = w.chooseWord();
    private int count = 0;
    String guess;

public String getWord() {
    String w = word;
    return w;
}

public int countLetters() {
    for (int i = 0; i < word.length(); i++) {
        if (Character.isLetter(word.charAt(i))) {
            count++;
        }
    }
    return count;
}

public String Blanks() {
    countLetters();
    int num = 0;
    String spaces = "";
    String blankSpace = "*";
    while (num < count) {
        spaces = spaces + blankSpace;
        num++;
    }
    return spaces;
}

String wordSoFar = Blanks();

public int countOccurrences() {
    int count = 0;
    for (int i = 0; i < word.length(); i++) {
        if (word.substring(i, i + 1).equals(guess)) {
            count++;
        }
    }
    return count;
}

public String wordSoFar() {
    for (int i = 0; i < word.length(); i++) {
        if (word.substring(i, i + 1).equals(guess)) {
            wordSoFar = wordSoFar.substring(0, i) + guess + wordSoFar.substring(i + 1 , wordSoFar.length());
        }
    }
    return wordSoFar;
}  

public void Guess() {
    //Removed code that draws hangman due to it making this really long

    boolean correct = false;
    Scanner scan = new Scanner(System.in);
    int numIncorrectGuesses = 0;
    int numCorrectGuesses = 0;

    while (numCorrectGuesses != word.length() && numIncorrectGuesses < 6) {  
        guess = scan.next().substring(0,1);

        if (word.contains(guess)) {
            correct = true;
            numCorrectGuesses += countOccurrences();
            System.out.println(wordSoFar());
        } else {
            correct = false;
            numIncorrectGuesses++;
        }

    //Removed code that draws hangman due to it making this really long

    if (numCorrectGuesses == word.length()) {
        System.out.println("You win");
    } else {
        System.out.println("You lose");
    }
}

添加驱动程序类:

public class runGame {
    public static void main(String[]args) {
        Hangman game = new Hangman();
        System.out.println(game.getWord());
        System.out.println(game.Blanks());
        game.Guess();
    }
}

选择单词的类:

import java.util.*;

    public class Word {
        //String[] bank = {removing word bank due to length};

        public String chooseWord() {
            Random r = new Random();
            return new String(bank[r.nextInt(bank.length)]);
        }
    }

以下是发生疯狂的一个例子(第一行仅用于测试;最终游戏不会显示单词。单个字符行是我的猜测):

object
************
    _______
   |/      |
   |
   |
   |
   |
   |
___|___
o
o*****
b
ob****
j
obj***
e
obje**
c
objec*
t
object
You win

2 个答案:

答案 0 :(得分:2)

你必须删除该行

wordSoFar = Blanks();

从wordSoFar()函数开始。而是在guess()函数的开头做它。每当你猜出一个新角色时,你都会用空白初始化它。每次调用Blank()函数时,它都会增加计数(因为它是一个类变量),最终会增加&#34; _&#34;的数量。

现在针对你当前的双*问题,在Hangman类中为wordSoFar写一个getter。

public String getWordSoFar() {
    return wordSoFar
}

主要功能

public class runGame {
    public static void main(String[]args) {
        Hangman game = new Hangman();
        System.out.println(game.getWord());
        System.out.println(game.getWordSoFar());
        game.Guess();
    }
}

另一个人认为,countLetter()函数应该在计数之前将计数初始化为0.

public int countLetters() {
    int count = 0;
    for (int i = 0; i < word.length(); i++) {
        if (Character.isLetter(word.charAt(i))) {
            count++;
        }
    }
    return count;
}

答案 1 :(得分:0)

您可以将其转换为字符数组

,而不是使用字符串
char[] wordArray = word.toCharArray();
char[] blankArray = new char[wordArray.length];
Arrays.fill(blankArray,'_');

猜猜应该是一个角色,因为用户应该只能猜一个字母

然后为此

public String wordSoFar(){
    for(int i = 0; i < wordArray.length(); i++){
        if(wordArray[i] == guess){
            blankArray[i] = wordArray[i];
        }
    }
    return blankArray;
}

blankArray继续拼凑在一起。您可以测试它是否通过

解决
if(String(blankArray)==String(wordArray)){}