Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package static_approach;
- import java.util.Random;
- import java.util.Scanner;
- public class hangman_static {
- static Scanner keyboard = new Scanner(System.in);
- static Random rand = new Random();
- // determines if character c is inside String str. True returned if
- // inside. call isIn inside printCurrStatus
- // see comments in hint for printCurrStatus
- static boolean isIn(char c, String str)
- {
- int length = str.length();
- boolean bool = false;
- for(int i = 0; i < length; i++)
- {
- if(c == str.charAt(i))
- return true;
- else
- bool = false;
- }
- return bool;
- }
- // If userInput contains "ard" and strToGuess contains "aardvark"
- // then the routine prints output that looks like:
- // Current Status for userInputs=ard
- // a a r d _ a r _
- // this routine returns true if all letters are guessed, otherwise
- // false is returned
- // HINT: It is useful to have for loop that goes through each of char
- // in strToGuess. Call isIn for each character
- //(note the second parameter would be userInputs)
- // If isIn returns true, print out character, if isIn
- // returns false then print out '_'
- // you can use variable like:
- // boolean success = true;
- // Whenever you output atleast one '_' you can set success = false;
- // code can return variable success and it will return true if
- // the user has picked all letters
- static boolean printCurrStatus(String strToGuess, String userInputs)
- {
- boolean success = false, is = true;
- int length = strToGuess.length();
- System.out.print("Current Status for userInputs=" + userInputs);
- System.out.println("");
- for (int i = 0; i < length; i++)
- {
- success = isIn(strToGuess.charAt(i), userInputs);
- if (success == true) {
- System.out.print(strToGuess.charAt(i));
- System.out.print(" ");
- }
- else
- {
- System.out.print("_");
- System.out.print(" ");
- is = false;
- }
- }
- return is;
- }
- // this routine returns random string from list of words
- // use array list????
- static String getNextWordToGuess()
- {
- final int num_words = 10;
- int num = rand.nextInt(num_words);
- // int num = (int)(num_words* Math.random());
- String[] words = new String[] {"elephant", "tiger", "monkey", "baboon", "barbeque",
- "giraffe", "simple", "zebra", "porcupine", "aardvark"};
- return words[num];
- }
- // this next routine plays the hangman game. it calls
- // getNextWordToGuess to
- // get the word that should be guessed. It then has a loop which
- // outputs:
- //"Enter next letter"
- // A string named userInputs stores all letters selected already
- // then the routine printCurrStatus is called to print out current
- // status of
- // the guessed word. If printCurrStatus returns true, we are done.
- static void playGame()
- {
- String userInputs = "";
- String strToGuess = getNextWordToGuess();
- while (printCurrStatus(strToGuess, userInputs) == false)
- {
- System.out.println("");
- System.out.print("Enter next letter: ");
- userInputs = userInputs + keyboard.next();
- System.out.println("");
- }
- }
- public static void main(String[] args)
- {
- String response="";
- do
- {
- playGame();
- System.out.println("");
- System.out.println("");
- System.out.println("Congratulations! You guessed the word!");
- System.out.print("Do you want to play Static Approach Hangman again? (y or n): ");
- response = keyboard.next();
- } while (response.charAt(0) == 'y');
- System.out.println("Bye");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement