import java.util.*; public class HangMan2 { char guess; String userInputs; String word; static String[] userInput; static Scanner scan = new Scanner(System.in); static Random rand = new Random();
// The following routine will determin if the character c // is inside the String str. A true is returned if it is inside. static boolean isIn(char c, String str) { if(str.length()==0) return false; for(int i=0;i<str.length();i++) if (c==str.charAt(i)); return true; } // 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. static boolean printCurrStatus(String strToGuess, String userInputs) { String status=""; for (int i1=0;i1<strToGuess.length();i1++) { status+="_"; } System.out.println(status); for (int i2=0;i2<strToGuess.length();i2++) { for (int i3=0;i3<userInputs.length();i3++) { char c=userInputs.charAt(i3); if (userInputs.charAt(i3)==strToGuess.charAt(i2)) { int len =status.length(); String substr=status.substring(0,i2); substr+=c; substr+=status.substring(i2+1,len); status=substr; System.out.println(status); } } }
//** Fill in Details
if (status.equals(strToGuess)) return true; else return false; }
// The following routine will return a random String from the list of words: // elephant, tiger, monkey, baboon, barbeque, giraffe, simple, zebra, // porcupine, aardvark static String getNextWordToGuess() { Random generator = new Random(); String[] array = { "aardvark","porcupine","zebra","simple","giraffe","barbeque","baboon", "monkey","tiger","elephant"}; int rnd = generator.nextInt(array.length); return array[rnd]; //********** Fill in Details // HINT: a switch statement can be quite useful here } // 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. static void playGame() { String word = HangMan2.getNextWordToGuess(); boolean current=true; String userInputs; do{ System.out.println("Hello, and welcome to Hangman!"); System.out.println("Enter next letter"); userInputs= scan.next(); current= HangMan2.printCurrStatus(word,userInputs); int l=word.length(); String [] userInput=new String[l-1]; userInput[l-1]=userInputs; System.out.println(userInput[l-1]); }while(printCurrStatus(word, userInputs)==false); } //********** Fill in Details // 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) { playGame(); String retry_ans; boolean retry= false; do{ System.out.println("Would you like to try again?"); retry_ans = scan.next(); if(retry_ans.equalsIgnoreCase("yes")) { retry = true; playGame(); } }while(retry = true); } }
No one really......???????
Ads