|
|
@ -41,12 +41,16 @@ public class Hangman extends Application { |
|
|
scene.setRoot(newPane); |
|
|
scene.setRoot(newPane); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scene.setOnKeyPressed( |
|
|
scene.setOnKeyPressed( |
|
|
e -> { |
|
|
e -> { |
|
|
|
|
|
//System.out.println("Key pressed!!"); |
|
|
if (activeGame) { |
|
|
if (activeGame) { |
|
|
String guess = e.getText(); |
|
|
String guess = e.getText(); |
|
|
|
|
|
//System.out.println(guess); |
|
|
guess = guess.toLowerCase();//use a string method to convert guess to lowercase |
|
|
guess = guess.toLowerCase();//use a string method to convert guess to lowercase |
|
|
checkGuess(guess, statusBox); |
|
|
checkGuess(guess, statusBox); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
@ -60,7 +64,7 @@ public class Hangman extends Application { |
|
|
|
|
|
|
|
|
//set new word to guess |
|
|
//set new word to guess |
|
|
this.secretWord = dict.getRandomWord(); |
|
|
this.secretWord = dict.getRandomWord(); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println(this.secretWord); |
|
|
GridPane word = new GridPane(); |
|
|
GridPane word = new GridPane(); |
|
|
word.setHgap(5); |
|
|
word.setHgap(5); |
|
|
|
|
|
|
|
|
@ -74,7 +78,7 @@ public class Hangman extends Application { |
|
|
//create letterGuesses |
|
|
//create letterGuesses |
|
|
this.letterGuesses = new Text[26];// finish this line |
|
|
this.letterGuesses = new Text[26];// finish this line |
|
|
for (int i = 0; i < 26; i++) { |
|
|
for (int i = 0; i < 26; i++) { |
|
|
String temp = "_";//finish this line |
|
|
|
|
|
|
|
|
String temp = String.valueOf(Dictionary.ALPHABET.charAt(i));//finish this line |
|
|
this.letterGuesses[i] = new Text(temp); |
|
|
this.letterGuesses[i] = new Text(temp); |
|
|
letterGuesses[i].setFont(gameFont); |
|
|
letterGuesses[i].setFont(gameFont); |
|
|
lettersPane.add(letterGuesses[i], i % 13, i / 13); |
|
|
lettersPane.add(letterGuesses[i], i % 13, i / 13); |
|
|
@ -152,8 +156,13 @@ public class Hangman extends Application { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
private int findLetter(String s) { |
|
|
private int findLetter(String s) { |
|
|
int index = Dictionary.ALPHABET.indexOf(s); |
|
|
|
|
|
boolean isLetter = (index != -1) && (s.length() == 1); |
|
|
|
|
|
|
|
|
if (s.length() == 1) { |
|
|
|
|
|
return Dictionary.ALPHABET.indexOf(s); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
/*boolean isLetter = (index != -1) && (s.length() == 1); |
|
|
//Determine if s contains a single letter of the alphabet, using String methods |
|
|
//Determine if s contains a single letter of the alphabet, using String methods |
|
|
//Return the index of this letter in the array letterGuesses |
|
|
//Return the index of this letter in the array letterGuesses |
|
|
//e.g. "a" is stored in index 0, "b" in index 1, etc. |
|
|
//e.g. "a" is stored in index 0, "b" in index 1, etc. |
|
|
@ -167,7 +176,7 @@ public class Hangman extends Application { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return ret; |
|
|
|
|
|
|
|
|
return ret;*/ |
|
|
//write your code for this method |
|
|
//write your code for this method |
|
|
} |
|
|
} |
|
|
private boolean alreadyGuessed(int index) { |
|
|
private boolean alreadyGuessed(int index) { |
|
|
|