为什么这段代码不起作用?刽子手

时间:2013-10-17 21:33:48

标签: java swing

我正在制作一个刽子手游戏。一切正常,我已准备好代码用于失败游戏并给出猜测-1。虽然将它添加到else语句时它会重复等于单词的长度并且它也会猜测 - 即使它正确吗?我没看到代码中有什么问题?我相信这是我的代码,当我猜错了哪个不正确,虽然我没有别的办法吗?

这是代码:

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);
      }
   }
}

2 个答案:

答案 0 :(得分:3)

如果random是您的Word,则迭代它的每个字符,然后检查每个单个字符是否与每个与猜测a -1不匹配的字符的猜测相匹配。

例如:Word为Bananarama,您猜测n您的第一个和第二个匹配将转到else子句。然后有一次if子句再次出现,然后是else等等。

你必须

  1. 遍历所有字符,检查它们是否匹配
  2. 如果匹配发生,请替换char并增加计数器
  3. 检查正确字符的计数器是否等于0
  4. 如果是,请减少猜测
  5. 其他一些提示:您应该在比较之前对输入和字符串使用.toLower()以允许对案例不敏感

    一些示例代码:

    int charsGuessedCorrectly;
    for ( int i = 0; i < random.length( ); i++ )
    {
        if ( random.charAt( i ) == guess )
        {
            charsGuessedCorrectly++;
            newDisplaySecret += random.charAt(v); // newly guessed
                                                  // character
        }
    }
    
    if ( charsGuessedCorrectly == 0 )
    {
        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);
    }
    

答案 1 :(得分:1)

以下是检查单词并生成“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);
}

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

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

}

这需要放在检查字代码之后。你的代码似乎减少了随机单词中每个字母的猜测。

更新显示:

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

现在你知道这一举动的现状是什么,你可以决定这个人是赢还是输,或者只是需要继续比赛:

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*/
}

希望这有帮助