Write a game of Hangman representing the word and the letters that have been gue
ID: 3788700 • Letter: W
Question
Write a game of Hangman representing the word and the letters that have been guessed as arrays .
Your program should first ask the user to enter the word to be guessed (assuming there are two or more players, and one of them is choosing the word). Then, it should display the number of letters in the word (each letter represented by an underscore "_" to indicate that the users haven't guessed it yet), the letters that have been guessed, and the number of guesses the users have left (starting with 20). An updated version of this information should be displayed after every guess. If the user guesses the word, the program should tell them that they win and terminate. If they exhaust their guesses, the program should tell them that they lose and then terminate.
Explanation / Answer
Java code for hangman game.
import java.util.Scanner;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args)
{
char[] upperAlphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z' };
// Array of alphabet to display
String wordtoGuess;
char letterChoice;
String userChoiceString;
String wordArraytoString;
do {
System.out.println("Please enter a valid word (letters only)");
// User1 to enter valid word
Scanner wordInput = new Scanner(System.in);
wordtoGuess = wordInput.next();
wordtoGuess = wordtoGuess.toUpperCase();
} while (Pattern.matches("[A-Z]+", wordtoGuess) == false);
// examining the word is correct
char[] wordArray = wordtoGuess.toCharArray();
// Moving the word in character array
char[] guessingWordArray = new char[wordtoGuess.length()];
for (int h = 0; h < guessingWordArray.length; h++)
guessingWordArray[h] = '_';
// Use' _'(underscore) for the word to guess with user2
for (int' i = 0; i < 20; i++) {
// To print 20 empty lines to hide the input from user 1
System.out.println();
}
for (int j = 0; j < 10; j++) {
// Attempts loop
do {
System.out.print("Word to guess: ");
System.out.println(guessingWordArray);
System.out
.println("Please choose a letter or solve the word. " + "Attempts left: " + (10 - j));
// expecting the whole word
System.out.println(upperAlphabet);
Scanner userInput = new Scanner(System.in);
userChoiceString = userInput.next();
userChoiceString = userChoiceString.toUpperCase();
// To get the input as string
letterChoice = userChoiceString.charAt(0);
letterChoice = Character.toUpperCase(letterChoice);
// Getting first letter of the input
if (Character.isLetter(letterChoice) == false)
System.out.println("Invalid letter. Please try again.");
if (userChoiceString.length() > 1
// message error if the input is not a valid word or more than 1 character
&& userChoiceString.length() < wordtoGuess.length())
System.out.println(("Choose only one letter. Try again."));
} while (userChoiceString.length() != 1
&& userChoiceString.length() != wordtoGuess.length()
|| Character.isLetter(letterChoice) == false);
if (userChoiceString.length() == 1) {
// when input is only 1 character
for (int k = 0; k < upperAlphabet.length; k++) {
// Correctly Guessed letter is replaced by '_'
if (letterChoice == upperAlphabet[k]) {
upperAlphabet[k] = '_';
}
}
for (int m = 0; m < wordtoGuess.length(); m++) {
// if the correct letter is given as input, then the correct one will shown
if (letterChoice == wordArray[m]) {
guessingWordArray[m] = wordArray[m];
}
}
wordArraytoString = new String(guessingWordArray);
// Winning message once all the letters are correctly guessed
if (wordArraytoString.equals(wordtoGuess)) {
System.out.println(guessingWordArray);
System.out.print("You win.");
System.out.print("You guessed the word correctly: ");
System.out.print(wordtoGuess);
System.out.println(" in " + (j + 1) + " guesses.");
break;
}
} else if (userChoiceString.length() == wordtoGuess.length()) {
// display winning message when user2 guesses the right word
if (userChoiceString.equals(wordtoGuess)) {
System.out.println(guessingWordArray);
System.out.print("Excellent, you win.");
System.out.print("You've guessed the word correctly ");
System.out.print(wordtoGuess);
if (j == 0)
System.out.println(" in " + (j + 1) + " guess.");
else
System.out.println(" in " + (j + 1) + " guesses.");
break;
} else
System.out.println("Wrong guess, try again."); // If guessing word is wrong.
}
if (j >= 9)
System.out
.println("You lose, number of attempts allowed expires");
// More than 10 tries
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.