-1

我正在编写一个 Java 刽子手游戏,到目前为止,我已经获得了一次替换一个的代码。我需要的是每次都替换,直到猜到单词为止。这是一次一个的工作方式:

        for(int i = 0; i < GuessWord.length(); i++) {
        foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");
        }
            GuessedLetters = xletters.toString().toUpperCase();
            WordLabel.setText(foundword.toUpperCase());
            GuessedLabel.setText(GuessedLetters);
            GuessText.setText(null);
            GuessText.requestFocusInWindow();

这是我的另一次尝试,但效果不佳:

       //replace underscores with letters as they are guessed while the word is not solved
        do {
        foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");
        }
        while (!SetMain.equals(GuessWord));
            //set results to labels
            WordLabel.setText(foundword.toUpperCase());
            GuessedLabel.setText(GuessedLetters);
            GuessText.setText(null);
            GuessText.requestFocusInWindow();

这是我为需要它的人提供的完整代码:

    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.JOptionPane;

    public class MainFrame extends javax.swing.JFrame {

        public MainFrame() {
            initComponents();
        }
    static String SecretWord = "";
    double Result = 0;
    StringBuilder mainword = new StringBuilder();
     String[] words = {"technology", "computer", "camera", "graphic design", "digital", "media", "technician", "photography", "troubleshoot", "pixels", "application", "download"};
     Random r = new Random();
    int randvalue = r.nextInt(11);
    String GuessWord = words[randvalue];
    int errors = 0;

        private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

        for(int i = 0; i < GuessWord.length(); i++) { 
           mainword.append("_ ");
        }
           mainword.append(SecretWord);
           String SetMain = mainword.toString();
           WordLabel.setText(SetMain);
           GuessButton.setEnabled(true);
           GoButton.setEnabled(false); 
        }                                        

        private void GuessButtonActionPerformed(java.awt.event.ActionEvent evt){                                            
        String strGuess = GuessText.getText(); //user input
        StringBuilder xletters = new StringBuilder(strGuess); // letters guessed
        String GuessedLetters = null;
        String foundword = null;

        //replace underscores with letters as they are guessed
        for(int i = 0; i < GuessWord.length(); i++) {
        foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");

        }
            //set to labels to display results
            GuessedLetters = xletters.toString().toUpperCase();
            WordLabel.setText(foundword.toUpperCase());
            GuessedLabel.setText(GuessedLetters);
            GuessText.setText(null);
            GuessText.requestFocusInWindow();

所以我希望代码能够累积替换字母,即如果这个词是你好:

密语:_ _ _ _ _ 猜到 e....

密语:_e _ _ _ 猜到 o....

密语:_e _ _ o 猜到了……

密语:_ e _ _ o 猜到 h....

Seret Word: he _ _ o guessed l....

密语:你好,恭喜!

我需要它在 Netbeans IDE 7.2 中工作,它必须适用于 JLayeredPane,而不是 System.out.print 方法。谢谢!

4

1 回答 1

0

我不确定这是否完全是你所追求的,但它可能会给你一些想法......

public class TestHangMan {

    public static void main(String[] args) {
        new TestHangMan();
    }

    public TestHangMan() {
        String word = "Testing";
        String mask = "_______";

        mask = checkGuess(word, mask, 'a');
        System.out.println(mask);
        mask = checkGuess(word, mask, 't');
        System.out.println(mask);
        mask = checkGuess(word, mask, 'e');
        System.out.println(mask);
        mask = checkGuess(word, mask, 's');
        System.out.println(mask);
        mask = checkGuess(word, mask, 'g');
        System.out.println(mask);
        mask = checkGuess(word, mask, 'n');
        System.out.println(mask);
        mask = checkGuess(word, mask, 'i');
        System.out.println(mask);
    }

    protected String checkGuess(String word, String mask, char guess) {
        guess = Character.toLowerCase(guess);
        StringBuilder sb = new StringBuilder(mask);
        for(int index = 0; index < word.length(); index++) {
            if (Character.toLowerCase(word.charAt(index)) == guess) {
                sb.setCharAt(index, word.charAt(index));
            }
        }
        return sb.toString();
    }

}

哪个产生...

_______
T__t___
Te_t___
Test___
Test__g
Test_ng
Testing

您可以检查掩码以查看用户是否已经做出了猜测,只需使用mask.toLowerCase().contains(stringGuess.toLowerCase())

希望能帮助到你。

于 2012-11-07T23:21:59.450 回答