We Value Your Privacy

We and our partners use technology such as cookies on our site to personalise content and ads, provide social media features, and analyse our traffic. Click below to consent to the use of this technology across the web. You can change your mind and change your consent choices at anytime by returning to this site.

Update Consent
Loading ...

How to Create a Hangman Game With Java

Updated March 23, 2017

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.

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

  2. class Main { static boolean playing Game = true; static ArrayList tried Letters = new ArrayList(); static int guesses = 0; static String secret Word;

  3. }

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

  5. 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.

  6. public static String askForWord() { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 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)

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

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

  9. }

  10. 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 ("_").

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

  12. 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.

  13. 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:

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

Loading ...

Things You'll Need

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


About the Author

Amber D. Walker has been writing professionally since 1989. She has had essays published in "Fort Worth Weekly," "Starsong," "Paper Bag," "Living Buddhism" and more. Walker holds a Bachelor of Arts degree in English from the University of Texas and worked as an English teacher abroad for six years.

Loading ...