Java - Guess The Word Game Source Code

How To Make Guess The Word Game Project Using NetBeans

Java Guess The Word Game Source Code


In this Java Tutorial we will see How to Make a Simple Word Guessing Game App In Java NetBeans
In This Java NetBeans Game Project We Will Use Using Jframe JPanels, JButtons and Jlabels .





Project Source Code:

     // array of words
    String[] words = {"driver","signature","history", "response","president","highway", 
                      "computer", "appartment", "forest", "chocolat", "lawyer"};
    int index = -1;
    
    Border panel_border = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.black);
    
    public Guess_Word_Game() {
        initComponents();
        
        this.setLocationRelativeTo(null);
        
        jPanel1.setBorder(panel_border);
        
        displayWord();
        
        jButton_Next_.setEnabled(false);
    }
    
   
    // create a function to display the word
    public void displayWord()
    {
        // default text
        if(index == -1)
        {
            jLabel_Word.setText("--Word--");
            jTextField_Guess.setText("--Guess--");
        }
        else
        {
            // get random position
            int pos1 = (int) (Math.random() * words[index].length());
            int pos2 = (int) (Math.random() * words[index].length());
            int pos3 = (int) (Math.random() * words[index].length());
            
            // set '_' at random positions
            StringBuilder newtext = new StringBuilder(words[index]);
            newtext.setCharAt(pos1, '_');
            newtext.setCharAt(pos2, '_');
            newtext.setCharAt(pos3, '_');
            
            // set text to the jlabel
            jLabel_Word.setText(newtext.toString());
        }
    }
    
    // create a function to check if the user guessed the correct word
    public void checkWord()
    {
        // if the guess is correct
        if(jTextField_Guess.getText().equals(words[index]))
        {
            jLabel_Result.setText("Correct");
            jLabel_Result.setBackground(Color.green);
        }
        // if not
        else
        {
            jLabel_Result.setText("Wrong");
            jLabel_Result.setBackground(Color.red);
        }
        // if it's the last word in the list
        if(index == words.length - 1)
        {
            jButton_Next_.setEnabled(false);
            jButton_Start_.setEnabled(true);
        }
        jTextField_Guess.setText("");
    }
    
     // label to close the form
    private void jLabel_Close_MouseClicked(java.awt.event.MouseEvent evt) {                                           
        // close the form
        System.exit(0);
    }                                          

    // button start / restart the game
    private void jButton_Start_ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // start or restart the game
        index = 0;
        jTextField_Guess.setText("");
        jButton_Next_.setEnabled(true);
        jButton_Start_.setEnabled(false);
        jLabel_Result.setText("Result");
        jLabel_Result.setBackground(new java.awt.Color(102, 102, 102));
        displayWord(); 
    }                                              

    // button next
    private void jButton_Next_ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // check the word / display the next word
        checkWord();
        if(index < words.length - 1)
        {
           index++;
           displayWord(); 
        }
    }                                             
                                          



////// OUTPUT : 

Guess The Word Game Source Code In Java 1

Guess The Word Game Source Code In Java 2

Guess The Word Game Source Code In Java 3

Guess The Word Game Source Code In Java 4

Guess The Word Game Source Code In Java 5





download the source code



More Java Projects:





Share this

Related Posts

Previous
Next Post »