dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Hangman game question jTextField string check character

  1. #1
    Join Date
    Nov 2015
    Posts
    1

    Hangman game question jTextField string check character

    I'm Trying to write a Simple Hangman game, so far i have managed to get the word length and the word chosen randomly. However i can't find the way to implement user input. The idea is that user inputs a letter in to JTextField and the XXX is substituted if the letter is guessed correctly. I would appreciate if someone can help me with sorting this out, please have a look at the code i have so far, also pleas note this is done in NetBeans.
    I need to check if the user input in to JTextField is equal to a stored String[]

    Code:
    static String [] words = { "dog", "java" };
    int[] wordLength = new int[2];
    String line = "",letter;    
    int level = (int) (Math.random() * 2);    
    int a = 0,b = 0, wrong=0;
    
    private void btGuessActionPerformed(java.awt.event.ActionEvent evt) {                                        
        for (a = 0; a < 2; a++) {
            wordLength[a] = words[a].length();
        } 
        while (b < wordLength[level]) {
            line += " X";
            b++;
        }
    
        Letter0.setText(line);
        letter = uInput.getText();
        if (!words[level].contains(letter)) {
            wrong++;
            if (wrong == 1) {
                pbShow.setIcon(new javax.swing.ImageIcon("images/H0.png"));
            }
            if (wrong == 2) {
                pbShow.setIcon(new javax.swing.ImageIcon("images/H1.png"));
            }
            if (wrong == 3) {
                pbShow.setIcon(new javax.swing.ImageIcon("images/H2.png"));
            }
            if (wrong == 4) {
                pbShow.setIcon(new javax.swing.ImageIcon("images/H3.png"));
            }
            if (wrong == 5) {
                pbShow.setIcon(new javax.swing.ImageIcon("images/H4.png"));
            }
            if (wrong == 6) {
                pbShow.setIcon(new javax.swing.ImageIcon("images/H5.png"));
            }
            if (wrong == 7) {
                pbShow.setIcon(new javax.swing.ImageIcon("images/Hlost.png"));
            }              
        }
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,853

    Re: Hangman game question jTextField string check character

    Norm

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)