Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What is a Java hangman game?

Asked by Aashish Chaursiya Oct 12, 2022 899 views 1 answer
Share

About this question

Hangman game coded in Java (at a beginner's level) where Hangman.java can be run with one or more arguments.


Hangman.java:
package hangman;
import hangman.Game;
import hangman.Prompter;
public class Hangman {
    public static String normalizeArgs(String[] array) {
        String result = "";
        switch (array.length) {
            case 0:
                System.out.println("Usage:  java Hangman ");
                System.err.println("answer is required");
                System.exit(1);
                break;
            case 1:
                result = array[0];
                break;
            default:
                for (int i = 0; i < array>                    result += array[i];
                    if (i != array.length-1) {
                        result += ' ';
                    }
                }   
        }
        return result;
    }
    public static void main(String[] args) {
        Game game = new Game(normalizeArgs(args));
        Prompter prompter = new Prompter(game);
        while (game.getRemainingTries() > 0 && !game.isWon()) {
            prompter.displayProgress();
            prompter.promptForGuess();
        }
        prompter.displayOutcome();
    }
}
Prompter.java:
package hangman;
import java.util.Scanner;
public class Prompter {
    private Game game;
    public Prompter(Game game) {
        this.game = game;
    }
    public boolean promptForGuess() {
        Scanner scanner = new Scanner(System.in);
        boolean isHit = false;
        boolean isAcceptable = false;
        do {
            System.out.printf("Enter a letter:  ");
            String guessInput = scanner.nextLine().toLowerCase();
            try {
                isHit = game.applyGuess(guessInput);
                isAcceptable = true;
            } catch (IllegalArgumentException iae) {
                System.out.printf("%s.  Please try again. %n", 
                                  iae.getMessage());
            }
        } while (!isAcceptable);
        return isHit;
    }
    public void displayProgress() {
        System.out.printf("You have %d tries left to solve: %s%n", 
                           game.getRemainingTries(),
                           game.getCurrentProgress());
    }
    public void displayOutcome() {
        if (game.isWon()) {
            System.out.printf("Congratulations! You won with %d tries left.%n", game.getRemainingTries());
        } else {
            System.out.printf("Bummer! The word was %s.  :(%n", game.getAnswer());
        }
    }
}
Game.java:
package hangman;
public class Game {
    public static final int MAX_TRIES = 7;
    private String answer;
    private String hits;
    private String misses;
    public Game(String answer) {
        this.answer = answer.toLowerCase();
        hits = "";
        misses = "";
    }
    private chat normalizeGuess(char letter) {
        if (!Character.isLetter(letter)) {
            throw new IllegalArgumentException("Guess is not a letter");
        }
        if (hits.indexOf(letter) != -1 || misses.indexOf(letter) != -1) {
            throw new IllegalArgumentException("Letter already guessed");
        }
        return letter;
    }
    public boolean applyGuess(String letters) {
        if (letters.length() == 0) {
            throw new IllegalArgumentException("No letters found");
        }
        return applyGuess(letters.charAt(0));
    }
    public boolean applyGuess(char letter) {
        normalizeGuess(letter);
        boolean isHit = answer.indexOf(letter) != -1; 
        if (isHit) {
            hits += letter;
        } else {
            misses += letter;
        }
        return isHit;
    }
    public int getRemainingTries() {
        return MAX_TRIES - misses.length();
    }
    public String getCurrentProgress() {
        String progress = "";
        for (char letter : answer.toCharArray()) {
            if (hits.indexOf(letter) != -1) {
                progress += letter;
            } else if (letter == ' ') {
                progress += ' '; 
            } else {
                progress += '-';
            }
        }
        return progress;
    }
    public boolean isWon() {
        return getCurrentProgress().indexOf('-') == -1;
    }
    public String getAnswer() {
        return answer;
    }
}


Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.