1. Modifiy the code so that the guess word can be any length (utilize the length
ID: 3739725 • Letter: 1
Question
1. Modifiy the code so that the guess word can be any length (utilize the length method for this).
2. Add code to display the number of guesses left.
3. Add code to determine if the user has already used a letter and prompt user when they have already guessed the letter. Do not include the letter in the number of incorrect guesses.
4. Add code to display the letters the user has already guessed. Display the information immediately before prompting the user to enter another letter.
//Simulates the Hangman Game: Part 1 - Java Version before modifications
import java.io.*;
import java.util.*;
public class Hangman
{
public static void main(String[] args)
{
//declare variables
String origWord = "";
String letter = "";
boolean dashReplaced = false;
boolean gameOver = false;
int numIncorrect = 0;
String guessWord = "-----";
String updatedWord = "";
Scanner dataIn = new Scanner(System.in);
//get the original word from player 1
do
{
System.out.println("Enter the original word: ");
origWord = dataIn.nextLine();
} while (origWord.length() != 5);
//convert the original word to uppercase
origWord = origWord.toUpperCase();
//clear the window
for (int x = 0; x < 50; x++)
{
System.out.println(' ');
}//end for
//begin game
System.out.println("WORD GUESS GAME");
//allow player 2 to guess letter; the game is over when either the word is guessed
//or player makes 10 incorrect guesses
while (gameOver == false)
{
dashReplaced = false;
//display guessWord with dashes
System.out.println("Guess this word: " + guessWord);
//get a letter from player 2, then convert the letter to uppercase
System.out.print("Enter a letter ");
letter = dataIn.nextLine();
letter = letter.toUpperCase();
//search for the guessed letter in the original word
for (int x = 0; x < 5; x++)
{
if (origWord.indexOf(letter, x) >= 0) //letter found in original word
{
//replace the appropriate dash with guessed letter
//in the appropriate location
updatedWord = guessWord.substring(0, origWord.indexOf(letter, x));
updatedWord += letter;
updatedWord += guessWord.substring(origWord.indexOf(letter, x) + 1, guessWord.length());
//updated word has replaced the appropriate dash
//with guessed letter- assign to guessWord
guessWord = updatedWord;
//indicate that a replacement was made
dashReplaced = true;
}//end if
}//end for
//determine whether a replacement was made
if (dashReplaced == true)
{
//if the word does not contain any dashes,
//then the user guessed the word,
//so the game is over
if (guessWord.indexOf('-') == -1) //check to see if any dashes remain
{
gameOver = true;
System.out.println("Yes, the word is " + guessWord);
System.out.println("Great guessing! You win!");
}//end if
}
else //processed when no dash was replaced
{
//update the counter variable
numIncorrect++;
// determine whether player made 10 incorrect guesses
if (numIncorrect == 10)
{
//the game is over
gameOver = true;
System.out.println("Sorry, you lose. You made too many incorrect guesses.");
System.out.println("The word is " + origWord);
}//end if
}//end if
}//end while
}//end main
}//end class
Explanation / Answer
Hi,
Below is the modified program. The changes are indicated in Bold and also numbered in comments as per question. Also extra comments are added for clarification. Run the program and verify the output. Let me know if you have any questions.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.