0
I’m creating a hangman’s game for further learning of programming logic.
1- I masked the textfield
with ***
so that the user cannot see the word.
2- In my example, I created 3 buttons, being respectively "A", "B" e "C"
.
Suppose the word to be guessed is "mara"
, when I click the button "A"
, i can find the position of the letter a in String. They meet in the posição 2 e 4
. How the text is masked with "*"
, when I click the button "A"
, I would like the jtextfield
presented all the letters "A"
.
Would look like this:
Example: *a*a
But I can’t figure out how to show only those letters.
Follow the code of my simple checkable example:
String p;
int tamanho;
Scanner input = new Scanner(System.in);
public Principal() {
initComponents();
jba.addActionListener(this);
jbb.addActionListener(this);
jbc.addActionListener(this);
System.out.println("Digite uma palavra: ");
p = input.nextLine();
String replaceP = p.replaceAll("[a-zA-Z]", "*");
jTextFieldPalavra.setText(replaceP);
}
@Override
public void actionPerformed(ActionEvent ae) {
int tamanho = p.length();
if(ae.getSource() == jba) {
for(int i=0; i<tamanho; i++) {
if(p.substring(i, i+1).equals("a")) {
int posicao = i+1;
System.out.println("Está na posição " + posicao);
//System.out.println(p.substring(0, posicao));
}
}
} else if(ae.getSource() == jbb) {
for(int i=0; i<tamanho; i++) {
if(p.substring(i, i+1).equals("b")) {
int posicao = i+1;
System.out.println("Está na posição " + posicao);
//System.out.println(p.substring(0, posicao));
}
}
} else if(ae.getSource() == jbc) {
for(int i=0; i<tamanho; i++) {
if(p.substring(i, i+1).equals("c")) {
int posicao = i+1;
System.out.println("Está na posição " + posicao);
//System.out.println(p.substring(0, posicao));
}
}
}
}
Can add a [mcve] of your code? I think you can apply Document to it.
– user28595
So, this would be the example Minimum, Complete and Verifiable that I created rs. But I will try to represent better according to the link you sent me. ;)
– Marcielli Oliveira
The code is not executable, try to copy and save as . java and run for you to see. Anyway, look what I found on another SE site, the same game, see if you can understand the logic http://codereview.stackexchange.com/questions/82440/hangman-program-in-java
– user28595
I understood, I didn’t pay attention to it. Thank you, I will read and try to understand.
– Marcielli Oliveira