Member 14981080 Ответов: 1

Как мне исправить этот графический код для hangman


Last week I was meant to do this Java exam based on this code. Our tutor insists that this code works but I showed it another software developer and he insisted that the code sucks. I didnt go through with the exam as I feel Im not trained properly to do it.

So the code you see here is a J Applet form and copied and pasted below that is a Class. 

Ive went over it 5 times, can you give me advice of where it all went wrong, was it Netbeans?

Hope you can help a newb out. Thanks. 

(And let me stress to you this is the code our tutor came up and told to practice on so I think I hadnt a hope in hell of passing.)









import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author patri
 */
public class GuessTheWord extends javax.swing.JApplet {
Hangman GTW_Object = new Hangman ();
boolean blnKeyPadUnLock;
    
    
    @Override
    public void init() {
       resize(600,500);
        /* Create and display the applet */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    
    private void enterNewLetter (char newLetter){
    
    if(blnKeyPadUnLock){
        
    txtGuessTheWord.setText(new String (GTW_Object.enterNewLetter(newLetter)));
    txtGuessesRemaining.setText (String.valueOf (GTW_Object.getRemainingLives()));
    
       
    }    
   
UpdateImage();    
if(GTW_Object.getFoundWord() && GTW_Object.getRemainingLives() >0){
JOptionPane.showMessageDialog(null,"YOU WIN");    
}        
   
else if (GTW_Object.getFoundWord() && GTW_Object.getRemainingLives () ==0)   
JOptionPane.showMessageDialog(null, "YOU LOSE, THE WORD WAS:"+ GTW_Object.getGeneratedWord());    
    
    }
    
//==============================================================================

private void UpdateImage() {
//DISPLAYS REMAINING LIVES//

    
switch (GTW_Object.getRemainingLives()) {
    
    case 0 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break;   
    case 1 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break;
    case 2 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break; 
    case 3 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break;
    case 4 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break;
    case 5 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break;
    case 6 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break; 
    case 7 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break; 
    case 8 :lblImage.setIcon (new ImageIcon ("Hangman0.jpg"));break;       
            
}     
    
lblImage.setVisible(true);    
      
}
//=========================================================================
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        btnA = new java.awt.Button();
        btnB = new java.awt.Button();
        btnC = new java.awt.Button();
        btnD = new java.awt.Button();
        btnE = new java.awt.Button();
        btnF = new java.awt.Button();
        btnG = new java.awt.Button();
        btnH = new java.awt.Button();
        btnI = new java.awt.Button();
        btnJ = new java.awt.Button();
        btnK = new java.awt.Button();
        btnL = new java.awt.Button();
        btnM = new java.awt.Button();
        btnN = new java.awt.Button();
        btnO = new java.awt.Button();
        btnP = new java.awt.Button();
        btnQ = new java.awt.Button();
        btnR = new java.awt.Button();
        btnS = new java.awt.Button();
        btnT = new java.awt.Button();
        btnU = new java.awt.Button();
        btnV = new java.awt.Button();
        btnW = new java.awt.Button();
        btnX = new java.awt.Button();
        btnY = new java.awt.Button();
        btnZ = new java.awt.Button();
        btnStart = new java.awt.Button();
        btnAnswer = new java.awt.Button();
        txtGuessTheWordTextValueChanged = new java.awt.TextField();
        txtGuessesRemainingTextValueChanged = new java.awt.TextField();

        setBackground(new java.awt.Color(204, 204, 204));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnA.setLabel("A");
        btnA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAActionPerformed(evt);
            }
        });
        getContentPane().add(btnA, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 200, 50, 40));

        btnB.setLabel("B");
        btnB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBActionPerformed(evt);
            }
        });
        getContentPane().add(btnB, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 200, 50, 40));

        btnC.setLabel("C");
        btnC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCActionPerformed(evt);
            }
        });
        getContentPane().add(btnC, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 200, 50, 40));

        btnD.setLabel("D");
        btnD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDActionPerformed(evt);
            }
        });
        getContentPane().add(btnD, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 200, 50, 40));

        btnE.setActionCommand("button E");
        btnE.setLabel("E");
        btnE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEActionPerformed(evt);
            }
        });
        getContentPane().add(btnE, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 200, 50, 40));

        btnF.setLabel("F");
        btnF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFActionPerformed(evt);
            }
        });
        getContentPane().add(btnF, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 200, 50, 40));

        btnG.setLabel("G");
        btnG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGActionPerformed(evt);
            }
        });
        getContentPane().add(btnG, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 200, 50, 40));

        btnH.setLabel("H");
        btnH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHActionPerformed(evt);
            }
        });
        getContentPane().add(btnH, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 270, 50, 40));

        btnI.setLabel("I");
        btnI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIActionPerformed(evt);
            }
        });
        getContentPane().add(btnI, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 270, 50, 40));

        btnJ.setLabel("J");
        btnJ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnJActionPerformed(evt);
            }
        });
        getContentPane().add(btnJ, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 270, 50, 40));

        btnK.setLabel("K");
        btnK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKActionPerformed(evt);
            }
        });
        getContentPane().add(btnK, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 270, 50, 40));

        btnL.setLabel("L");
        btnL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLActionPerformed(evt);
            }
        });
        getContentPane().add(btnL, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 274, 50, 40));

        btnM.setLabel("M");
        btnM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMActionPerformed(evt);
            }
        });
        getContentPane().add(btnM, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 274, 50, 40));

        btnN.setLabel("N");
        btnN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNActionPerformed(evt);
            }
        });
        getContentPane().add(btnN, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 270, 50, 40));

        btnO.setLabel("O");
        btnO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOActionPerformed(evt);
            }
        });
        getContentPane().add(btnO, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 350, 50, 40));

        btnP.setLabel("P");
        btnP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPActionPerformed(evt);
            }
        });
        getContentPane().add(btnP, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 350, 50, 40));

        btnQ.setLabel("Q");
        btnQ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQActionPerformed(evt);
            }
        });
        getContentPane().add(btnQ, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 350, 50, 40));

        btnR.setLabel("R");
        btnR.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRActionPerformed(evt);
            }
        });
        getContentPane().add(btnR, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 350, 50, 40));

        btnS.setLabel("S");
        btnS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSActionPerformed(evt);
            }
        });
        getContentPane().add(btnS, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 350, 60, 40));

        btnT.setLabel("T");
        btnT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTActionPerformed(evt);
            }
        });
        getContentPane().add(btnT, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 350, 50, 40));

        btnU.setLabel("U");
        btnU.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUActionPerformed(evt);
            }
        });
        getContentPane().add(btnU, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 350, 50, 40));

        btnV.setLabel("V");
        btnV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVActionPerformed(evt);
            }
        });
        getContentPane().add(btnV, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 410, 60, 40));

        btnW.setLabel("W");
        btnW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWActionPerformed(evt);
            }
        });
        getContentPane().add(btnW, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 410, 50, 40));

        btnX.setLabel("X");
        btnX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXActionPerformed(evt);
            }
        });
        getContentPane().add(btnX, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 410, 50, 40));

        btnY.setLabel("Y");
        btnY.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnYActionPerformed(evt);
            }
        });
        getContentPane().add(btnY, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 410, 50, 40));

        btnZ.setLabel("Z");
        btnZ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZActionPerformed(evt);
            }
        });
        getContentPane().add(btnZ, new org.netbeans.lib.awtextra.AbsoluteConstraints(323, 410, 50, 40));

        btnStart.setLabel("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });
        getContentPane().add(btnStart, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 414, 50, 40));

        btnAnswer.setLabel("Answer");
        btnAnswer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnswerActionPerformed(evt);
            }
        });
        getContentPane().add(btnAnswer, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 414, -1, 40));

        txtGuessTheWordTextValueChanged.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtGuessTheWordTextValueChangedActionPerformed(evt);
            }
        });
        getContentPane().add(txtGuessTheWordTextValueChanged, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 90, 220, 40));

        txtGuessesRemainingTextValueChanged.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtGuessesRemainingTextValueChangedActionPerformed(evt);
            }
        });
        getContentPane().add(txtGuessesRemainingTextValueChanged, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 150, 110, -1));
    }// </editor-fold>                        

    private void btnAActionPerformed(java.awt.event.ActionEvent evt) {                                     
    enterNewLetter ('A');
    }                                    

    private void btnBActionPerformed(java.awt.event.ActionEvent evt) {                                     
     enterNewLetter ('B');
    }                                    

    private void btnCActionPerformed(java.awt.event.ActionEvent evt) {                                     
      enterNewLetter ('C');
    }                                    

    private void btnDActionPerformed(java.awt.event.ActionEvent evt) {                                     
         enterNewLetter ('D');
    }                                    

    private void btnEActionPerformed(java.awt.event.ActionEvent evt) {                                     
        enterNewLetter ('E');
    }                                    

    private void btnFActionPerformed(java.awt.event.ActionEvent evt) {                                     
        enterNewLetter ('F');
    }                                    

    private void btnGActionPerformed(java.awt.event.ActionEvent evt) {                                     
         enterNewLetter ('G');
    }                                    

    private void btnHActionPerformed(java.awt.event.ActionEvent evt) {                                     
         enterNewLetter ('H');
    }                                    

    private void btnIActionPerformed(java.awt.event.ActionEvent evt) {                                     
         enterNewLetter ('I');
    }                                    

    private void btnJActionPerformed(java.awt.event.ActionEvent evt) {                                     
       enterNewLetter ('J');
    }                                    

    private void btnKActionPerformed(java.awt.event.ActionEvent evt) {                                     
        enterNewLetter ('K');
    }                                    

    private void btnLActionPerformed(java.awt.event.ActionEvent evt) {                                     
       enterNewLetter ('L');
    }                                    

    private void btnMActionPerformed(java.awt.event.ActionEvent evt) {                                     
       enterNewLetter ('M');
    }                                    

    private void btnNActionPerformed(java.awt.event.ActionEvent evt) {                                     
         enterNewLetter ('N');
    }                                    

    private void btnOActionPerformed(java.awt.event.ActionEvent evt) {                                     
       enterNewLetter ('O');
    }                                    

    private void btnPActionPerformed(java.awt.event.ActionEvent evt) {                                     
        enterNewLetter ('P');
    }                                    

    private void btnQActionPerformed(java.awt.event.ActionEvent evt) {                                     
       enterNewLetter ('Q');
    }                                    

    private void btnRActionPerformed(java.awt.event.ActionEvent evt) {                                     
         enterNewLetter ('R');
    }                                    

    private void btnSActionPerformed(java.awt.event.ActionEvent evt) {                                     
        enterNewLetter ('S');
    }                                    

    private void btnTActionPerformed(java.awt.event.ActionEvent evt) {                                     
         enterNewLetter ('T');
    }                                    

    private void btnUActionPerformed(java.awt.event.ActionEvent evt) {                                     
       enterNewLetter ('U');
    }                                    

    private void btnVActionPerformed(java.awt.event.ActionEvent evt) {                                     
     enterNewLetter ('V');
    }                                    

    private void btnWActionPerformed(java.awt.event.ActionEvent evt) {                                     
       enterNewLetter ('W');
    }                                    

    private void btnXActionPerformed(java.awt.event.ActionEvent evt) {                                     
       enterNewLetter ('X');
    }                                    

    private void btnYActionPerformed(java.awt.event.ActionEvent evt) {                                     
        enterNewLetter ('Y');
    }                                    

    private void btnZActionPerformed(java.awt.event.ActionEvent evt) {                                     
        enterNewLetter ('Z');
    }                                    

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {                                         
    blnKeyPadUnLock = true;
    GTW_Object.getGeneratedWord();
    UpdateImage();
    
    txtGuessTheWord.setText(GTW_Object.getMaskedWord());
    txtGuessesRemaining.setText(String.valueOf)(GTW_Object.getRemainingLives()));
        
    }                                        

    private void btnAnswerActionPerformed(java.awt.event.ActionEvent evt) {                                          
    blnKeyPadUnLock = true;
    UpdateImage();
    txtGuessTheWord.setText(GTW_Object.getGeneratedWord());
    txtGuessesRemaining.setText("0");      
        
        
    }                                         

    private void txtGuessTheWordTextValueChangedActionPerformed(java.awt.event.ActionEvent evt) {                                                                
    //GUESS THE WORD BOX
    
    if (txtGuessTheWord.getText().length()>24)
    txtGuessTheWord.setText(txtGuessTheWord.getText().substring(0,24));
   
    }                                                               

    private void txtGuessesRemainingTextValueChangedActionPerformed(java.awt.event.ActionEvent evt) {                                                                    
       if (txtGuessesRemaining.getText().lenght()>0)
       txtGuessesRemaining.setText(txtGuessesRemaining.getText().substring(0,3))    
           
    }                                                                   


    // Variables declaration - do not modify                     
    private java.awt.Button btnA;
    private java.awt.Button btnAnswer;
    private java.awt.Button btnB;
    private java.awt.Button btnC;
    private java.awt.Button btnD;
    private java.awt.Button btnE;
    private java.awt.Button btnF;
    private java.awt.Button btnG;
    private java.awt.Button btnH;
    private java.awt.Button btnI;
    private java.awt.Button btnJ;
    private java.awt.Button btnK;
    private java.awt.Button btnL;
    private java.awt.Button btnM;
    private java.awt.Button btnN;
    private java.awt.Button btnO;
    private java.awt.Button btnP;
    private java.awt.Button btnQ;
    private java.awt.Button btnR;
    private java.awt.Button btnS;
    private java.awt.Button btnStart;
    private java.awt.Button btnT;
    private java.awt.Button btnU;
    private java.awt.Button btnV;
    private java.awt.Button btnW;
    private java.awt.Button btnX;
    private java.awt.Button btnY;
    private java.awt.Button btnZ;
    private java.awt.TextField txtGuessTheWordTextValueChanged;
    private java.awt.TextField txtGuessesRemainingTextValueChanged;
    // End of variables declaration                   
}


