Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- // Renee Waggoner
- // July 8, 2019
- // Special Requirements: None
- package static_approach;
- import java.util.Random;
- import java.util.Scanner;
- public class Hangman {
- static Scanner keyboard = new Scanner(System.in);
- static Random rand = new Random();
- // The following routine will determine if the character c
- // is inside the String str. A true is returned if it is inside.
- // It is very useful to call the isIn routine inside of printCurrStatus ...
- // See the comments in the Hint for printCurrStatus.
- static boolean isIn(char c, String str) {
- for (char currentStatus : str.toCharArray())
- {
- if (currentStatus == c)
- {
- return true;
- }
- }
- return false;
- }
- // ****** printCurrStatus
- // This routine returns true if all letters were guessed, otherwise false is
- // returned.
- public static boolean printCurrStatus(String strToGuess, String userInputs) {
- String printToConsole = "";
- //If isIn returns true, just print out the character, if
- // isIn
- // returns false, then print out '_'.
- // Your code can just return the variable "success" and it will return true
- // if the user has picked all of the letters.
- boolean success = true;
- // HINT: It is useful to have a for loop that goes through each of the characters in
- // strToGuess. Call isIn for each character (note the second parameter would
- // be userInputs).
- for (char word : strToGuess.toCharArray()) {
- if (isIn(word, userInputs)) {
- printToConsole += word + " ";
- } else {
- // Whenever you output at least one '_', you can set success = false.
- printToConsole += "_ ";
- success = false;
- }
- }
- System.out.println(printToConsole);
- return success;
- }
- private static String guess = "";
- private static String[] wordlist = {"byte", "short", "int", "long", "float", "double", "boolean", "char"};
- // the following routine will return a random String from my list of words
- static String getNextWordToGuess()
- {
- final int num_words = 8; // change this if you have a different number of words
- int num = rand.nextInt(num_words);
- return wordlist[num];
- // Another way to accomplish the same thing:
- // int num = (int)(num_words* Math.random());
- }
- // The following routine plays the hangman game. It calls getNextWordToGuess
- // to
- // get the word that should be guessed. It then has a loop which outputs the
- // following prompt:
- // Enter next letter
- //
- // A String named userInputs stores all letters selected already.
- static void playGame()
- {
- guess = "";
- String currentWord = getNextWordToGuess();
- boolean notSolved = false;
- do
- {
- String storedInput = "Current Status for userInputs = ";
- for (char guessStatus : guess.toCharArray())
- {
- storedInput += guessStatus;
- }
- System.out.println(storedInput);
- // Then the routine printCurrStatus is called to print out the current
- // status of
- // the guessed word. If printCurrStatus returns true, we are done.
- notSolved = printCurrStatus(currentWord, guess);
- if (!notSolved)
- {
- System.out.print("Enter next letter: ");
- String guessedWord = keyboard.next();
- char guessedWordChar = guessedWord.toCharArray()[0];
- guess += guessedWordChar;
- System.out.println();
- }
- }while(!notSolved);
- System.out.println("Congratulations: you guessed the word!!");
- }
- // main will call playGame to play the hangman game.
- // Then main will issue the prompt:
- public static void main(String[] args) {
- String response = "";
- Hangman hangman = new Hangman();
- do {
- Hangman.playGame();
- System.out.print("Do you want to play object oriented Hangman again? (y or n): ");
- response = Hangman.keyboard.next();
- // If the answer is "y", then call playGame again, otherwise exit
- } while (response.charAt(0) == 'y');
- System.out.println("Bye");
- System.out.println();
- keyboard.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement