Read Also: Rock Paper Scissors Game in Java
Rules of the Hangman Game:
Rules of the Hangman game are quite simple. Let's find out below:1. One player thinks of a word.
2. Other player tries to guess the word by suggesting letters
3. Guessing player needs to guess the word with in the limited number of tries. In our java program below, I kept the number of tries to 7.
4. If guessing player guesses the letter which occurs in the word then another player will write it down to all the correct positions.
5. Each time, if the letter guesses by the guessing player are not in the word, then the number of tries given is reduced by 1.
6. If guessing player predicts the word with in the given number of tries then the guesser wins.
Pseudo Code of the Hangman Game in Java:
1. The computer will pick a random word by using java.util.Random class nextInt(int) method. In the below program, I called the word to be guessed as ANSWERWORD.2. Create a String which consists of asterisks only. The number of asterisks will be equal to the number of letters in the ANSWERWORD.
3. Check if the letter guessed by the guessing player is in ANSWERWORD or not.
a. if the letter is not present in ANSWERWORD, then reduce the number of attempts(tries) by 1.
b. If the letter is present, then update the newAsteriskString.
Assign newAsteriskString value to aestriskString.
4. If aestriskString is equal to the ANSWERWORD, then guessing player wins.
Hangman Game in Java Program Code:
import java.util.Random; import java.util.Scanner; public class Hangman { public static final String[] WORDS = {"JAVA","PYTHON","SCALA","KOTLIN","PERL"}; // ANSWERWORD is the word User try to Guess public static final String ANSWERWORD = getAnswerWord(); // Below statement will create an EmptyString /* Default value will be NULL for each character present in the String */ public static String emptyString = new String(new char[ANSWERWORD.length()]); // Replace every character in emptyString with "*" public static String aestriskString = emptyString.replace("\0","*"); // Number of Tries public static int attemptsRemaining = 7; public static void main(String args[]) { do { System.out.print("Guess any letter: " ); System.out.println(aestriskString); // Take letter from the user Scanner in = new Scanner(System.in); String guessLetter = in.next(); checkGuess(guessLetter.toUpperCase()); }while(attemptsRemaining > 0 && aestriskString.contains("*")); if (attemptsRemaining == 0) System.out.println("YOU LOST"); } public static void checkGuess(String guessLetter) { StringBuilder newAestriskString = new StringBuilder(aestriskString); // Loop through each letter of ANSWERWORD for(int i=0; i< ANSWERWORD.length(); i++) { //Check if guessLetter is present in the ANSWERWORD if(ANSWERWORD.charAt(i) == guessLetter.charAt(0)) newAestriskString.setCharAt(i,guessLetter.charAt(0)); } /* If guessLetter is NOT present in ANSWERWORD, then reduce the number of attempts by 1 */ if(aestriskString.equals(String.valueOf(newAestriskString))) attemptsRemaining--; else aestriskString = String.valueOf(newAestriskString);
if(aestriskString.equals(ANSWERWORD)) System.out.println("YOU WIN !! ANSWERWORD was: "+ANSWERWORD); else System.out.println("ATTENTION !! Tries Left: "+attemptsRemaining); } public static String getAnswerWord() { Random random = new Random(); int index = random.nextInt(WORDS.length); String answerWord = WORDS[index]; return answerWord; } }
Output :
That's all for today, please mention in comments in case you have any questions related to Hangman game in java.
Reference:
Wikipedia