Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package object_oriented;
- import java.util.*;
- public class Hangman
- {
- Scanner keyboard = new Scanner(System.in);
- 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.
- boolean checkGuess(String strToGuess, String playerGuess)
- {
- if (strToGuess.equals(playerGuess))
- {
- return true;
- }
- else return false;
- }
- // ****** printCurrStatus
- // If userInputs contains "ard" and strToGuess contains "aardvark" then
- // the following routine prints out an output that looks something like:
- //
- // Current Status for userInpts=ard
- // a a r d _ a r _
- // This routine returns true if all letters were guessed, otherwise false is returned.
- // 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). If isIn returns true, just print out the character, if isIn
- // returns false, then print out '_'.
- // Additionally, you can have a variable like:
- // boolean success = true;
- // Whenever you output at least one '_', you can set success = false.
- // Your code can just return the variable "success" and it will return true if
- // the user has picked all of the letters.
- String printCurrStatus(String strToGuess, String userInputs)
- {
- char c;
- char[] output = new char[strToGuess.length()];
- for(int k = 0; k < strToGuess.length(); k++)
- {
- output[k] = '_';
- }
- for(int j = 0; j < userInputs.length(); j++)
- {
- c = userInputs.charAt(j);
- for (int i = -1; (i = strToGuess.indexOf(c, i + 1)) != -1; i++)
- {
- output[i] = c;
- }
- }
- String playerGuess = new String(output);
- return playerGuess;
- }
- // The following routine will return a random String from the list of words:
- // elephant, tiger, monkey, baboon, barbeque, giraffe, simple, zebra,
- // porcupine, aardvark
- String getNextWordToGuess()
- {
- final int num_words=10; // change this if you have a different number of words
- int num = rand.nextInt(num_words);
- // Another way to accomplish the same thing:
- // int num = (int)(num_words* Math.random());
- //********** Fill in Details
- String word = null;
- switch(num)
- {
- case 1: word = "elephant"; break;
- case 2: word = "tiger"; break;
- case 3: word = "monkey"; break;
- case 4: word = "baboon"; break;
- case 5: word = "giraffe"; break;
- case 6: word = "simple"; break;
- case 7: word = "zebra"; break;
- case 8: word = "porcupine"; break;
- case 9: word = "aardvark"; break;
- case 10: word = "banana"; break;
- }
- word = "check";
- return word;
- }
- // 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.
- // Then the routine printCurrStatus is called to print out the current status of
- // the guessed word. If printCurrStatus returns true, we are done.
- void playGame()
- {
- boolean playing = true;
- String word = null, userInputs = "_", guessed = null;
- word = getNextWordToGuess();
- while(playing)
- {
- if(checkGuess(word, guessed))
- {
- break;
- }
- guessed = printCurrStatus(word, userInputs);
- System.out.println(guessed);
- userInputs += keyboard.nextLine();
- }
- }
- // main will call playGame to play the hangman game.
- // Then main will issue the prompt:
- // Do you want to play again (y or n)
- // If the answer is "y", then call playGame again, otherwise exit
- public static void main(String[] args)
- {
- Hangman hangman = new Hangman();
- String response="";
- do
- {
- hangman.playGame();
- System.out.print("Do you want to play object oriented Hangman again? (y or n): ");
- response = hangman.keyboard.next();
- } while (response.charAt(0) == 'y');
- System.out.println("Bye");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement