In Java create a word guessing game called “ Hangman ”. The game is played betwe
ID: 3807938 • Letter: I
Question
In Java create a word guessing game called “Hangman”. The game is played between the computer and one player. The player needs to guess the correct word by guessing a single character each time. The player is allowed upto 8 lives in one game. Every incorrect guess costs the player one life. The player loses the game if all 8 lives were used up, and wins if the complete word is guessed any time before that. (please include comments in your code)
Level 1
Choose a specific word of your liking and hardwire it in your code so you already know the size of the word.
Notify the player
Number of characters in the word
8 lives left
In each turn
Prompt the player to ”enter a char”
If the entered char is exists in the word, the player is notified about the number of times the char appears in the word.
If the entered char is not present in the word, the player is notified of its absence from the word.
Stop the game when all 8 lives are used up.
Game should end immediately, if the player has guessed the correct word
Output should display the correct word
Output should also display whether the game was a win or a loss.
Sample output is shown below
Sample output word to guess is "program" Your Word has 7 characters You have 8 lives. Let's play! Enter a char sorry! d is not a character in this word. Try again! You have 7 lives left Enter a char sorry! e is not a character in this word. Try again! You have 6 lives left. Enter a char Yes, the character o appears once in your word. You have 6 lives left Enter a char Yes, the character p appears once in your word. You have 6 lives left Enter a char Yes, the character appears twice in your word. You have 6 lives left Enter a char Yes, the character g' appears once in your word. You have 6 lives left Enter a char Yes, the character a appears once in your word. You have 6 lives left Enter a char Yes, the character m' appears once in your word. You have 6 lives left That is it! You win! The correct word is "program If the player used up all 8 lives, your output must say- Sorry, you have no lives left. You lose! The correct word is javg 2/22/2017Explanation / Answer
Hangman.java file
import java.util.Scanner;
public class Hangman
{
public static void main(String[] args)
{
//word that we want the user should guess
String word="java";
//no of lives a player should have, can change the value of this variable to change no of lives
int noOflives=8;
//length of word player has to guess.
int len =word.length();
//charecter entered by player
char ch;
int successfullGuessCount=0;
Scanner sc=new Scanner(System.in);
System.out.println("Your word has "+len+" character");
System.out.println("you have "+noOflives+" lives");
System.out.println("Lets play!");
for(;;)
{
System.out.println("Enter the Char");
//read the charecter from player
ch=sc.next().charAt(0);
//isPresentInWord method will check whether your entered char is in word and returns the no of times it is there in word.
int count=isPresentInWord(word,ch);
//if entered char appears in word display success message. and we are incrementing successfullGuesscount
if(count>0)
{
System.out.println("yes, the character '"+ch+"' appears "+count+" times in your words. You have "+noOflives+" left");
successfullGuessCount++;
}
else
{
System.out.println("Sorry!,"+ch+", is not a character in this word. Try again! You have "+noOflives+" left");
noOflives--;
}
if(noOflives<=0)
{
System.out.println("you have no lives left,You loose");
System.out.println("The Correct word is "+word);
break;
}
else if(successfullGuessCount==len)
{
System.out.println("That is it, you win");
System.out.println("The Correct word is "+word);
break;
}
}
System.out.println("Game Ends!!!");
}
private static int isPresentInWord(String word, char ch)
{
int counter=0;
for(int i=0;i<word.length();i++)
{
if(ch==word.charAt(i))
{
counter++;
}
}
return counter;
}
}
output:
Your word has 4 character
you have 8 lives
Lets play!
Enter the Char
a
yes, the character 'a' appears 2 times in your words. You have 8 left
Enter the Char
a
yes, the character 'a' appears 2 times in your words. You have 8 left
Enter the Char
v
yes, the character 'v' appears 1 times in your words. You have 8 left
Enter the Char
j
yes, the character 'j' appears 1 times in your words. You have 8 left
That is it, you win
The Correct word is java
Game Ends!!!
please dont forget to rate the ans.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.