Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE THE JAVA LANGUAGE AND DO NOT USE BUFFERED READER The dictionary file is too

ID: 3870704 • Letter: U

Question

USE THE JAVA LANGUAGE AND DO NOT USE BUFFERED READER

The dictionary file is too big to include so please use a sample file of your choosing in its place

Objective:

Write a game of Hangman. The player will guess one letter at a time until the word is finally revealed. Also there are a limited number of guesses allowed before the game is over. I advise using two classes. The first class will have all the back end data and will contain:

- Member Variables

--The secret word

-- The disguised word where each unknown letter is replaced by a question mark (?). For example, if the word is abracadabra and the letters a,b,e have been guessed, the disguised word would be ab?a?a?ab?a

-- The number of guesses made

-- The number of incorrect guesses

-- Dictionary of words

- Constructors

-- A default constructor initializing each value to a default value and resets the game.

- Accessors and Mutators for all member variables. Make sure they check for valid values!

- Methods

-- loadDictionary – This method loads dictionary file. This file has each word on a separate line

-- resetGame – Picks a new word from the dictionary and resets all of the values.

-- makeGuess – this returns a Boolean value if the letter was found in the secret word. Case does not matter. Also it passes a character as a parameter. This will add to the number of guesses made, check to see if the letter was found, and if the letter was found it replaces the question marks in the disguised word with that letter. Keep in mind this may be decomposable into a couple of methods, but that is up to you.

-- isFound – returns a true if the hidden word has been completely uncovered.

Next write another class that runs the game. This can be done in several different ways, but some points I do expect to see is

- At each update the user is shown the disguised word and the number of guesses they have made.

- Also they should be shown a little diagram of a person. Initially it shows the gallows, then for every missed letter the picture adds this in the following order

-- Head

-- Body

-- One Arm

-- Both Arms

-- Left Leg

-- Right Leg

- After the user is shown their current state, they are then prompted to enter a letter or type something to quit the game

- Once the user has successfully guessed the secret word, or has more than 7 unsuccessful guesses the game is over and the user is prompted whether or not they want to play again. If they do the process starts all over.

Example Dialog:

Welcome to Hangman!

You have made 0 guesses

???????

_______

|   |

|

|

|

|

|

Enter a letter

R

You have made 1 guesses

???????

_______

|   |

|   O

|

|

|

|

Enter a letter

S

You have made 2 guesses

???????

_______

|   |

|   O

|   |

|   |

|

|

Enter a letter

T

You have made 3 guesses

???????

_______

|   |

|   O

| |

|   |

|

|

Enter a letter

E

You have made 4 guesses

??????e

_______

|   |

|   O

| |

|   |

|

|

Enter a letter

A

You have made 5 guesses

?a??a?e

_______

|   |

|   O

| |

|   |

|

|

Enter a letter

L

You have made 6 guesses

?a??a?e

_______

|   |

|   O

| |/

|   |

|

|

Enter a letter

N

You have made 7 guesses

?a??a?e

_______

|   |

|   O

| |/

|   |

| /

|

Enter a letter

B

You have made 8 guesses

?abba?e

_______

|   |

|   O

| |/

|   |

| /

|

Enter a letter

G

You have made 9 guesses

?abbage

_______

|   |

|   O

| |/

|   |

| /

|

Enter a letter

Z

You have made 10 guesses

?abbage

_______

|   |

|   O

| |/

|   |

| /

|

Enter a letter

Q

You have made 11 guesses

?abbage

_______

|   |

|   O

| |/

|   |

| /

|

GAME OVER!

The word was cabbage.

Would you like to play again? (Yes/No)

No

Explanation / Answer

Given below are the files for the question. Output shown at the end. Please don't forget to rate the answer if it helped. Thank you very much.

