为什么这段代码不起作用?刽子手 [英] Why is this code not working? Hangman

查看:23
本文介绍了为什么这段代码不起作用?刽子手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个刽子手游戏.一切正常,我已经准备好用于使游戏失败并为猜测提供 -1 的代码.虽然当将它添加到 else 语句时,它会重复等于单词的长度,并且它也给出了一个猜测——即使它是对的?我看不出代码有什么问题?我相信这是我猜错时的代码,虽然我看不出其他方法,但没有正确放置?

I am creating a hangman game. Everything works fine, I have code ready to be used for failing the game and giving -1 to the guesses. Though when adding it to the else statement it gets duplicate equal to the length of the word and it also gives a guess—even though its right? I don't see what's wrong in the code? I believe it's my code when guessing wrong which is not placed right though I see no other way?

这是代码:

private class check implements ActionListener {
   public void actionPerformed(ActionEvent ae) {
      try {
         // Grabs the letter from the guessField and converts it into a char
         // which can be used to compare against the word.
         guess = guessField.getText();
         guessField.setText("");
         char guess2 = guess.charAt(0);

         // --------------------
         // Here is the guessing logic but it's currently
         // not working and you can not win since i haven't written code for
         // it yet. it's not selecting all the letters. for Example if
         // choosing A in a word such as Banana it only selects the first
         // a--------------------------- //
         String displaySecret = wordField.getText();
         if (displaySecret.equals("")) {/* case for fist execution */
            displaySecret = "";
            for (int i = 0; i < random.length(); i++)
               displaySecret += "_ ";
         }
         String newDisplaySecret = "";
         for (int v = 0; v < random.length(); v++) {
            if (guess2 == random.charAt(v)) {
               newDisplaySecret += random.charAt(v); // newly guessed
                                                     // character
            } else {
               newDisplaySecret += displaySecret.charAt(v); // old state
               guesses--;
               statusLabel.setText("Guesses left: " + guesses);
               missField.setText(missField.getText() + guess);
               if (guesses <= 0) {
                  JOptionPane.showMessageDialog(null,
                        "Game over! The word was: " + random);
                  guessField.setEditable(false);
                  wordField.setText("");
                  missField.setText("");
                  guesses = 7;
                  statusLabel.setText("Guesses left: " + guesses);
               }
            }
         }
         displaySecret = new String(newDisplaySecret);
         wordField.setText(displaySecret);
         if (displaySecret.equals(random)) {
            JOptionPane.showMessageDialog(null, "You Won! The Word was: "
                 + random);
            guesses = 7;
            statusLabel.setText("Guesses left: " + guesses);
            wordField.setText("");
            missField.setText("");
            guessField.setEditable(false);
         }
      } catch (Exception e) {
         System.out.println(e);
      }
   }
}

推荐答案

以下是您检查单词并生成newDisplaySecret"所需的内容:

Here is what you need to check your word and generate "newDisplaySecret":

for (int v = 0; v < random.length(); v++) {
      if (guess2 == random.charAt(v)) {
           newDisplaySecret += random.charAt(v); // newly guessed
                                                 // character
        } else {
           newDisplaySecret += displaySecret.charAt(v);
}

以下是确定玩家猜对还是错的方法:

Here is how you can determine whether the player guessed right or wrong:

if(newDisplaySecret.equals(displaySecret)){
    guesses --;

}

这需要放在您的校验字代码之后.您的代码似乎减少了单词 random 中每个字母的猜测.

This needs to be placed after your check word code. Your code seems to decrement guesses for each letter in the word random.

更新显示:

     displaySecret = new String(newDisplaySecret);
     wordField.setText(displaySecret);

既然您知道了这一举动的当前状况,您就可以决定该人是赢了还是输了,还是只需要继续玩:

Now that you know what the current state of affairs is for this move you can decide if the person has won or lost or just needs to continue playing:

if(guesses <= 0){
    /*place here instructions for loosing scenario*/
}else{
     if(displaySecret.equals(random)) {
         /*place here instructions for winning scenario*/
     }
    /*simply do nothing the game is neither lost or won*/
}

希望能帮到你

这篇关于为什么这段代码不起作用?刽子手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