Advertisement
letsdoitjava

Hangman

Jul 6th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. // Hangman, this is a game for a user to guess a word one letter at a time.  Five tries, 8 words, and asks if the user wants to quit before exiting.
  2. // Renee Waggoner
  3. // July 8, 2019
  4. // Special Requirements: None
  5. package static_approach;
  6. import java.util.Random;
  7. import java.util.Scanner;
  8. public class Hangman {
  9.     static Scanner keyboard = new Scanner(System.in);
  10.     static Random rand = new Random();
  11.  
  12.     // The following routine will determine if the character c
  13.     // is inside the String str. A true is returned if it is inside.
  14.     // It is very useful to call the isIn routine inside of printCurrStatus ...
  15.     // See the comments in the Hint for printCurrStatus.
  16.  
  17.     static boolean isIn(char c, String str) {
  18.         for (char currentStatus : str.toCharArray())
  19.         {
  20.             if (currentStatus == c)
  21.             {
  22.                 return true;
  23.             }
  24.         }
  25.         return false;
  26.     }
  27.     // ****** printCurrStatus
  28.     // This routine returns true if all letters were guessed, otherwise false is
  29.     // returned.
  30.     public static boolean printCurrStatus(String strToGuess, String userInputs) {
  31.         String printToConsole = "";
  32.         //If isIn returns true, just print out the character, if
  33.         // isIn
  34.         // returns false, then print out '_'.
  35.         // Your code can just return the variable "success" and it will return true
  36.         // if the user has picked all of the letters.
  37.         boolean success = true;
  38.         // HINT: It is useful to have a for loop that goes through each of the characters in
  39.         // strToGuess. Call isIn for each character (note the second parameter would
  40.         // be userInputs).
  41.         for (char word : strToGuess.toCharArray()) {
  42.  
  43.             if (isIn(word, userInputs)) {
  44.                 printToConsole += word + " ";
  45.             } else {
  46.                 // Whenever you output at least one '_', you can set success = false.
  47.                 printToConsole += "_ ";
  48.                 success = false;
  49.             }
  50.         }
  51.         System.out.println(printToConsole);
  52.         return success;
  53.     }
  54.     private static String guess = "";
  55.     private static String[] wordlist = {"byte", "short", "int", "long", "float", "double", "boolean", "char"};
  56.  
  57.     // the following routine will return a random String from my list of words
  58.  
  59.     static String getNextWordToGuess()
  60.     {
  61.         final int num_words = 8; // change this if you have a different number of words
  62.         int num = rand.nextInt(num_words);
  63.         return wordlist[num];
  64.  
  65.         // Another way to accomplish the same thing:
  66.         // int num = (int)(num_words* Math.random());
  67.     }
  68.  
  69.     // The following routine plays the hangman game. It calls getNextWordToGuess
  70.     // to
  71.     // get the word that should be guessed. It then has a loop which outputs the
  72.     // following prompt:
  73.     // Enter next letter
  74.     //
  75.     // A String named userInputs stores all letters selected already.
  76.     static void playGame()
  77.     {
  78.         guess = "";
  79.         String currentWord = getNextWordToGuess();
  80.         boolean notSolved = false;
  81.         do
  82.         {
  83.             String storedInput = "Current Status for userInputs = ";
  84.  
  85.             for (char guessStatus : guess.toCharArray())
  86.             {
  87.                 storedInput += guessStatus;
  88.             }
  89.             System.out.println(storedInput);
  90.             // Then the routine printCurrStatus is called to print out the current
  91.             // status of
  92.             // the guessed word. If printCurrStatus returns true, we are done.
  93.             notSolved = printCurrStatus(currentWord, guess);
  94.             if (!notSolved)
  95.             {
  96.                 System.out.print("Enter next letter: ");
  97.                 String guessedWord = keyboard.next();
  98.                 char guessedWordChar = guessedWord.toCharArray()[0];
  99.                 guess += guessedWordChar;
  100.                 System.out.println();
  101.             }
  102.  
  103.         }while(!notSolved);
  104.         System.out.println("Congratulations: you guessed the word!!");
  105.     }
  106.     // main will call playGame to play the hangman game.
  107.     // Then main will issue the prompt:
  108.     public static void main(String[] args) {
  109.         String response = "";
  110.         Hangman hangman = new Hangman();
  111.         do {
  112.             Hangman.playGame();
  113.             System.out.print("Do you want to play object oriented Hangman again? (y or n): ");
  114.             response = Hangman.keyboard.next();
  115.             // If the answer is "y", then call playGame again, otherwise exit
  116.         } while (response.charAt(0) == 'y');
  117.         System.out.println("Bye");
  118.         System.out.println();
  119.         keyboard.close();
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement