How to Create a Hangman Game With Java

Written by amber d. walker
  • Share
  • Tweet
  • Share
  • Email

Everyone remembers playing the word-guessing game Hangman as a child. One player secretly chooses a word. The other player has to guess letters that may be in the word. If she is right, the letters are added. If she is wrong, another body part for the hangman is drawn in. This tutorial will teach you how to create a simple Hangman game that can be played by two people using the Java programming language.

Skill level:

Other People Are Reading

Things you need

  • Computer
  • Java Runtime Environment
  • Java Development Kit
  • Text editor or Java IDE

Show MoreHide


  1. 1

    Build the main game loop. Create a class called Main, and write the following main function.

    class Main { static boolean playing Game = true; static ArrayList<String> tried Letters = new ArrayList<String>(); static int guesses = 0; static String secret Word; public static int main(String[] args) { secretWord = askForWord(); while (playingGame) { printBodyAndTriedLetters(); printWordSoFar(); getLetter(); if (gameStatus() == 1) { System.out.println("You win!"); return 0; } else if (gameStatus() == -1) { System.out.println("You lose! Secret word was: " + secretWord); return 0; } System.out.println(); System.out.println(); System.out.println(); } } }

    This lays out the game's structure and gives you five functions you need to write to complete the game: askForWord(), printBodyAndTriedLetters(), printWordSoFar(), getLetter() and gameStatus().

  2. 2

    Write the askForWord function. This function will be pretty simple. Use the BufferedReader class that comes with Java to get a secret word from the console, and then print out about a hundred lines to hide the word.

    public static String askForWord() { BufferedReader in = new BufferedReader(new InputStreamReader(; System.out.print("What is the secret word: "); String word = in.readLine().toLowerCase(); for (int x = 0; x < 100; x++) { System.out.println(); } return (word); } (see References 1)

  3. 3

    Write the printBodyAndTriedLetters() function. For this, you're going to print out the hanged body parts based on the number of guesses the player has had so far, and print out all the letters she has tried.

    public static void printBodyAndTriedLetters() { // If zero guesses, none of these will evaluate as true. // If two, then both the first and second will evaluate as true.
    if (guesses >= 1) System.out.print("HEAD "); if (guesses >= 2) System.out.print("BODY "); if (guesses >= 3) System.out.print("LEFT ARM "); if (guesses >= 4) System.out.print("RIGHT ARM "); if (guesses >= 5) System.out.print("LEFT LEG "); if (guesses >= 6) System.out.print("RIGHT LEG ");

    // You want the letters printed in alphabetical order. Collections.sort(triedLetters); for ( String letter : tried Letters ) { System.out.print(letter + " "); } System.out.println(); }

  4. 4

    Write printWordSoFar(). You want to compare each letter in the word with the tried letters and print those letters that match. If no match is found, you want to print an underscore ("_").

    public static void printWordSoFar() { for (int x = 0; x < secretWord.length(); x++) { String letter = secretWord.substring(x); boolean found = false; for (String t : tried Letters ) { if (letter.startsWith(t)) { System.out.print(t); found = true; } } if (!found) System.out.print("_"); } }

  5. 5

    Write get Letter(). This is actually simpler than it appears. The letter will be tested by game Status, so all you need to do here is reuse the code from Step 2 to retrieve a letter from the user.

  6. 6

    Write game Status(). To do this, reuse the code in printWordSoFar() for testing whether a letter exists. Only you need to change it in one small way: to test whether the loop has been successful in every iteration. The following will work:

    public static void game Status() { boolean solved = true; for (int x = 0; x < secretWord.length(); x++) { String letter = secretWord.substring(x); boolean found = false; for (String t : tried Letters ) { if (letter.startsWith(t)) { found = true; } } if (!found) solved = false; } // If solved, return 1. // If not solved and game over, return -1. // If none of the above, return 0. if (solved) return 1; else if (!solved && guesses >= 6) return -1; else return 0; }

Don't Miss


  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.