You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
7.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import javafx.application.*;
  2. import javafx.scene.*;
  3. import javafx.scene.shape.*;
  4. import javafx.scene.paint.*;
  5. import javafx.scene.layout.*;
  6. import javafx.scene.text.*;
  7. import javafx.stage.*;
  8. import javafx.scene.control.*;
  9. import javafx.scene.input.*;
  10. public class Hangman extends Application {
  11. private Dictionary dict = new Dictionary();
  12. private String secretWord;
  13. private Text[] letterGuesses;
  14. private Text[] lettersRevealedSoFar;
  15. private Shape[] hangman;
  16. private int currentHangmanPart;
  17. private boolean activeGame;
  18. public void start(Stage primaryStage) {
  19. primaryStage.setTitle("Hangman");
  20. Font gameFont = Font.font("Consolas", 22.0);
  21. Text statusBox = new Text(200, 100, "");
  22. statusBox.setFont(gameFont);
  23. Pane main = this.setUpLayout(statusBox);
  24. Scene scene = new Scene(main, 400, 500);
  25. Button resetButton = new Button("New Game");
  26. resetButton.setDefaultButton(true);
  27. resetButton.relocate(25,350);
  28. main.getChildren().add(resetButton);
  29. resetButton.setOnAction(
  30. e -> {
  31. Pane newPane = setUpLayout(statusBox);
  32. newPane.getChildren().add(resetButton);
  33. scene.setRoot(newPane);
  34. });
  35. scene.setOnKeyPressed(
  36. e -> {
  37. if (activeGame) {
  38. String guess = e.getText();
  39. guess = guess.toLowerCase();//use a string method to convert guess to lowercase
  40. checkGuess(guess, statusBox);
  41. }
  42. });
  43. primaryStage.setScene(scene);
  44. primaryStage.show();
  45. }
  46. private Pane setUpLayout(Text statusBox) {
  47. Pane main = new Pane();
  48. Font gameFont = Font.font("Consolas", 22.0);
  49. //set new word to guess
  50. this.secretWord = dict.getRandomWord();
  51. GridPane word = new GridPane();
  52. word.setHgap(5);
  53. this.currentHangmanPart = 0;
  54. GridPane lettersPane = new GridPane();
  55. lettersPane.relocate(25, 430);
  56. lettersPane.setHgap(5);
  57. lettersPane.setVgap(5);
  58. //create letterGuesses
  59. this.letterGuesses = new Text[26];// finish this line
  60. for (int i = 0; i < 26; i++) {
  61. String temp = "l";//finish this line
  62. this.letterGuesses[i] = new Text(temp);
  63. letterGuesses[i].setFont(gameFont);
  64. lettersPane.add(letterGuesses[i], i % 13, i / 13);
  65. }
  66. //set up lettersRevealedSoFar
  67. this.lettersRevealedSoFar = new Text[this.secretWord.length()];
  68. for (int i = 0; i < lettersRevealedSoFar.length; i++) {
  69. this.lettersRevealedSoFar[i] = new Text("_");
  70. this.lettersRevealedSoFar[i].setFont(gameFont);
  71. word.add(this.lettersRevealedSoFar[i], i, 0);
  72. }
  73. //set up the gallows
  74. Pane figure = new Pane();
  75. Shape[] gallows = { new Line(120, 40, 120, 10),
  76. new Line(120, 10, 40, 10),
  77. new Line(40, 10, 40, 250),
  78. new Rectangle(150, 7) };
  79. gallows[3].relocate(20, 250);
  80. for (int i = 0; i < gallows.length; i++) {
  81. gallows[i].setStroke(Color.BLACK);
  82. gallows[i].setStrokeWidth(2.0);
  83. figure.getChildren().add(gallows[i]);
  84. }
  85. //set up the hangman outline
  86. this.hangman = new Shape[6];
  87. this.hangman[0] = new Circle(120, 60, 20, Color.WHITE); // head
  88. this.hangman[1] = new Line(120, 80, 120, 150); // body
  89. this.hangman[2] = new Line(120, 110, 80, 90); // left arm
  90. this.hangman[3] = new Line(120, 110, 160, 90); // right arm
  91. this.hangman[4] = new Line(120, 150, 100, 200); // left leg
  92. this.hangman[5] = new Line(120, 150, 140, 200); // right leg
  93. for (int i = 0; i < this.hangman.length; i++) {
  94. this.hangman[i].setStroke(Color.WHITE);
  95. this.hangman[i].setStrokeWidth(3.0);
  96. figure.getChildren().add(this.hangman[i]);
  97. }
  98. //set up the alphabet
  99. Text title = new Text(25, 420, "Guesses so far:");
  100. //ready to play
  101. this.activeGame = true;
  102. statusBox.setText("Ready to play");
  103. statusBox.setFill(Color.BLUE);
  104. //place everything in window
  105. word.relocate(20, 300);
  106. main.getChildren().add(word);
  107. main.getChildren().add(figure);
  108. main.getChildren().add(title);
  109. main.getChildren().add(lettersPane);
  110. main.getChildren().add(statusBox);
  111. return main;
  112. }
  113. private void checkGuess(String s, Text statusBox) {
  114. int index = this.findLetter(s);
  115. if (!alreadyGuessed(index)) {
  116. boolean found = foundInSecretWord(s);
  117. if (found) /* correct guess */ {
  118. this.letterGuesses[index].setFill(Color.GREEN);
  119. checkIfWon(s, statusBox);
  120. }
  121. else /* incorrect guess */ {
  122. this.letterGuesses[index].setFill(Color.RED);
  123. checkIfLost(s, statusBox);
  124. }
  125. }
  126. }
  127. private int findLetter(String s) {
  128. int index = Dictionary.ALPHABET.indexOf(s);
  129. boolean isLetter = (index != -1) && (s.length() == 1);
  130. //Determine if s contains a single letter of the alphabet, using String methods
  131. //Return the index of this letter in the array letterGuesses
  132. //e.g. "a" is stored in index 0, "b" in index 1, etc.
  133. //If the string is not a letter, return -1
  134. int ret = -1;
  135. if (isLetter) {
  136. for (int i = 0; i < this.letterGuesses.length; i ++) {
  137. if (this.letterGuesses[i].getText() == s) {
  138. ret = i;
  139. break;
  140. }
  141. }
  142. }
  143. return ret;
  144. //write your code for this method
  145. }
  146. private boolean alreadyGuessed(int index) {
  147. if (index >= 0) {
  148. Color currColor = (Color)this.letterGuesses[index].getFill();
  149. if (currColor.equals(Color.BLACK)) {
  150. return false;
  151. }
  152. else {
  153. return true;
  154. }
  155. }
  156. return true;
  157. }
  158. private boolean foundInSecretWord(String s) {
  159. //Determine if s is a correct or incorrect guess
  160. //Return true if s corresponds to a letter in the secret word, and return false otherwise
  161. //write your code for this method
  162. boolean inSecretWord = this.secretWord.indexOf(s) != -1;
  163. boolean guessed = false;
  164. int i = 0;
  165. while (!guessed && i < this.lettersRevealedSoFar.length) {
  166. guessed = this.lettersRevealedSoFar[i].getText() == s;
  167. i += 1;
  168. }
  169. return inSecretWord && !guessed;
  170. }
  171. private void checkIfWon(String s, Text statusBox) {
  172. //write your code here
  173. boolean won = true;
  174. int i = 0;
  175. while (won && i < this.lettersRevealedSoFar.length) {
  176. won = lettersRevealedSoFar[i].getText() != "_";
  177. i += 1;
  178. }
  179. if (won) {
  180. statusBox.setText("YOU WIN!!!");
  181. statusBox.setFill(Color.GREEN);
  182. this.activeGame = false;
  183. }
  184. else {
  185. statusBox.setText("Letter " + s + " is \ncorrect!");
  186. statusBox.setFill(Color.FORESTGREEN);
  187. }
  188. }
  189. private void checkIfLost(String s, Text statusBox) {
  190. if (this.currentHangmanPart < 6) {
  191. this.hangman[this.currentHangmanPart].setStroke(Color.BLACK);
  192. this.currentHangmanPart++;
  193. statusBox.setText("Letter " + s + " is \nnot correct.");
  194. statusBox.setFill(Color.FIREBRICK);
  195. }
  196. else {
  197. for (int i = 0; i < this.lettersRevealedSoFar.length; i++) {
  198. this.lettersRevealedSoFar[i].setText(this.secretWord.charAt(i)+"");
  199. this.lettersRevealedSoFar[i].setFill(Color.FIREBRICK);
  200. }
  201. statusBox.setText("You lost. :(");
  202. statusBox.setFill(Color.RED);
  203. this.activeGame = false;
  204. }
  205. }
  206. public static void main(String[] args) {
  207. launch(args);
  208. }
  209. }