|
|
|
@ -11,75 +11,79 @@ import javafx.scene.input.*; |
|
|
|
public class Hangman extends Application { |
|
|
|
private Dictionary dict = new Dictionary(); |
|
|
|
private String secretWord; |
|
|
|
|
|
|
|
|
|
|
|
private Text[] letterGuesses; |
|
|
|
private Text[] lettersRevealedSoFar; |
|
|
|
private Shape[] hangman; |
|
|
|
|
|
|
|
|
|
|
|
private int currentHangmanPart; |
|
|
|
private boolean activeGame; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void start(Stage primaryStage) { |
|
|
|
public void start(Stage primaryStage) { |
|
|
|
primaryStage.setTitle("Hangman"); |
|
|
|
Font gameFont = Font.font("Consolas", 22.0); |
|
|
|
|
|
|
|
|
|
|
|
Text statusBox = new Text(200, 100, ""); |
|
|
|
statusBox.setFont(gameFont); |
|
|
|
Pane main = this.setUpLayout(statusBox); |
|
|
|
Scene scene = new Scene(main, 400, 500); |
|
|
|
|
|
|
|
|
|
|
|
Button resetButton = new Button("New Game"); |
|
|
|
resetButton.setDefaultButton(true); |
|
|
|
resetButton.relocate(25,350); |
|
|
|
main.getChildren().add(resetButton); |
|
|
|
|
|
|
|
resetButton.setOnAction( |
|
|
|
main.getChildren().add(resetButton); |
|
|
|
|
|
|
|
resetButton.setOnAction( |
|
|
|
e -> { |
|
|
|
Pane newPane = setUpLayout(statusBox); |
|
|
|
newPane.getChildren().add(resetButton); |
|
|
|
scene.setRoot(newPane); |
|
|
|
}); |
|
|
|
|
|
|
|
scene.setOnKeyPressed( |
|
|
|
|
|
|
|
|
|
|
|
scene.setOnKeyPressed( |
|
|
|
e -> { |
|
|
|
//System.out.println("Key pressed!!"); |
|
|
|
if (activeGame) { |
|
|
|
String guess = e.getText(); |
|
|
|
//System.out.println(guess); |
|
|
|
guess = guess.toLowerCase();//use a string method to convert guess to lowercase |
|
|
|
checkGuess(guess, statusBox); |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
primaryStage.setScene(scene); |
|
|
|
primaryStage.show(); |
|
|
|
} |
|
|
|
|
|
|
|
private Pane setUpLayout(Text statusBox) { |
|
|
|
|
|
|
|
private Pane setUpLayout(Text statusBox) { |
|
|
|
Pane main = new Pane(); |
|
|
|
Font gameFont = Font.font("Consolas", 22.0); |
|
|
|
|
|
|
|
|
|
|
|
//set new word to guess |
|
|
|
this.secretWord = dict.getRandomWord(); |
|
|
|
|
|
|
|
System.out.println(this.secretWord); |
|
|
|
GridPane word = new GridPane(); |
|
|
|
word.setHgap(5); |
|
|
|
|
|
|
|
|
|
|
|
this.currentHangmanPart = 0; |
|
|
|
|
|
|
|
|
|
|
|
GridPane lettersPane = new GridPane(); |
|
|
|
lettersPane.relocate(25, 430); |
|
|
|
lettersPane.setHgap(5); |
|
|
|
lettersPane.setVgap(5); |
|
|
|
|
|
|
|
|
|
|
|
//create letterGuesses |
|
|
|
this.letterGuesses = new Text[26];// finish this line |
|
|
|
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); |
|
|
|
letterGuesses[i].setFont(gameFont); |
|
|
|
lettersPane.add(letterGuesses[i], i % 13, i / 13); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//set up lettersRevealedSoFar |
|
|
|
this.lettersRevealedSoFar = new Text[this.secretWord.length()]; |
|
|
|
for (int i = 0; i < lettersRevealedSoFar.length; i++) { |
|
|
|
@ -87,7 +91,7 @@ public class Hangman extends Application { |
|
|
|
this.lettersRevealedSoFar[i].setFont(gameFont); |
|
|
|
word.add(this.lettersRevealedSoFar[i], i, 0); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//set up the gallows |
|
|
|
Pane figure = new Pane(); |
|
|
|
Shape[] gallows = { new Line(120, 40, 120, 10), |
|
|
|
@ -100,7 +104,7 @@ public class Hangman extends Application { |
|
|
|
gallows[i].setStrokeWidth(2.0); |
|
|
|
figure.getChildren().add(gallows[i]); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//set up the hangman outline |
|
|
|
this.hangman = new Shape[6]; |
|
|
|
this.hangman[0] = new Circle(120, 60, 20, Color.WHITE); // head |
|
|
|
@ -109,21 +113,21 @@ public class Hangman extends Application { |
|
|
|
this.hangman[3] = new Line(120, 110, 160, 90); // right arm |
|
|
|
this.hangman[4] = new Line(120, 150, 100, 200); // left leg |
|
|
|
this.hangman[5] = new Line(120, 150, 140, 200); // right leg |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this.hangman.length; i++) { |
|
|
|
this.hangman[i].setStroke(Color.WHITE); |
|
|
|
this.hangman[i].setStrokeWidth(3.0); |
|
|
|
figure.getChildren().add(this.hangman[i]); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//set up the alphabet |
|
|
|
Text title = new Text(25, 420, "Guesses so far:"); |
|
|
|
|
|
|
|
|
|
|
|
//ready to play |
|
|
|
this.activeGame = true; |
|
|
|
statusBox.setText("Ready to play"); |
|
|
|
statusBox.setFill(Color.BLUE); |
|
|
|
|
|
|
|
|
|
|
|
//place everything in window |
|
|
|
word.relocate(20, 300); |
|
|
|
main.getChildren().add(word); |
|
|
|
@ -131,16 +135,16 @@ public class Hangman extends Application { |
|
|
|
main.getChildren().add(title); |
|
|
|
main.getChildren().add(lettersPane); |
|
|
|
main.getChildren().add(statusBox); |
|
|
|
|
|
|
|
|
|
|
|
return main; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void checkGuess(String s, Text statusBox) { |
|
|
|
int index = this.findLetter(s); |
|
|
|
|
|
|
|
|
|
|
|
if (!alreadyGuessed(index)) { |
|
|
|
boolean found = foundInSecretWord(s); |
|
|
|
|
|
|
|
|
|
|
|
if (found) /* correct guess */ { |
|
|
|
this.letterGuesses[index].setFill(Color.GREEN); |
|
|
|
checkIfWon(s, statusBox); |
|
|
|
@ -152,8 +156,13 @@ public class Hangman extends Application { |
|
|
|
} |
|
|
|
} |
|
|
|
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 |
|
|
|
//Return the index of this letter in the array letterGuesses |
|
|
|
//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 |
|
|
|
} |
|
|
|
private boolean alreadyGuessed(int index) { |
|
|
|
@ -218,7 +227,7 @@ public class Hangman extends Application { |
|
|
|
if (this.currentHangmanPart < 6) { |
|
|
|
this.hangman[this.currentHangmanPart].setStroke(Color.BLACK); |
|
|
|
this.currentHangmanPart++; |
|
|
|
|
|
|
|
|
|
|
|
statusBox.setText("Letter " + s + " is \nnot correct."); |
|
|
|
statusBox.setFill(Color.FIREBRICK); |
|
|
|
} |
|
|
|
|