this is what I have done so far I did not find the way to update it, could you p
ID: 3591634 • Letter: T
Question
this is what I have done so far
I did not find the way to update it, could you please create one with the information below?
aback
abacus
abalone
abandon
abase
abash
abate
abater
abbas
abbe
abbey
abbot
abbreviate
abc
abdicate
abdomen
abdominal
abduct
abed
aberrant
aberrate
abet
abetted
abetting
abeyance
abeyant
abhorred
abhorrent
abide
abject
ablate
ablaze
able
ablution
abnormal
aboard
abode
abolish
abolition
abominable
abominate
aboriginal
aborigine
aborning
abort
abound
about
above
aboveboard
aboveground
abovementioned
abrade
abrasion
abrasive
abreact
abreast
abridge
abridgment
abroad
abrogate
abrupt
abscess
abscissa
abscissae
absence
absent
absentee
absenteeism
absentia
absentminded
absinthe
absolute
absolution
absolve
absorb
absorbent
absorption
absorptive
abstain
abstention
abstinent
abstract
abstracter
abstractor
abstruse
absurd
abuilding
abundant
abusable
abuse
abusive
abut
abutted
abutting
abysmal
abyss
academe
academia
academic
academician
academy
acanthus
accede
accelerate
accelerometer
accent
accentual
accentuate
accept
acceptant
acceptor
access
accessible
accession
accessory
accident
accidental
accipiter
acclaim
acclamation
acclimate
accolade
accommodate
accompaniment
accompanist
accompany
accomplice
Create a very simple game of hangman. You are given dict.txt which is a file of words (one per line). Load these words into an ArrayList. Randomly select one word. Display the word as all asterisks (one asterisk per letter) Allow the user 10 attempts to guess the word. After each guess, "revea" all occurrences of the guessed letter in the displayed string or update the number of incorrect guesses. You are required to use at least 2 methods besides main), although more may be warranted. A few sample runs are provided. Your game should be entirely case insensitive 2. Things you do not need to handle: Invalid input. If the user enters "6" as their guess, it will be counted as an incorrect guess Duplicate letters. It's up to the user to not guess the same letter twice . . You may use abbreviated documentation on this program. For each method, state a short description no javadocs required. In line comments, as needed.Explanation / Answer
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
* This program implements the word guessing game called Hangman.
*/
public class Hangman {
private String secretWord; // the chosen secret word
private ArrayList<Character> correctLetters; // correct guesses
private ArrayList<Character> incorrectLetters; // incorrect guesses
private Scanner stdin = new Scanner(System.in); // for user input
/**
* Constructs a Hangman game.
*/
public Hangman(String secretWord) {
this.secretWord = secretWord;
this.correctLetters = new ArrayList<Character>();
for (int i = 0; i < this.secretWord.length(); i++)
this.correctLetters.add('*');
this.incorrectLetters = new ArrayList<Character>();
}
/**
* playGame
*
* Play one game of Hangman until
* the user wins (guesses all of the letters in the secret word)
* or loses (guesses 10 incorrect letters):
*/
public void playGame() {
while (!gameOver()) {
//Print the Hangman picture
printHangman();
//Print the correct guesses in the secret word
for (int i = 0; i < this.correctLetters.size(); i++)
System.out.print(this.correctLetters.get(i) + " ");
//Print the incorrect letters that have been guessed
System.out.print(" Wrong letters: ");
for (int i = 0; i < this.incorrectLetters.size(); i++)
System.out.print(this.incorrectLetters.get(i) + " ");
//Prompt and read the next guess
System.out.print(" Enter a lower-case letter as your guess: ");
String guess = stdin.nextLine();
//Process the next guess
handleGuess(guess.charAt(0));
}
System.out.println("The secret word was: " + secretWord);
if (gameWon()) {
System.out.println("Congratulations, you won!");
} else {
System.out.println("Sorry, you lost.");
printHangman();
}
}
private void handleGuess(char ch) {
if(incorrectLetters.contains(ch))
return;
boolean isCorrectGuess = false;
for(int i = 0; i < secretWord.length(); i++)
{
if (ch == secretWord.charAt(i))
{
isCorrectGuess = true;
correctLetters.set(i, ch);
}
}
if(!isCorrectGuess)
{
incorrectLetters.add(ch);
}
}
/**
* gameWon
*
* Return true if the user has won the game;
* otherwise, return false.
*
* @return true if the user has won, false otherwise
*/
private boolean gameWon() {
StringBuilder result = new StringBuilder(correctLetters.size());
for (Character c : correctLetters) {
result.append(c);
}
String output = result.toString();
return secretWord.equals(output);
}
/**
* gameLost
*
* Return true if the user has lost the game;
* otherwise, return false.
*
* @return true if the user has lost, false otherwise
*/
private boolean gameLost() {
return (incorrectLetters.size() >= 10) && (!gameWon());
}
/**
* gameOver
*
* Return true if the user has either won or lost the game;
* otherwise, return false.
*
* @return true if the user has won or lost, false otherwise
*/
private boolean gameOver() {
return (gameWon() || gameLost());
}
/**
* printHangman
*
* Print the Hangman that corresponds to the given number of
* wrong guesses so far.
*
* @param numWrong number of wrong guesses so far
*/
private void printHangman() {
int poleLines = 6; // number of lines for hanging pole
System.out.println(" ____");
System.out.println(" | |");
int badGuesses = this.incorrectLetters.size();
if (badGuesses == 7) {
System.out.println(" | |");
System.out.println(" | |");
}
if (badGuesses > 0) {
System.out.println(" | O");
poleLines = 5;
}
if (badGuesses > 1) {
poleLines = 4;
if (badGuesses == 2) {
System.out.println(" | |");
} else if (badGuesses == 3) {
System.out.println(" | \|");
} else if (badGuesses >= 4) {
System.out.println(" | \|/");
}
}
if (badGuesses > 4) {
poleLines = 3;
if (badGuesses == 5) {
System.out.println(" | /");
} else if (badGuesses >= 6) {
System.out.println(" | / \");
}
}
if (badGuesses == 7) {
poleLines = 1;
}
for (int k = 0; k < poleLines; k++) {
System.out.println(" |");
}
System.out.println("__|__");
System.out.println();
}
/**
* toString
*
* Returns a string representation of the Hangman object.
* Note that this method is for testing purposes only!
* @return a string representation of the Hangman object
*/
public String toString() {
String s = "secret word: " + this.secretWord;
s += " correct letters: ";
for (int i = 0; i < this.correctLetters.size(); i++)
s += this.correctLetters.get(i) + " ";
s += " used letters: ";
for (int i = 0; i < this.incorrectLetters.size(); i++)
s += this.incorrectLetters.get(i) + " ";
s += " # bad letters: " + this.incorrectLetters.size();
return s;
}
public static void main(String [] args) throws FileNotFoundException {
List<String> words = new ArrayList<>();
Scanner sc = new Scanner(new FileReader("dict.txt"));
while(sc.hasNext()) {
words.add(sc.nextLine());
}
sc.close();
//Randomly choose a word from list of words
Random randIndex = new Random();
int index = randIndex.nextInt(words.size());
Hangman game = new Hangman(words.get(index));
game.playGame();
}
}
// sample run
____
| |
|
|
|
|
|
|
__|__
* * * * * * *
Wrong letters:
Enter a lower-case letter as your guess: a
____
| |
|
|
|
|
|
|
__|__
a * * * a * *
Wrong letters:
Enter a lower-case letter as your guess: c
____
| |
|
|
|
|
|
|
__|__
a c c * a * *
Wrong letters:
Enter a lower-case letter as your guess: c
____
| |
|
|
|
|
|
|
__|__
a c c * a * *
Wrong letters:
Enter a lower-case letter as your guess: l
____
| |
|
|
|
|
|
|
__|__
a c c l a * *
Wrong letters:
Enter a lower-case letter as your guess: i
____
| |
|
|
|
|
|
|
__|__
a c c l a i *
Wrong letters:
Enter a lower-case letter as your guess: m
The secret word was: acclaim
Congratulations, you won!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.