Write a hangman’s program where the user is a computer that has to guess the wor
ID: 3697953 • Letter: W
Question
Write a hangman’s program where the user is a computer that has to guess the word “JAVA.” The computer uses a brute force method.
a) First write the program that shows (prints) each guess (the letter) and also draws the result (line by line, circle by circle) when the guess is wrong.
b) What does the brute force method do exactly?
The computer draws both the gallows and the victim. The gallows and hapless victim look like this:
c) Why does the computer win every time if the word is JAVA?
d) Look at the following list of words and determine ahead of time if the computer would win. Then prove our conclusion by running your program
Word List:
ADA
COBOL
LOGO
PEARL
BASIC
Explanation / Answer
import java.util.*; import java.io.*; public class Hangman { public static Scanner keyboard; public static final int MAXSIZE = 15000; public static final int NUM_GUESS = 8; public static final int WORD_LENGTH = 20; public static void main(String[] args) { keyboard = new Scanner(System.in); int random = pickrandom(MAXSIZE); try { // Set up connection to the input file Scanner input = new Scanner(new FileReader("dictionary.txt")); String [] dictionaryWords = new String [MAXSIZE]; int usedsize = 0; while ( usedsize 0)) { char letter = guess.charAt(0); String guesses = ""; guesses += letter; if (word.indexOf(letter) < 0) { tries--; System.out.println("Incorrect letters tried: " + guess); System.out.println("Number of guesses left: " + tries); System.out.println("Enter a letter to guess or 9 to quit"); guess = keyboard.next(); } else { String correctWord = correctWord( guess, word, blank, letter ); System.out.println(correctWord); System.out.println("Enter a letter to guess or 9 to quit"); tries--; System.out.println("Number of guesses left: " + tries); guess = keyboard.next(); tries--; System.out.println("Number of guesses left: " + tries); } } if (guess.equals("9")){ System.out.println("Thanks for playing"); } if ( guess.equals(word)){ System.out.println("You won!"); } if( tries == 0){ System.out.println("You have no more guesses left"); } System.out.println("Play again? Y/N"); decision = keyboard.nextLine().toUpperCase(); } while (decision.equals("Y")); //System.out.println("Play again? Y/N"); //decision = keyboard.nextLine().toUpperCase(); } catch (FileNotFoundException e) { System.out.println("There was an error opening one of the files."); } } //Clears screen after introduction private static void clearScreen (){ for (int blanks = 0; blanks < 80; blanks++) { System.out.println(); } } // This method returns a randomly selected integer // between 0 and count-1 public static int pickrandom(int count) { Random generator = new Random(); return generator.nextInt(count); } // Places asterisks in place of the letters in the word // Parameter is the string word. it holds mystery word public static String blankWord(String word) { String blank = ""; for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.