I’m writing a Java hangman game, and so far I’ve gotten the code to replace one at a time. What I need is it to replace each time until the word is guessed. Here’s how it works one at a time:
for(int i = 0; i < GuessWord.length(); i++) {
foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");
}
GuessedLetters = xletters.toString().toUpperCase();
WordLabel.setText(foundword.toUpperCase());
GuessedLabel.setText(GuessedLetters);
GuessText.setText(null);
GuessText.requestFocusInWindow();
And here’s my other attempt that didn’t work out so well:
//replace underscores with letters as they are guessed while the word is not solved
do {
foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");
}
while (!SetMain.equals(GuessWord));
//set results to labels
WordLabel.setText(foundword.toUpperCase());
GuessedLabel.setText(GuessedLetters);
GuessText.setText(null);
GuessText.requestFocusInWindow();
And here’s my entire code for those who need it:
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class MainFrame extends javax.swing.JFrame {
public MainFrame() {
initComponents();
}
static String SecretWord = "";
double Result = 0;
StringBuilder mainword = new StringBuilder();
String[] words = {"technology", "computer", "camera", "graphic design", "digital", "media", "technician", "photography", "troubleshoot", "pixels", "application", "download"};
Random r = new Random();
int randvalue = r.nextInt(11);
String GuessWord = words[randvalue];
int errors = 0;
private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {
for(int i = 0; i < GuessWord.length(); i++) {
mainword.append("_ ");
}
mainword.append(SecretWord);
String SetMain = mainword.toString();
WordLabel.setText(SetMain);
GuessButton.setEnabled(true);
GoButton.setEnabled(false);
}
private void GuessButtonActionPerformed(java.awt.event.ActionEvent evt){
String strGuess = GuessText.getText(); //user input
StringBuilder xletters = new StringBuilder(strGuess); // letters guessed
String GuessedLetters = null;
String foundword = null;
//replace underscores with letters as they are guessed
for(int i = 0; i < GuessWord.length(); i++) {
foundword = words[randvalue].replaceAll("[^" + xletters + "]", "_ ");
}
//set to labels to display results
GuessedLetters = xletters.toString().toUpperCase();
WordLabel.setText(foundword.toUpperCase());
GuessedLabel.setText(GuessedLetters);
GuessText.setText(null);
GuessText.requestFocusInWindow();
So I want the code to be able to replace letters cumulatively, i.e. if the word was hello:
Secret Word: _ _ _ _ _
guessed e….
Secret Word: _ e _ _ _
guessed o….
Secret Word: _ e _ _ o
guessed a….
Secret Word: _ e _ _ o
guessed h….
Seret Word: h e _ _ o
guessed l….
Secret Word: h e l l o
Congrats!
I need this to work in Netbeans IDE 7.2, and it has to work for a JLayeredPane, not the System.out.print method. Thanks!
I’m not sure if this is entirly what you after, but it might give you some ideas…
Which produces…
You can check the mask to see if the user has already made a guess simply by using
mask.toLowerCase().contains(stringGuess.toLowerCase())Hope it helps.