0

对于我的刽子手游戏,我希望有一堆错误消息来检查诸如输入的多个字母、两次猜测同一个字母等内容。到目前为止,我的完整代码:

    import java.awt.Color;
    import java.awt.Font;
    import java.util.Random;
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import javax.swing.plaf.FontUIResource;

    public class MainFrame extends javax.swing.JFrame {

        public MainFrame() {
            initComponents();
        }
        //declare variables
        static String secretWord = "";
        double result = 0;
        StringBuilder mainWord = new StringBuilder();
        StringBuilder xletters = new StringBuilder(); // letters guessed
        String[] words = {"technology", "computer", "camera", "graphic", "digital", "media", "technician",
            "photography", "troubleshoot", "pixels", "application", "download"};
        Random r = new Random();
        int randValue = r.nextInt(12);
        String guessWord = words[randValue];
        int errors = 0;
        public static int wins = 0, losses = 0;
        String foundWord = null;
        private void GuessButtonActionPerformed(java.awt.event.ActionEvent evt)         {                                            
            String strGuess = GuessText.getText(); //user input           
            String letter = strGuess;
            xletters.append(strGuess.toUpperCase());
            String GuessedLetters = xletters.toString();

            try {
    //replace underscores with letters as they are guessed
                do {
                    for (int i = 0; i < 1; i++) {
                        secretWord = secretWord + letter.charAt(0);
                        foundWord = words[randValue].replaceAll("[^" + secretWord + "]", "_ ");
                        //if user entered more than one letter
                        if (strGuess.length() > 1) {
                            JOptionPane.showMessageDialog(null, "Only one letter at a time!");
                            xletters.append("");
                            GuessedLetters = null;
                            GuessText.setText(null);
                            GuessText.requestFocusInWindow();
                        } //if letter isn't in word
                        else if (guessWord.indexOf(strGuess) == -1) {
                            JOptionPane.showMessageDialog(null, "Sorry, that wasn't in the word.");
                            errors++;
                            if (errors == 1) {
                                Hangman0.setVisible(false);
                            }
                            if (errors == 2) {
                                Hangman1.setVisible(false);
                            }
                            if (errors == 3) {
                                Hangman2.setVisible(false);
                            }
                            if (errors == 4) {
                                Hangman3.setVisible(false);
                            }
                            if (errors == 5) {
                                Hangman4.setVisible(false);
                            }
                            if (errors == 6) {
                                Hangman5.setVisible(false);
                            }
                            if (errors == 7) {
                                Hangman6.setVisible(false);
                            }
                            if (errors == 8) {
                                Hangman7.setVisible(false);
                            }
                            if (errors == 9) {
                                Hangman8.setVisible(false);
                            }
                            if (errors == 10) {
                                Hangman9.setVisible(false);
                                JOptionPane.showMessageDialog(null, "You lost! The word was: " + guessWord);
                                losses++;
                                DirectionsFrame DFrame = new DirectionsFrame();
                                DFrame.setVisible(true);
                                setVisible(false);
                                MainFrame MFrame = new MainFrame();
                                MFrame.dispose();
                                xletters.delete(0, 100);
                                secretWord = "";
                                foundWord = null;
                                strGuess = null;
                                String strLosses = Integer.toString(losses);
                                String strWin = Integer.toString(wins);
                                DirectionsFrame.WinsLabel.setText(strWin);
                                DirectionsFrame.LossesLabel.setText(strLosses);
                            }
                }
            }
                    WordLabel.setText(foundWord.toUpperCase());
                    GuessedLabel.setText(GuessedLetters);
                    GuessText.setText(null);
                    GuessText.requestFocusInWindow();
                } while (foundWord == null);
                if (foundWord.equalsIgnoreCase(guessWord)) {
                    JOptionPane.showMessageDialog(null, "Yay!");
                    wins++;
                    DirectionsFrame DFrame = new DirectionsFrame();
                    DFrame.setVisible(true);
                    setVisible(false);
                    MainFrame MFrame = new MainFrame();
                    MFrame.dispose();
                    xletters.delete(0, 100);
                    secretWord = "";
                    foundWord = null;
                    String strWin = Integer.toString(wins);
                    String strLosses = Integer.toString(losses);
                    DirectionsFrame.WinsLabel.setText(strWin);
                    DirectionsFrame.LossesLabel.setText(strLosses);
                }
            } catch (StringIndexOutOfBoundsException e) {
                JOptionPane.showMessageDialog(null, "Please enter a letter.");
                GuessedLabel.setText(GuessedLetters);
                GuessText.setText(null);
                GuessText.requestFocusInWindow();
            }
        }                                           


        private void GetButtonActionPerformed(java.awt.event.ActionEvent evt) {
            //print out underscores to begin game
            for (int i = 0; i < guessWord.length(); i++) {
                mainWord.append("_ ");
            }
            String SetMain = mainWord.toString();
            mainWord.append(secretWord);
            WordLabel.setText(SetMain);
            GuessButton.setEnabled(true);
            GetButton.setEnabled(false);
        }

我遇到最多的问题是检查用户是否输入了两次相同的字母。所以你只能猜字母表中的一个字母一次。此外,它检查用户是否输入多个字母的代码我相信有一个错误,因为我希望它不将这些字母添加到猜测的字母框中,无论如何它都会添加。(即用户猜测“hf”并且在猜测的字母中为“hf”,它应该什么都没有)

我觉得答案在于方法 indexOf(),但我不完全确定 if 条件会说什么......

我正在使用 Netbeans IDE 7.2 来编译我的代码。请帮忙!谢谢。

4

3 回答 3

1

在您想知道某些东西是否已经被使用过的情况下,aSet就派上用场了。例如,在刽子手游戏中使用的字母,您将拥有Set<String>collection :

Set<String> lettersUsed = new HashSet<String>();

如果您想知道是否已经选择了某些东西:

String inputLetter = "Z";  // test for the letter Z

if (!lettersUsed.contains(inputLetter)) {
    lettersUsed.add(inputLetter);

    // process letter in word
} else {
    // letter has already been used!
}

如果您只想允许一组特定的字母,已经最初设置:

if (lettersAllowed.contains(inputLetter)) {
   lettersAllowed.remove(inputLetter);

   // process letter in word
} else {
   // letter is not allowed (anymore)
}
于 2012-11-10T22:07:17.473 回答
0

为了查看他们是否已经猜到了一些东西,你可以先有一个空的字符串猜测,当他们做出猜测时,你可以将该字母添加到字符串中:

String guesses = "";
...
guesses+= letter;

看起来你已经在做类似的事情了,但一定要附加字母,而不是为它创建一个新的字符串。这样,您可以每次检查您的猜测字符串是否已经包含新猜测的字母 (if(guesses.contains(letter))。

另外,我不确定为什么你有一个只有一次迭代的 for 循环,但在多字母的情况下,你可以尝试跳出循环,或者如果你觉得可以继续

于 2012-11-10T21:50:39.083 回答
0

我对你的信件检查要做的是:用用过的信件制作一个清单。每次玩家尝试一个字母时,如果该字母在您的列表中,您就会使用 for 循环检查该字母。如果不是,请将其添加到列表中。这是一个例子:

List<String> usedletters = new List<String>(26);

boolean checkLetterUsed(String input){
    boolean tempvariable = false;
    for(String letter: usedletters){
        if(letter==input){
            tempvariable = true;
        }
    }
    if(tempvariable){
        return true;
    }else{
        usedletters.add(input);
        return false;
    }
}
于 2012-11-10T21:53:33.057 回答