WordGuess.java


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class WordGuess {
private char[] secretWord;
private char[] disguisedWord;
private int numGuesses;
private int numWrongGuesses;
private ArrayList<String> words;
private Random random ;
public WordGuess()
{
words = new ArrayList<String>();
random = new Random(System.currentTimeMillis());
}
public void loadDictionary(String filename) throws FileNotFoundException
{
Scanner infile = new Scanner(new File(filename));
while(infile.hasNextLine())
words.add(infile.next().toLowerCase());
infile.close();
}
public void reset()
{
String word = words.get(random.nextInt(words.size()));
secretWord = word.toCharArray();
disguisedWord = word.replaceAll(".", "?").toCharArray();
numGuesses = 0;
numWrongGuesses = 0;
}
public boolean makeGuess(char ch)
{
boolean found = false;
ch = Character.toLowerCase(ch);
for(int i = 0; i < secretWord.length; i++)
{
if(secretWord[i] == ch && disguisedWord[i] == '?')
{
disguisedWord[i] = ch;
found = true;
}
}
numGuesses++;
if(!found)
numWrongGuesses++;
return found;
}
public boolean isFound()
{
for(int i = 0; i < disguisedWord.length; i++)
if(disguisedWord[i] == '?') //still there is a question mark, so not found
return false;
return true; //whole word has been guessed
}
public String getSecretWord() {
return new String(secretWord);
}
public String getDisguisedWord() {
return new String(disguisedWord);
}
public int getNumGuesses() {
return numGuesses;
}
public int getNumWrongGuesses() {
return numWrongGuesses;
}
}

HangmanGame.java


import java.io.FileNotFoundException;
import java.util.Scanner;
public class HangmanGame {
private static void drawHangman(int step)
{
System.out.println("________");
System.out.println("| |");
System.out.print("| ");
if(1 <= step)
System.out.print("O");
System.out.println();
System.out.print("| ");
if(step >= 4)
System.out.print("\|/"); //use \ to represent backslash since is an escape character
else if(step >= 3)
System.out.print("\|"); //use \ to represent backslash since is an escape character
else if(step == 2)
System.out.print(" |");
System.out.println();
//draw 2 line in body
System.out.print("| ");
if(step >= 2)
System.out.print("|");
System.out.println();
//draw leg
System.out.print("| ");
if(step == 5)
System.out.print("/");
else if(step == 6)
System.out.print("/ \");
System.out.println(" | ");
}
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
System.out.println("Welcome to Hangman!");
WordGuess wg = new WordGuess();
try {
wg.loadDictionary("dictionary.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
while(true)
{
wg.reset();
while(!wg.isFound() && wg.getNumWrongGuesses() < 6)
{
System.out.println("You have made " + wg.getNumGuesses() + " guesses");
System.out.println(wg.getDisguisedWord());
drawHangman(wg.getNumWrongGuesses());
System.out.println("Enter a letter");
char ch = keybd.next().charAt(0);
wg.makeGuess(ch);
}
drawHangman(wg.getNumWrongGuesses());
System.out.println("GAME OVER!");
if(wg.isFound())
System.out.println("You guessed it correct!!!");
else
System.out.println("The word was " + wg.getSecretWord());
System.out.println("Would you like to play again? (yes/no)");
String ans = keybd.next();
if(!ans.equalsIgnoreCase("yes"))
break;
}
}
}

Input file: dictionary.txt

banana
grapes
tomato
pineapple
cabbage
watermelon
muskmelon
chilly

output

Welcome to Hangman!
You have made 0 guesses
?????????
________
| |
|   
|
|   
|
|

Enter a letter
a
You have made 1 guesses
?????????
________
| |
| O
|
|   
|
|

Enter a letter
e
You have made 2 guesses
?????e???
________
| |
| O
|
|   
|
|

Enter a letter
o
You have made 3 guesses
?????e?o?
________
| |
| O
|
|   
|
|

Enter a letter
u
You have made 4 guesses
?u???e?o?
________
| |
| O
|
|   
|
|

Enter a letter
s
You have made 5 guesses
?us??e?o?
________
| |
| O
|
|   
|
|

Enter a letter
p
You have made 6 guesses
?us??e?o?
________
| |
| O
| |
| |
|
|

Enter a letter
q
You have made 7 guesses
?us??e?o?
________
| |
| O
| |
| |
|
|

Enter a letter
t
You have made 8 guesses
?us??e?o?
________
| |
| O
| |/
| |
|
|

Enter a letter
g
You have made 9 guesses
?us??e?o?
________
| |
| O
| |/
| |
| /
|

Enter a letter
m
You have made 10 guesses
mus?me?o?
________
| |
| O
| |/
| |
| /
|

Enter a letter
k
You have made 11 guesses
muskme?o?
________
| |
| O
| |/
| |
| /
|

Enter a letter
n
You have made 12 guesses
muskme?on
________
| |
| O
| |/
| |
| /
|

Enter a letter
q
________
| |
| O
| |/
| |
| /
|

GAME OVER!
The word was muskmelon
Would you like to play again? (yes/no)
no