Hangman game getting user input from JTextField

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1300 Views - Last Post: 30 November 2015 - 07:30 AM Rate Topic: -----

#1 tomaszsvd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-November 15

Hangman game getting user input from JTextField

Posted 28 November 2015 - 01:40 PM

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.



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


Is This A Good Question/Topic? 0
  • +

Replies To: Hangman game getting user input from JTextField

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,943
  • Joined: 25-December 13

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 01:49 PM

Quote

help me with sorting this out
Please explain what the problem is.
What does the user do
and what does the program do in response?

Note: Instead of a bunch of if statements to test the value of wrong and use it to get a value,
another way to do it would be to put all the values in an array and use the value of wrong to index into the array.
Was This Post Helpful? 0
  • +
  • -

#3 tomaszsvd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-November 15

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 02:10 PM

I have a static String [] words = { "dog", "java" }; declared
those strings are picked as random and showed in a label called Letter0 as XXXX
user should enter a letter in to a jTextField and press the guess button, and correct XXXX should be substituted with a character
like a guess word, the current problem is to get that user input to check for a correct character in those XXX.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,943
  • Joined: 25-December 13

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 02:27 PM

Quote

problem is to get that user input to check for a correct character in those XXX.

Ok that sounds like a logic problem that needs some steps worked out to solve.
What is the user's input? A single character or a full String to match?
Is the user's input in the text field?
If the input is a single char, the logic needs to do what? What are the steps you would do manually? When you get the steps, then work on the coding.

This post has been edited by NormR: 28 November 2015 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 tomaszsvd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-November 15

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 02:52 PM

User inputs a single character in to a jTextField. if String [] words = { "dog", "java" }; contains this character one of the X in the label called Letter should be substituted with the correct character if user has guessed correctly
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,943
  • Joined: 25-December 13

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 02:57 PM

Ok, that sounds like a description of what the code should do. Now work out the steps needed to do it.
When you have the steps(in English) try writing the code to implement the steps.
Was This Post Helpful? 0
  • +
  • -

#7 tomaszsvd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-November 15

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 03:03 PM

It's the whole problem i know what the program should do and what the all steps are, but i cnat get this working in the code you see...
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,943
  • Joined: 25-December 13

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 03:06 PM

Quote

i know what the program should do and what the all steps are

Good. Can you list the steps?
Then we'll help you write the code for each step, one by one.
Each step shouldn't take over 1 or 2 lines of code.

This post has been edited by NormR: 28 November 2015 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#9 tomaszsvd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-November 15

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 03:26 PM

Step 1 Read the length of string such as “dog” string is chosen as random and display corresponding XXX in the jLabel
Step 2 User enters single character in to a jTextField, program reads user input.
Step 3 program checks the user input and looks for the equivalent character in the string
Step 4 program displays inputted character (only if guessed correctly) in the jLabel
Step 5 if character is not correct add one to label called wrongGuess and start to show picture of hangman
Step 6 if all inputted characters matches the string display “YOU HAVE WON”
Step 7 if all inputted characters don’t matches the string display “YOU HAVE LOST ”
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,943
  • Joined: 25-December 13

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 03:33 PM

step 1 - build String of Xs the same length as hidden word.
A couple of ways to do that:
1) write a loop to concatenate Xs to a String
2) have a loong String of Xs and use the length of the hidden word as the substring value
Was This Post Helpful? 0
  • +
  • -

#11 tomaszsvd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-November 15

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 03:38 PM

also please note that I'am using drag and drop features for buttons and labels in NetBeans.

Step 1 is already implemented in the program

 for (a = 0; a < 2; a++) {
            wordLength[a] = words[a].length();
        }while (b < wordLength[level]) {
            line += " X";
            b++;
        }

Program finds the random string and prints the corresponding amount of X in the label
The only problem im having here is with the user interaction (input) and checking the corresponding letter in the string


Step 2 User enters single character in to a jTextField, program reads user input.
Step 3 program checks the user input and looks for the equivalent character in the string
Step 4 program displays inputted character (only if guessed correctly) in the jLabel
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,943
  • Joined: 25-December 13

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 03:46 PM

Ok you have the code for step 1.
step 2 looks very easy. I assume you have that
step 3 has two parts:
1-program checks the user input - what is this check for? For example if the input is one character in the range A-Z?
2-looks for the equivalent character in the string - The String class has a method that will see if one String is in another String. Look at the API doc for the String class.
Was This Post Helpful? 0
  • +
  • -

#13 tomaszsvd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-November 15

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 03:52 PM

Unfortunately not much help. I have no clue what should be used and how ...
Was This Post Helpful? 0
  • +
  • -

#14 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,943
  • Joined: 25-December 13

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 03:55 PM

Which step or substep are you having problems with?
The first part of step 3 was - program checks the user input
What checks are supposed to be made?

Note: If you are expecting me to write the code, you will be disappointed.

This post has been edited by NormR: 28 November 2015 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#15 tomaszsvd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-November 15

Re: Hangman game getting user input from JTextField

Posted 28 November 2015 - 04:14 PM

This is the problem all the way,
i know that program should check for characters A-Z and if string contains them

1-program checks the user input - what is this check for? For example if the input is one character in the range A-Z?
2-looks for the equivalent character in the string - The String class has a method that will see if one String is in another String. Look at the API doc for the String class.

Note: im not expecting ready code from you, i would never do that its called plagiarism.
But it would be nice if you could point me in the right direction to do a research on this and what to look for, what functions should i use to implement this
The string i have is an Array so the user input has to be an array right? what function should be done to get that one inputed character in to an array and what function to use to get it check if the full string includes this character
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2