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.

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