Hangman Game in Java
In this tutorial, We would like to share example program about, Simple Hangman Game in Java. The example program has been tested and output also attached in the same post.
Project Structure
What is the Hangman?
The Hangman is a word guessing game. The game tells to the user a random word from the given list, The user tries to guess it by suggesting letters, within a certain number of guesses.
Game Engine
package com.hangman.game; /** * * @author Dinesh Krishnan * */ public class HangmanGame { public static String[] easyList = { "one", "two" }; public static String[] mediumList = { "cow", "orange" }; public static String[] hardList = { "computer", "printer" }; private static int count = 0; public static String word; public static String gameWord; /* * Constructor which accept the game level */ public HangmanGame(GameLevel level) { if (level.getLevel().equals("easy")) { word = easyList[(int) Math.random() * easyList.length]; } else if (level.getLevel().equals("medium")) { word = mediumList[(int) Math.random() * mediumList.length]; } else if (level.getLevel().equals("hard")) { word = hardList[(int) Math.random() * hardList.length]; } gameWord = new String(new char[word.length()]).replace("\0", "-"); } /** * The main logic of the game * * @param guess */ public void hang(String guess) { guess = guess.toLowerCase(); // converting to lower case to support case // sensitive String newGameWord = ""; for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == guess.charAt(0)) { newGameWord += guess.charAt(0); } else if (gameWord.charAt(i) != '-') { newGameWord += word.charAt(i); } else { newGameWord += "-"; } } if (gameWord.equals(newGameWord)) { // if the game word and new word are // eq increasing the count count++; System.out.println("wrong guess"); } else { gameWord = newGameWord; } if (gameWord.equals(word)) { // once found correct answer System.out.println(word); System.out.println("Correct answer.."); System.exit(0); } } public int getCount() { return count; } public String getGameWord() { return gameWord; } }
Game Level
package com.hangman.game; /** * * @author Dinesh Krishnan * */ public enum GameLevel { ESAY("easy"), MEDIUM("medium"), HARD("hard"); private String level; private GameLevel(String level) { this.level = level; } public String getLevel() { return level; } public static GameLevel getRandam() { GameLevel[] levels = { GameLevel.ESAY, GameLevel.MEDIUM, GameLevel.HARD }; return levels[(int) (Math.random() * levels.length)]; } }
Main Application
package com.hangman.game; import java.util.Scanner; /** * * @author Dinesh Krishnan * */ public class Game { static String wordGuessed = ""; public static void main(String[] args) { HangmanGame game = null; Scanner sc = new Scanner(System.in); System.out.println("Welcome to Hangman Game"); System.out.println("Select the level : {Easy, Medium, Hard, Random} and type to 'exit' to quit game !!!"); String level = sc.next(); if (level.equalsIgnoreCase(GameLevel.ESAY.getLevel())) { // setting the level from the user selection game = new HangmanGame(GameLevel.ESAY); } else if (level.equalsIgnoreCase(GameLevel.MEDIUM.getLevel())) { game = new HangmanGame(GameLevel.MEDIUM); } else if (level.equalsIgnoreCase(GameLevel.HARD.getLevel())) { game = new HangmanGame(GameLevel.HARD); } else { game = new HangmanGame(GameLevel.getRandam()); // random level } System.out.println(game.getGameWord()); while (game.getCount() < 5 && true) { // running loop until the count is < 5 String guess = sc.next(); if (guess.equalsIgnoreCase("exit")) { System.out.println("Game exited"); System.exit(0); } game.hang(guess); wordGuessed += guess.charAt(0); // this is for guessed string System.out.println( game.getGameWord() + ", so far " + wordGuessed + " - and number of life " + (5 - game.getCount())); } if (game.getCount() >= 5) { System.out.println("Game over"); sc.close(); System.exit(0); } } }
Download Source Code
Download the source code from here
Output
Welcome to Hangman Game Select the level : {Easy, Medium, Hard, Random} and type to 'exit' to quit game !!! hard -------- c c-------, so far c - and number of life 5 o co------, so far co - and number of life 5 m com-----, so far com - and number of life 5 p comp----, so far comp - and number of life 5 u compu---, so far compu - and number of life 5 t comput--, so far comput - and number of life 5 e compute-, so far compute - and number of life 5 r computer Correct answer..
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet