/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code16nov17hangman; /** * * @author jcmtiernan */ public class Code16Nov17Hangman { /** 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 choiceOfWords = 10; int rand = (int) (Math.random()* choiceOfWords); String selectedWord = SelectWord(rand); char[] guessedLetters = new char[30]; char userInput = 'e'; int wrongCount = 0; /* Team quiz Nov 16 Quiz - write a high-level algorithm for the actions needed to play hangman */ /* After quiz - what steps did you put? Tell the user what to do Tell number of letters the word has Ask user to enter a letter Read the user's input Store user input into a variable Extract first char from user's input */ /* After quiz - what steps did you put? Compare the input to the selected word - Break up word into letters Decide */ // is userInput one of letters in selectedWord? boolean found = false; for (int k=0; k < selectedWord.length();k++) { if (userInput == selectedWord.charAt(k)) { guessedLetters[k] = userInput; found = true; } } /* After quiz - what steps did you put? If letter is in word save letter into word being built If not in word print new part of hangman */ if (found) { // output the partial word from guessLetter } else { wrongCount++; // print out current hangman // HangDoll(wrongCount); } /* After quiz - what else do we need? Loop for more letters Way to determine if finished word or finished hangman */ } /** 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) { String temp = " "; switch(roll) { // bad Dr. T - use "returns" or temp; NOT both case 0: return "excited"; case 1: return "successful"; case 2: return "wonderful"; case 3: temp = "terrific"; break; case 4: temp = "awesome"; break; case 5: temp = "amazing"; break; case 6: temp = "neat-o keen"; break; case 7: temp = "cool"; break; case 8: temp = "great"; break; case 9: return "fantastic"; } return temp; } /** This method draws the hangman based on how * many incorrect guesses the user has made. * ---+ * o * /@\ * / \ * @param guess - a counter */ public static void 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"," ","+----------+"); } }