Guest User

Untitled

a guest
May 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. /*
  2. * File: Hangman.java
  3. * ------------------
  4. * This program will eventually play the Hangman game from
  5. * Assignment #4.
  6. */
  7.  
  8. import acm.graphics.*;
  9. import acm.program.*;
  10. import acm.util.*;
  11.  
  12. import java.awt.*;
  13.  
  14. public class Hangman extends ConsoleProgram {
  15.  
  16. private RandomGenerator rgen = RandomGenerator.getInstance();
  17. HangmanLexicon lexicon = new HangmanLexicon();
  18.  
  19. private int numberOfGuesses = 8;
  20. private String result;
  21. private String word;
  22.  
  23.  
  24. public void run() {
  25. println("Welcome to Hangman!");
  26. generateRandomWord();
  27. }
  28.  
  29. private void generateRandomWord() {
  30. int random = rgen.nextInt(0, lexicon.getWordCount() - 1);
  31. word = lexicon.getWord(random);
  32. println(word);
  33. result = "";
  34. int length = word.length();
  35. for(int i = 0; i < length; i++) {
  36. result = result + "-";
  37. }
  38. println("The word now looks like this: " + result);
  39. println("You have " + numberOfGuesses + " guesses left." );
  40. guessWords(length);
  41. }
  42.  
  43. private void guessWords(int length) {
  44. while (true) {
  45. int counterWin = 0;
  46. int counterLoss = 0;
  47. if (! result.equalsIgnoreCase(word) && numberOfGuesses > 0) {
  48. String input = readLine("Your guess: ");
  49. for (int i = 0; i < length; i++) {
  50. String currentLetter = word.substring(i, i + 1);
  51. int charIndex = word.indexOf(currentLetter);
  52. if (currentLetter.equalsIgnoreCase(input)) {
  53. counterWin = i;
  54. displayGoodMessage(counterWin, charIndex);
  55. println("That guess is correct.");
  56. if (result.equalsIgnoreCase(word)) {
  57. println("You guessed the word: " + word);
  58. println("You win.");
  59. } else {
  60. println("The word now looks like this: " + result);
  61. println("You have " + numberOfGuesses + " guesses left.");
  62. }
  63. } else {
  64. counterLoss++;
  65. }
  66. if (counterLoss == word.length()) {
  67. numberOfGuesses--;
  68. println("There are no " + input.toUpperCase() + "'s in the word.");
  69. if (numberOfGuesses == 0) {
  70. println("You're completely hung.");
  71. println("The word was: " + word);
  72. println("You lose.");
  73. } else {
  74. println("The word now looks like this: " + result);
  75. println("You have " + numberOfGuesses + " guesses left.");
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82.  
  83. private String displayGoodMessage(int counterWin, int charIndex) {
  84. char letterCorrect = word.charAt(counterWin);
  85. for (int i = 0; i < word.length(); i++) {
  86. if (i == charIndex) {
  87. result = result.substring(0, i) + letterCorrect + result.substring(i + 1, word.length());
  88. }
  89. }
  90. return result;
  91. }
  92. }
Add Comment
Please, Sign In to add comment