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.

267 lines
7.4 KiB

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