Assignment 4 — Hangman Due: Thursday, July 21 at 1:00P.M. For Assignment #4, you
ID: 3627416 • Letter: A
Question
Assignment 4 — Hangman
Due: Thursday, July 21 at 1:00P.M.
For Assignment #4, your mission is to write a program that plays the game of Hangman.
As an assignment, Hangman serves two purposes. First, the program is designed to give
you some practice writing programs that manipulate strings and files using the facilities
described in the chapter on files. Second, by extending the program using the graphical
tools from the book and lecture, you will have a chance to work with multiple classes in a
single application.
When you play Hangman, the computer first selects a secret word at random from a list
built into the program. The program then prints out a row of dashes—one for each letter
in the secret word and asks the user to guess a letter. If the user guesses a letter that is in
the word, the word is redisplayed with all instances of that letter shown in the correct
positions, along with any letters correctly guessed on previous turns. If the letter does not
appear in the word, the user is charged with an incorrect guess. The user keeps guessing
letters until either (1) the user has correctly guessed all the letters in the word or (2) the
user has made eight incorrect guesses. Two sample runs that illustrate the play of the
game are shown in Figure 1 on the next page.
When it is played by children, the real fascination (a somewhat morbid fascination, I
suppose) from Hangman comes from the fact that incorrect guesses are recorded by
drawing an evolving picture of the user being hanged at a scaffold. For each incorrect
guess, a new part of a stick-figure body—first the head, then the body, then each arm,
each leg, and finally each foot—is added to the scaffold until the hanging is complete.
For example, the three diagrams below show the drawing after the first incorrect guess
(just the head), the third (the head, body, and left arm), and the diagram at the tragic end
of a losing game:
Explanation / Answer
please rate - thanks
message me if any problems
remember to create a file of possible words named
words.txt
import java.util.*;
import java.io.*;
public class Hangman
{
public static void main(String[] args) throws IOException
{
int words,n,i,choice;
Random gen = new Random();
int maxTries = 8;
int wordLength,index,foundletters;
boolean solved,found;
Scanner inScan = new Scanner(System.in);
char letter;
String secretWord="";
System.out.println("Welcome to Hangman!");
StringBuffer guessedLetters = new StringBuffer();
Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
words=fileScan.nextInt();
n=gen.nextInt(words);
for(i=0;i<=n;i++)
secretWord = fileScan.next().toLowerCase();
fileScan.close();
foundletters=secretWord.length();
//Creates a StringBuffer for the viewing of the letters guessed
StringBuffer word = new StringBuffer();
for(i = 0; i < secretWord.length(); i++)
word.append("_ ");
System.out.print("The word now looks like this: ");
System.out.println(word);
solved=false;
while(maxTries > 0&&!solved)
{System.out.println("You have "+maxTries+" guesses left.");
found=false;
System.out.print("Your guess: ");
letter = Character.toLowerCase(inScan.next().charAt(0));
guessedLetters.append(letter + " ");
index=0;
while(secretWord.indexOf(letter,index) != (-1))
{
found=true;
word.setCharAt(secretWord.indexOf(letter,index)*2,letter);
foundletters--;
index=secretWord.indexOf(letter,index)+1;
if(foundletters==0)
{solved=true;
System.out.println("Congratulations, you win!");
System.out.println("Here is the final board:");
System.out.println(word);
}
}
if(!found)
maxTries--;
if(!solved&&!found)
System.out.println("There are no " + letter + "'s in the word.");
if(!solved&&found)
{System.out.println("That guess is correct");
System.out.println("The word now looks like this: "+word);
}
}
if(!solved)
{System.out.println("Your completely hung.");
System.out.println("the word was "+secretWord);
System.out.println("You lose. ");
}
}
}