Quote:
Originally Posted by Fou-Lu
That's correct. Since it doesn't have a particular branch to go down, that is executed all the time and then it determines which branch to go down (guess or restart).
You can likely group both the restart and okay buttons together though, the functionality sounds like it'd be the same.
|
Thank you so much for your help so far!
I improved my code a bit now and it almost works. The problem now is if the word is "test" and you guess 't' it comes up "t**t" but then if you guess s after that, it comes up "**s*". I know the issue is with the for loop resetting the the array every time the guess button is pushed but I can't think of what to do so that isn't done every time the button is pressed. You've been a big help so far, any other help will be greatly appreciated.
Here is my updated code(just the actionPerformed section):
Code:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == okay){
newErrorCount = Integer.parseInt(errorField.getText());
wordLength = wordField.getText().length();
char[] blankWordArray = new char[wordLength];
actualWord = wordField.getText();
for(i=0; i<wordLength; i++){
if(wordField.getText().charAt(i) ==' '){
blankWordArray[i] = ' ';
}else{
blankWordArray[i] = '*';
}
}
blankWord = new String(blankWordArray);
errorField.setText(""+newErrorCount);
wordField.setText(blankWord);
}else if(source == guess){
char[] guessedWordArray = new char[wordLength];
for(i=0; i<wordLength; i++){
if(wordField.getText().charAt(i) ==' '){
guessedWordArray[i] = ' ';
}else{
guessedWordArray[i] = '*';
}
}
guessChar = guessField.getText().charAt(0);
guessField.setText("");
guessMatched=false;
for(i=0; i<actualWord.length(); i++){
if(guessChar==actualWord.charAt(i)){
guessedWordArray[i]=guessChar;
guessedWord = new String(guessedWordArray);
wordField.setText(guessedWord);
guessMatched = true;
}else{
}
}
if(guessMatched==false){
newErrorCount--;
errorField.setText(""+newErrorCount);
}else{
}
}else if(source == restart){
wordField.setText("");
errorField.setText(""+error);
guessField.setText("");
}
}