_____________________________________________________________________________________

CLASS



import java.util.ArrayList;
import java.util.Collections;
/**
 *
 * @author patri
 */
public class Hangman {
    
public ArrayList<String>ListOfwords = new ArrayList<>();
private final ArrayList <Character> ListOfEnteredLetters = new ArrayList<>();
private int intRemainingLives;
private boolean blnlifeLost;
private boolean blnFoundWord;
private String GeneratedWord;
private StringBuilder sbMaskedWord;

//======================================================================


public Hangman () {
    
ListOfwords.add("JAVA");   
ListOfwords.add("TUESDAY");     
ListOfwords.add("WINDOWS");     
ListOfwords.add("NOKIA");     
ListOfwords.add("MARKER");     
        
}
    

//========================================================================

@SuppressWarnings("empty-statement")
public void generatedNewWord() {
    
Collections.shuffle (ListOfwords);
GeneratedWord = ListOfwords.get(0);
sbMaskedWord = new StringBuilder(GeneratedWord);
ListOfEnteredLetters.clear();
intRemainingLives = 8;
blnFoundWord=false;

//inserting astericks for every word

for (int i = 0, i < sbMaskedWord.length(); i ++) {
sbMaskedWord.setCharAt(i,'*');

    
}
        
}

    
 //==================================================================

public int getRemainingLives(){
return intRemainingLives;
    
}

//=====================================================================

public String getMaskedWord() {
    
return sbMaskedWord.toString();
    
    
}    

//=====================================================================

public String getGeneratedWord() {
return GeneratedWord;    
        
}
//=======================================================================

public boolean getFoundWord() {
    
return blnFoundWord;   
        
}

//========================================================================

public String enterNewLetter (char chrLetter) {
    
 blnlifeLost = true;
 
 if(! ListOfEnteredLetters.contains(chrLetter)) {
      ListOfEnteredLetters.add(chrLetter);
      System.out.println(ListOfEnteredLetters);
      
      
  for (int i = 0; i < GeneratedWord.length(); i++) {
      
  if (GeneratedWord.charAt(i)== chrLetter) {
      
  sbMaskedWord.setCharAt(i,chrLetter);
  blnlifeLost=false;
      
      
        }        
      
    }   
           
 }
    
 else 
     blnlifeLost=false;
    
 //updatelives   
    
 if (blnlifeLost == true ){
 intRemainingLives--;    
     
     
     
 }
 
 if (GeneratedWord.equals(sbMaskedWord.toString()))
     blnFoundWord = true;
 
 return sbMaskedWord.toString();
     
     
}



}


Что я уже пробовал:

Все! примерно в 5 раз. Даже начиная с JFrame и преобразуя его в форму JApplet.

Richard MacCutchan

К сожалению, этот сайт не предоставляет услуги проверки кода. Если у вас есть реальная проблема, то, пожалуйста, объясните, что это такое и где в коде она возникает.

1 Ответов

Рейтинг:
0

Gerry Schmitz

Проблема в том, что вы не знаете, как писать "универсальный" код.

"Шаблон" здесь заключается в том, что вы используете буквы алфавита, и вы должны быть в состоянии создать "петлю", которая проходит через алфавит и делает это с помощью некоторого "общего буквенного кода" ... вместо того, чтобы повторять все 26 раз.

Ваша "программа" примерно в 20 раз больше, чем должна быть. Не могу поверить, что кто-то говорит тебе сделать это таким образом.