Advertisement
Guest User

ESHKEEEIYIITITITITTTT

a guest
Jul 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1.  
  2. // TODO: comment this file
  3.  
  4. import acm.program.*;
  5. import acm.util.*;
  6. import java.io.*; // for File
  7. import java.util.*; // for Scanner
  8.  
  9. public class Snowman extends SnowmanProgram {
  10.  
  11. public void run() {
  12. int winCount = 0;
  13. int gameCount = 0;
  14. int bestGuessGame = 0;
  15. intro();
  16. String file = promptUserForFile("Directory filename? ", "res");
  17. boolean play = true;
  18. while (play) {
  19. int remainingGuesses = playOneGame(getRandomWord(file));
  20. if (remainingGuesses > 0) {
  21. winCount++;
  22. }
  23. gameCount++;
  24. bestGuessGame = bestGame(remainingGuesses, bestGuessGame);
  25. if (readBoolean("Play again (Y/N)?", "y", "n") == false) {
  26. play = false;
  27. }
  28. }
  29. stats(gameCount, winCount, bestGuessGame);
  30. }
  31.  
  32. /*
  33. * This method prints an introductory message to the snowman game
  34. * Post: An introductory message will be displayed.
  35. */
  36. private void intro() {
  37. println("CS106A Snowman!");
  38. println("I will think of a random word.");
  39. println("You'll try to guess its letters.");
  40. println("Every time you guess a letter");
  41. println("that isn't in my word, a new");
  42. println("piece of the snowman appears.");
  43. println("Guess correctly to avoid");
  44. println("bringing him to life in the sun!");
  45. println();
  46. }
  47.  
  48. // TODO: comment this method
  49. private int playOneGame(String secretWord) {
  50. boolean keepPlaying = true;
  51. boolean win = false;
  52. boolean lose = false;
  53. String guessedLetters = "";
  54. int guessCount = 0;
  55. boolean letterInWord = false;
  56. displaySnowman(guessCount);
  57. println("#######");
  58. while (keepPlaying) {
  59. println("Secret Word : " + createHint(secretWord, guessedLetters));
  60. println("Your Guesses: " + guessedLetters);
  61. println("Guesses Left: " + (8 - guessCount));
  62. char guess = readGuess(guessedLetters);
  63. for (int i = 0; i < secretWord.length(); i++) {
  64. if (secretWord.charAt(i) == (guess)) {
  65. letterInWord = true;
  66. }
  67. }
  68. if (letterInWord) {
  69. guessedLetters += guess;
  70. println("Correct!");
  71. letterInWord = false;
  72. if (createHint(secretWord, guessedLetters).equals(secretWord)) {
  73. keepPlaying = false;
  74. win = true;
  75. } else {
  76. displaySnowman(guessCount);
  77. println("#######");
  78. }
  79. } else {
  80. guessedLetters += guess;
  81. guessCount++;
  82. println("Incorrect.");
  83. displaySnowman(guessCount);
  84. println("#######");
  85. if (8 - guessCount == 0) {
  86. keepPlaying = false;
  87. lose = true;
  88. }
  89. }
  90.  
  91. }
  92. if (win) {
  93. println("You win! My word was \"" + secretWord + "\".");
  94. } else if (lose) {
  95. println("You lose! My word was \"" + secretWord + "\".");
  96. }
  97. return 8 - guessCount;
  98. }
  99.  
  100. // TODO: comment this method
  101. private String createHint(String secretWord, String guessedLetters) {
  102. String hint = "";
  103. for (int i = 0; i < secretWord.length(); i++) {
  104. hint += "-";
  105. }
  106. for (int i = 0; i < guessedLetters.length(); i++) {
  107. for (int j = 0; j < secretWord.length(); j++) {
  108. if (guessedLetters.charAt(i) == secretWord.charAt(j)) {
  109. hint = hint.substring(0, j) + secretWord.charAt(j) + hint.substring(j + 1);
  110. }
  111. }
  112. }
  113. return hint;
  114. }
  115.  
  116. // TODO: comment this method
  117. private char readGuess(String guessedLetters) {
  118. String guess = readLine("Your guess? ");
  119. boolean invalidGuess = true;
  120. boolean checkGuessedLetters = true;
  121. char finalGuess = ' ';
  122. while (invalidGuess) {
  123. if (guess.length() > 1 || isNumber(guess) || guess.length() == 0) {
  124. println("Type a single letter from A-Z.");
  125. guess = readLine("Your guess? ");
  126. } else {
  127. guess = guess.toUpperCase();
  128. checkGuessedLetters = true;
  129. for (int i = 0; i < guessedLetters.length(); i++) {
  130. if (Character.toString(guessedLetters.charAt(i)).equalsIgnoreCase(guess)) {
  131. checkGuessedLetters = false;
  132. }
  133. }
  134. if (checkGuessedLetters == false) {
  135. println("You already guessed that letter.");
  136. guess = readLine("Your guess? ");
  137. } else {
  138. finalGuess = guess.charAt(0);
  139. invalidGuess = false;
  140. }
  141. }
  142. }
  143. return finalGuess;
  144.  
  145. }
  146.  
  147. /*
  148. * This method displays an different image of a snowman, depending on the amount of guesses the player has taken.
  149. * Pre: The player must incorrectly guess a letter of the secret word
  150. * Post: An image of a snowman will be displayed.
  151. */
  152. private void displaySnowman(int guessCount) {
  153. try {
  154. Scanner input = new Scanner(new File("res/display" + (8 - guessCount) + ".txt")); //opens file of needed image
  155. while (input.hasNextLine()) {
  156. String line = input.nextLine();
  157. println(line); //prints each line of the image one by one
  158. }
  159. } catch (FileNotFoundException ex) {
  160. println("Error reading the file: " + ex); //displays an error if the file is not found
  161. }
  162. }
  163.  
  164. /*
  165. * This program prints a set of statistics about the games of snowman that have been played
  166. * Pre: The player must choose not to play another game
  167. * Post: The statistics for all games played will be displayed
  168. */
  169. private void stats(int gamesCount, int gamesWon, int best) {
  170. println(" ");
  171. println("Overall statistics:");
  172. println("Games played: " + gamesCount);
  173. println("Games won: " + gamesWon);
  174. println("Win percent: " + ((double) gamesWon / gamesCount * 100) + "%");
  175. println("Best game: " + best + " guess(es) remaining");
  176. println("Thanks for playing!");
  177. }
  178.  
  179. /*
  180. * This method randomly selects and retrieves a word from a file full of words.
  181. * Pre: A valid file name is provided.
  182. * Post: A random word will be returned from the file
  183. */
  184. private String getRandomWord(String filename) {
  185. String word = " ";
  186. try {
  187. Scanner input = new Scanner(new File(filename)); //opens the file of words
  188. String digit = input.nextLine(); //stores the first line (which is a digit) in a string
  189. int randInt = Integer.parseInt(digit); //converts the digit from a string to an integer
  190. int number = RandomGenerator.getInstance().nextInt(1, randInt); //generates a random number between 1 and the number of words in the file
  191. for (int i = 1; i <= number; i++) {
  192. word = input.nextLine(); //moves to the word corresponding to the random number and stores it as a string
  193. }
  194. input.close();
  195. } catch (FileNotFoundException ex) {
  196. print("Unable to open that file. Try again."); //displays an error if the file is not found
  197. }
  198. return word;
  199. }
  200.  
  201. private boolean isNumber(String input) {
  202. try {
  203. Integer.parseInt(input);
  204. return true;
  205. } catch (NumberFormatException e) {
  206. return false;
  207. }
  208. }
  209.  
  210. private int bestGame(int guesses, int bestGuesses){
  211. int best = 0;
  212. if(guesses > bestGuesses){
  213. best = guesses;
  214. } else {
  215. best = bestGuesses;
  216. }
  217. return best;
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement