Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.    
  18.     private String orig;
  19.     private String hidden;
  20.     private String userInput;
  21.     private String lowerInput;
  22.     // private String missedLetters = "";  // this is not needed
  23.    
  24.     private char lastLetter;
  25.     private int guess = 8;
  26.    
  27.     //private boolean guessed = false;    //this is not needed
  28.     private boolean GameWon = false;     // added this to stop the program before 8 tries passed if all letters were guessed
  29.  
  30.     public class getWord extends HangmanLexicon {
  31.         int rand = rgen.nextInt(getWordCount());
  32.         String str = getWord(rand);
  33.     }
  34.  
  35.     getWord w = new getWord();
  36.     private String getAWord() {
  37.         return(w.str);
  38.     }
  39.  
  40.     public void run() {
  41.         console();
  42. //      graphics();
  43. //      readLexicon();
  44.     }
  45.    
  46.     private void console() {
  47.         orig = getAWord();
  48.         hidden = orig;   // assign it here, only once. Used then due the game to control win/loss
  49.         interactWithUser();
  50.     }
  51.  
  52.     private void interactWithUser() {
  53.         println("Welcome to Hangman!");
  54.         gameProcess();
  55.         gameEnd();    // changed to finish game regardless of current status. gameEnd will determine if won or lost.
  56.     }
  57.    
  58.     private void gameProcess() {
  59.         while (guess != 0 || GameWon) {   // added "or if GameWon = true" to exit the loop if won before 8 tries
  60.             println("The word now looks like this: " + hidden);
  61.             checkUserInput();
  62.             println("You have " + guess + " guesses left.");
  63.         }
  64.     }
  65.  
  66.     private void checkUserInput() {
  67.         userInput = readLine("Your guess: ");
  68.         lastLetter = userInput.charAt(0);
  69.         lowerInput = orig.toLowerCase();
  70.         if (orig.contains(userInput) || lowerInput.contains(userInput)) {
  71.             // guessed = true;   // this is not needed
  72.             updateHidden();
  73.             checkWin();
  74.         } else {
  75.             guess--;    // placed instead of guessed = false;
  76.         }
  77.         // this is not needed
  78.         //if (!guessed) {
  79.         //  guess--;
  80.         //}
  81.     }
  82.  
  83.     private void updateHidden() {
  84.         for (int i=0; i< orig.lenth(); i++) {
  85.             if (orig.charAt(i).toLowerCase() == lastLetter.toLowerCase()) {
  86.                 // match found, need to replace
  87.                 hidden.charAt(i) = orig.charAt(i);
  88.             }
  89.         }
  90.     }
  91.  
  92.     private void checkWin() {
  93.         if (!hidden.contains("-")) {
  94.             GameWon =  true;
  95.         }
  96.     }
  97.  
  98.     private void gameEnd() {    // changed from gameLost to gameEnd
  99.         if (guess == 0) {
  100.             println("You're completely hung.");
  101.             println("The word was: " + getAWord());
  102.             println("You lose.");
  103.         }
  104.         if (GameWon) {
  105.             println("Congratulations, you WON!!");
  106.             println("The word was: " + getAWord());        
  107.         }
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement