/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package lab5hangmankey; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Lab5HangmanKey { /** This is a hangman style word game. * The program chooses a word from its list of words. * The user makes a guess of a letter. If the letter * is in the word, the user gets to see where the letter(s) * are in the word. If the letter is not in the word, * the parts of the hangman start appearing. If the * whole hangman appears before the word is guessed, * the user loses. If the word is completed before * the hangman, then the user wins. * @param args the command line arguments */ public static void main(String[] args) { int numLetters = 30; // 1.c) changed ;Change to match words/phrases in MakeWordArray int numberOfWordsToGuess = 30;// 1.d) changed ;Change to match words in MakeWordArray String wordToGuess; char[] guesses = new char[numLetters]; // all user guesses stored here char[] correctGuess = new char[numLetters]; // building the word from the guesses // 1.a) Declare a 2D array of at least 30 rows and 30 columns to // hold the words that can be guessed by the user. Just // declare the array here and initialize its size. char[][] wordArray = new char [numberOfWordsToGuess][numLetters]; // 1.a) answer char guess = ' '; String printGuess; int count = 0; int wrongCount = 0; int inWord = 0; int indexWord = 0; int noBlank = 0; int r = 29; boolean done = false; boolean guessed = true; String again = "Y"; Scanner input = new Scanner(System.in); /* This is a hangman style game */ // 1.e) call MakeWordArray method to put words into 2D array makeWordArray(wordArray); // 1.e) answer /* Choose a word */ while (again.equalsIgnoreCase("Y")) { r = (int)(Math.random() * numberOfWordsToGuess); wordToGuess = selectWord(r, wordArray);// 2.b) changed ;pass in array as second parameter //System.out.println("DEBUG: Random number was "+r+" giving word: **"+wordToGuess+"**"); System.out.println("\n\n+----------------------+"); System.out.println("| Welcome to the game! |"); System.out.println("+----------------------+"); System.out.println("Enter letters to guess the word. Get done before the doll is hung!\n"); for (int i= 0; i < numLetters; i++) { guesses[i] = ' '; correctGuess[i] = ' '; } System.out.println("The word you are guessing has "+wordToGuess.length() + " letters."); // Alternate way to show this System.out.print("\n** "); for (int n = 0; n < wordToGuess.length(); n++) { System.out.print("__ "); } System.out.println("** "); done = false; count = 0; indexWord = 0; wrongCount = 0; while (!done) { // 6) Give the user an indication of the number of letters in the // selected word. You can print blanks or you can just tell how // many letters are in the word or do something else to give this info. System.out.print("\nPlease enter a lowercase letter for your guess: "); // 3.d) What happens if the user guesses a letter they have already used? // 3.e) Indicate to the user if they guess a letter they already used. // method getGuess solves 3.d & e) guesses[count] = getGuess(input, numLetters, guesses); //* // Is the letter in the selected word? // inWord = 0; if (wordToGuess.indexOf(guesses[count]) == -1) { // The letter is not in the word if ((wrongCount = hangDoll(wrongCount)) > 7) // 3.a) Why does this check > 7? { done = true; System.out.println("\nSo sorry. You lose."); } } else { // 3.b) Does this section correctly record the guesses? Why or why not? // 3.c) If needed, correct this section to record all guesses. // 3.b & c) do-while loop solves this problem indexWord = wordToGuess.indexOf(guesses[count], inWord); do { correctGuess[indexWord] = guesses[count]; inWord = indexWord + 1; indexWord = wordToGuess.indexOf(guesses[count], inWord); } while (indexWord != -1); printGuess = String.valueOf(correctGuess); //noBlank = printGuess.indexOf(' '); printGuess = printGuess.substring(0, wordToGuess.length()); //System.out.println("**"+printGuess+"**"); printGuesses(printGuess, wordToGuess); if (wordToGuess.equalsIgnoreCase(printGuess)) { done = true; System.out.println("\nGreat! You win!"); System.out.println("You are a "+ wordToGuess +" student!"); } } count++; } input.nextLine(); System.out.print("\nWould you like to play another game of HangDoll? Y/N: "); again = input.nextLine(); //System.out.println("DEBUG: User entered **"+again+"**"); again = again.substring(0,1); } // 4.a) Modify the game so that the user can play multiple times if desired. System.out.println("\nThank you for playing!"); } /** 1.b) MakeWordArray is a method that takes in a 2 dimensional array and * fills it with words that the player will guess. * Use the multi-dimensional array that is passed in which * must have at least 30 rows and 30 columns. Assign the * following words to the array and add words or phrases * so that there are at least 20 positive words to choose * from that could go in the sentence shown below. Words/phrases * should be of length 6 or more "achieving" "successful" "happy" "excited" "beneficial" "powerful" "diligent" "exceptional" "terrific" "outstanding" Your additional words/phrases must positively complete the following sentence: "You are a _______ student!" */ public static void makeWordArray(char[][] wordArray) { /* String[] words = {"achieving","successful","happy","excited", "beneficial","powerful","diligent","exceptional","terrific", "outstanding", "superior","prepared","intelligent","competent", "fantastic","great","joyous","killer","laughing","masterful", "neighborly","quick","righteous","unstoppable","virtuous", "wicked","X! (Roman numeral 10)","young-at-heart","zealous"}; int w = words.length; */ int l = wordArray.length; int limit = wordArray[0].length; int aWord; String newWord = ""; boolean wordsExist = true; File wordFile = new File("words.txt"); Scanner words; try { words = new Scanner(wordFile); } catch (FileNotFoundException fnf) { words = new Scanner(System.in); wordsExist = false; } /* if (l > w) { limit = w; } else { limit = l; } */ if (wordsExist) { for (int i = 0; (i < l) && (words.hasNextLine()); i++) { //aWord = words[i].length() - 1; // for using the array newWord = words.nextLine(); aWord = newWord.length()-1; //System.out.println("DEBUG: words["+i+"] is "+words[i]+" and aWord length is "+aWord); //System.out.println("DEBUG: New word from file is "+newWord); for (int n = limit-1; n >= 0; n--) { if (n == aWord) { wordArray[i][n] = newWord.charAt(n); //System.out.println("DEBUG: wordArray["+i+"]["+n+"] is "+wordArray[i][n]); aWord--; } else { wordArray[i][n] = ' '; } } } } for (int k = limit; k < l; k++) { for (int j = 0; j < wordArray.length; j++) { (wordArray[k][j])= ' '; } } // Error checking /* for (int i = 0; i < wordArray.length; i++) { System.out.print("DEBUG: wordArray["+i+"] letters are "); for (int j = 0; j < wordArray.length; j++) { System.out.print((wordArray[i][j])); } System.out.println(""); } */ for (int i = 0; i < wordArray.length; i++) { System.out.println("DEBUG: " //+ "wordArray["+i+"] is \""+String.valueOf(wordArray[i]) +"\" and trimmed string is \""+String.valueOf(wordArray[i]).trim() +"\" with length of "+String.valueOf(wordArray[i]).trim().length()); System.out.println("\n"); } } /** Select a word from the possible words * in the program. A random number is sent into * the method and a string is returned. * @param roll - a random number */ public static String selectWord(int roll, char[][] wordArray ) // done; 2.a)add the word array as parameter { // 2.c) Select the row in the word array // indicated by 'roll' to be the chosen // word for the game. Turn the array into // a string and return the string of the word. // 2.c) complete //System.out.println("DEBUG: roll is "+roll+" in selectWord"); String word = String.valueOf(wordArray[roll]).trim(); while (word.length() == 0) { roll = (int)(Math.random() * wordArray.length); //System.out.println("DEBUG: roll AGAIN is "+roll+" in selectWord"); word = String.valueOf(wordArray[roll]).trim(); } //System.out.println("DEBUG: Selected word from array is "+ word+"\n"); return word; } /** This method draws the hangman based on how * many incorrect guesses the user has made. * ---+ * o * /@\ * / \ * @param guess - a counter */ public static int hangDoll(int guess) { System.out.println(""); System.out.printf("%10s%2s%4s\n"," "," |",(guess>=1?"---+":" ")); System.out.printf("%10s%2s%4s\n"," "," |",(guess>=2?" o":" ")); System.out.printf("%10s%2s%3s%1s%1s\n"," "," |", (guess>=3?" /":" "),(guess>=4?"@":" "),(guess>=5?"\\":" ")); System.out.printf("%10s%2s%3s%2s\n"," "," |", (guess>=6?" /":" "),(guess>=7?" \\":" ")); System.out.printf("%10s%2s\n"," "," | "); System.out.printf("%10s%2s\n"," "," | "); System.out.printf("%10s%13s\n"," ","+----------+"); System.out.printf("%10s%13s\n"," ","| |"); System.out.printf("%10s%13s\n"," ","+----------+"); return ++guess; // 5. What happens if ++guess is changed to guess++? // Describe what happens and explain why. } public static char getGuess(Scanner input, int numLetters, char[] guesses) { boolean guessed = true; char guess = ' '; while (guessed) { guessed = false; // reset the flag guess = input.next().charAt(0); System.out.println(""); for (int c = 0; c < numLetters; c++) { if (guess == guesses[c]) { guessed = true; } } if (guessed) { System.out.print("You have already guessed this letter. Please guess again: "); } } return guess; } public static void printGuesses(String soFar, String wordToGuess) { System.out.print("\n** "); for (int n = 0; n < wordToGuess.length(); n++) { if (soFar.charAt(n) == ' ') { System.out.print("__ "); } else { System.out.print(soFar.charAt(n)+" "); } } System.out.println("** "); } }