SHARE
TWEET

Untitled

a guest Dec 8th, 2019 59 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.         // write dashes in HIDDEN only once. Used then during the game to control win/loss
  49.         hidden = orig.replaceAll(".","-");
  50.         interactWithUser();
  51.     }
  52.  
  53.     private void interactWithUser() {
  54.         println("Welcome to Hangman!");
  55.         gameProcess();
  56.         gameEnd();    // changed to finish game regardless of current status. gameEnd will determine if won or lost.
  57.     }
  58.    
  59.     private void gameProcess() {
  60.         while (guess != 0 && !GameWon) {   // added "or if GameWon = true" to exit the loop if won before 8 tries
  61.             println("The word now looks like this: " + hidden);
  62.             checkUserInput();
  63.             println("You have " + guess + " guesses left.");
  64.         }
  65.     }
  66.  
  67.     private void checkUserInput() {
  68.         userInput = readLine("Your guess: ");
  69.         lastLetter = userInput.charAt(0);
  70.         lowerInput = orig.toLowerCase();
  71.         if (hidden.contains(userInput) || hidden.toLowerCase().contains(userInput)) {
  72.         // user entered already guessed letter. Need to warn him and exit checkUserInput
  73.             println("You have already guessed this letter, are you being smart?");
  74.             return;
  75.         }
  76.  
  77.         if (orig.contains(userInput) || lowerInput.contains(userInput)) {
  78.             println("** DEBUG: input letter matches letters in the word.");
  79.             updateHidden();
  80.             checkWin();
  81.         } else {
  82.             println("** DEBUG: input letter DOES NOT MATCH letters in the word.");
  83.             guess--;    // placed instead of guessed = false;
  84.         }
  85.         // this is not needed
  86.         //if (!guessed) {
  87.         //  guess--;
  88.         //}
  89.     }
  90.  
  91.     private void updateHidden() {
  92.         for (int i=0; i< orig.lenth(); i++) {
  93.             if (Character.toLowerCase(orig.charAt(i)) == Character.toLowerCase(lastLetter)) {
  94.                 println("** DEBUG: Trying to replace matched symbol at index: " + Integer.toString(i));
  95.                 char c = orig.charAt(i);
  96.                 hidden = replaceChar(hidden, c ,  i);
  97.             }
  98.         }
  99.     }
  100.  
  101.     private String replaceChar(String str, char ch, int index) {
  102.         return str.substring(0, index) + ch + str.substring(index+1);
  103.     }
  104.  
  105.     private void checkWin() {
  106.         if (!hidden.contains("-")) {
  107.             GameWon =  true;
  108.         }
  109.     }
  110.  
  111.     private void gameEnd() {    // changed from gameLost to gameEnd
  112.         if (guess == 0) {
  113.             println("You're completely hung.");
  114.             println("The word was: " + getAWord());
  115.             println("You lose.");
  116.         }
  117.         if (GameWon) {
  118.             println("Congratulations, you WON!!");
  119.             println("The word was: " + getAWord());        
  120.         }
  121.     }
  122.  
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top