How to Create a Hangman Game With Java

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.

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 tried Letters = new ArrayList(); static int guesses = 0; static String secret Word;


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().

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)

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 ");


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("_"); } }

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.

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; }

Most recent