import java.io.IOException; import java.io.FileReader; // TODO 1: IMPORT THE SCANNER CLASS /** This is an incomplete hangman program with methods. You need to add the input statements and some String processing in order for it to be complete. You should also make it checkstyle compliant as you finish. */ public class Hangman { /** Parts are 1 head, 1 body, 2 arms, 2 legs. */ static final int BODYPARTS = 6; /** Main method is where the execution always begins. @param args the command-line arguments (not used) @throws IOException in case of file issues */ public static void main(String[] args) throws IOException { Scanner keyboard = new Scanner(System.in); Scanner infile = new Scanner(new FileReader("hangwords.txt")); String word, guess, letters; StringBuffer guessedword; char let; int choice; int bodyparts; boolean quit = false, gameover, found; int i; while (! quit && infile.hasNext()) { // start new game System.out.println("\nchoosing secret word..."); // TODO 2: GET THE NEXT WORD FROM INFILE, STORE IN word bodyparts = 0; gameover = false; // TODO 3: INITIALIZE letters TO BE THE EMPTY STRING guessedword = convert(word); while (! gameover) { System.out.println("word to guess: " + guessedword + " letters guessed so far: {" + letters + "}"); choice = menu(keyboard); switch (choice) { case 2: System.out.print("letter -> "); // TODO 4: READ THE INPUT FROM THE KEYBOARD AND // SET let TO THE FIRST CHARACTER IN THE INPUT // TODO 5: ADD let TO THE END OF letters found = letterCheck(let, guessedword, word); if (! found) { bodyparts++; showHangman(bodyparts); } if (bodyparts == BODYPARTS) { System.out.println("you lose!"); gameover = true; } if (word.equalsIgnoreCase(guessedword.toString())) { System.out.println("\nCongratulations, you guessed it!"); gameover = true; } break; case 3: System.out.print("word -> "); // TODO 6: SET guess TO THE USER'S KEYBOARD INPUT if (guess.equalsIgnoreCase(word)) System.out.println("\nCongratulations, you guessed it!"); else { System.out.println("\nWrong word, you lose!"); } gameover = true; break; case 4: gameover = true; break; case 1: gameover = true; quit = true; break; default: System.out.println("invalid menu choice"); } // switch over } // game over } // program over System.out.println("\nthanks for playing"); } /** Display menu and get user choice. @param keyboard the input source @throws IOException if bad input @return the choice */ public static int menu(Scanner keyboard) throws IOException { int choice; System.out.println("1) quit program "); System.out.println("2) letter guess "); System.out.println("3) word guess "); System.out.println("4) new game "); System.out.print("choice -> "); // TODO 7: SET choice TO THE NUMBER INPUT AT THE KEYBOARD return choice; } /** Creates a blanked version of a string. @param orig the original string @return a series of blanks corresponding to characters in orig */ public static StringBuffer convert(String orig) { // create w/capacity same as original string length StringBuffer result = new StringBuffer(orig.length()); for (int i=0; i < orig.length(); i++) result.append('-'); return result; } /** Checks to see if a letter is in the original, and if so, updates the blank version where it should occur. @param guess the letter to look for @param blanks the string of blanks to update @param orig the original string @return true if guess is found, false otherwise */ public static boolean letterCheck(char guess, StringBuffer blanks, String orig) { boolean found = false; for (int i=0; i < orig.length(); i++) if (guess == orig.charAt(i)) { blanks.setCharAt(i, guess); found = true; } return found; } /** Display the hangman (number of bodyparts for now) @param bodyparts the number of bodyparts on the figure */ public static void showHangman(int bodyparts) { System.out.println("your hangman has " + bodyparts + " bodyparts"); return; // optional return statement } } // class defintion end