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.

Things You'll Need

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

Instructions

    • 1

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

      class Main {
      static boolean playingGame = true;
      static ArrayList<String> triedLetters = new ArrayList<String>(); static int guesses = 0;
      static String secretWord;

      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

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

    • 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 : triedLetters ) {
      System.out.print(letter + " ");
      }
      System.out.println();
      }

    • 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 : triedLetters ) {
      if (letter.startsWith(t)) {
      System.out.print(t);
      found = true;
      }
      }
      if (!found) System.out.print("_");
      }
      }

    • 5

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

    • 6

      Write gameStatus(). 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 gameStatus() {
      boolean solved = true;
      for (int x = 0; x < secretWord.length(); x++) {
      String letter = secretWord.substring(x);
      boolean found = false;
      for (String t : triedLetters ) {
      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;
      }

Related Searches:

References

Comments

  • Feb 16, 2011
    does not work.. do you have a complete java code instead of this individual parts?

You May Also Like

  • How to Make a Hangman Game

    Hangman is a simple two-player game in which one player chooses a word and the other tries to guess the word by...

  • How to Make Java Games

    Java games are computer games written in the computer programming language "Java." Java games can be as complex as the shoot-'em-up three...

  • How to Play Hangman

    Players try to guess a word by filling in the blanks. Each incorrect guess brings you closer to being "hanged."

  • Basic Java Games

    Java is an object-oriented programming language used for business and web applications, and also for games. Both basic and complex games are...

  • How to Make a Java Game Like RuneScape

    "Runescape" is a multiplayer online role-playing game (MMORPG). It was created in 2001, and even though it's quite old in comparison with...

  • How to Build a Hangman Game in Visual Basic

    In the classic guessing game of Hangman, a hidden word is revealed one letter at a time as the player guesses the...

  • How to Write a 3D Java Game

    Java 3-D games are based on a concept called a "scene graph," which simplifies game development. After conceiving the idea for a...

  • The Best Words for Hangman

    Hangman is a game that involves guessing your opponent's word, one letter at a time, up to a maximum number of wrong...

  • How to Build Simple Java Games

    If you're a beginning Java programmer and would like to write a game, it's best to start as simple as possible. Text-based...

  • How do I Create a Hangman Game?

    "Hangman" is a simple game that can be played just about anywhere. The object is to fill in the blanks of a...

  • How to Make an Object-Oriented Java Game

    Java is an object-oriented programming language created by Sun Microsystems. The term "object-oriented" refers to a programming approach that involves virtual objects...

  • How to Make a Hangman Puzzle

    Hangman is a word puzzle that can be played by two or more people. In the puzzle, one person chooses a word...

  • How to Make a Board Game in Java

    Anybody that has ever played a video game has said, "I like this, but I would have done it THIS way." As...

  • How to Make a Game Using Java Software

    The Java programming language can be used to develop complex applications involving databases, sounds and video. But, it can also be used...

  • How to Program a 3D Game

    3-D games are those having at least one element that appears to have height, width and depth. Programmers create these games by...

  • Hangman Games to Play

    Hangman Games to Play. The hangman game is an entertaining way to teach children and adults how to spell. The challenge of...

  • Free Word & Hangman Games

    Free Word & Hangman Games. Free word and Hangman games are plentifully available on the Internet. These games build vocabulary skills, especially...

  • How to Make a Wheel of Fortune Game Board

    Word-lovers everywhere simply cannot get enough of “Wheel of Fortune.” If you are one of these lexiphiles, you will be glad to...

  • How to Create Java Games

    The process of creating Java games is the same as the process for learning how to paint a painting or build a...

  • How to Write Games in Java

    The Java programming language has become a popular choice for development due to its object-oriented structure and its virtual-machine based interface that...

Related Ads

Featured