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.4 KiB

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