Hangman Game in Java

Hangman game is one of the popular paper and pencil games. This game is played between two or more players. In this hangman game in java post, the computer will pick the word and the user who should find this word with in the given number of tries. In this post, I will be sharing the rules of the game, pseudo-code and java program. Let's start with the rules of the game first.

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 :

Hangman game in java output


That's all for today, please mention in comments in case you have any questions related to Hangman game in java.

Reference:
Wikipedia

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry