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.

248 lines
7.8 KiB

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