| about | help | code help+videos | done | prefs | 
tmhscs@gmail.com hangman
  _   _                                            ____                      
 | | | | __ _ _ __   __ _ _ __ ___   __ _ _ __    / ___| __ _ _ __ ___   ___ 
 | |_| |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \  | |  _ / _` | '_ ` _ \ / _ \
 |  _  | (_| | | | | (_| | | | | | | (_| | | | | | |_| | (_| | | | | | |  __/
 |_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|  \____|\__,_|_| |_| |_|\___|
                    |___/                                                                             
Hangman is a guessing game for two or more players. One player thinks of a word and asks the other player(s) to guess it by suggesting a letter that might be in the answer. The player(s) lose if they can’t guess the hidden word within a number of tries.
I’ve created a Java class that is only missing three methods to complete the game! Solve them below:
 missCheck H
missCheck H  
 usedLetterSorter H
usedLetterSorter H  
 getWin
getWin 
Afterwards, you can copy the incomplete code below into BlueJ or an online IDE, copy in the completed methods, and try playing the game!
import java.util.*;
public class HangmanGame {
    public static void main(String[] args){
        boolean keepPlaying = true;
        boolean win = false;
        int missCount = 0;
        String lettersGuessed = "";
        String hiddenWord = "";
        String sortedUsedLetters = "";
        String currentLetter = "";
        while(keepPlaying){
            hiddenWord = startMenu();
            startGame();
            while(!win && (!(missCount>=7))){
                printBoard(missCount);
                printWord(hiddenWord, lettersGuessed);
                usedLetters(sortedUsedLetters);
                currentLetter = getLetter(lettersGuessed);
                lettersGuessed += currentLetter;
                sortedUsedLetters = usedLetterSorter(sortedUsedLetters,currentLetter);
                missCount += missCheck(lettersGuessed, hiddenWord);
                win = getWin(hiddenWord, lettersGuessed);
            }
            keepPlaying = exitMenu(win, hiddenWord);
            win = false;
            missCount = 0;
            lettersGuessed = "";
            hiddenWord = "";
            sortedUsedLetters = "";
            currentLetter = "";
        }
        System.exit(0);
    }
    public static boolean getWin(String hiddenWord, String lettersGuessed){
        boolean win = false;
        //Your code goes here
        return win;
    }
    public static String usedLetterSorter(String usedLetters, String letter){
        //You code goes here
        return new String();
    }
    public static int missCheck(String used, String word){
        int addToMiss = 0;
        //You code goes here
        return addToMiss;
    }
    public static String getLetter(String used){
        String temp = "";
        boolean check = true;
        Scanner sc = new Scanner(System.in);
        while(check){
            System.out.print("Enter your guess: ");
            temp = sc.nextLine();
            if(temp.isEmpty()){
                while(temp.isEmpty()){
                    System.out.println("You must enter a letter. Please try again!!!\n");
                    System.out.print("Enter your guess: ");
                    temp = sc.nextLine();
                }
            }
            temp = temp.substring(0,1).toUpperCase();
            if(!Character.isLetter(temp.charAt(0))){
                while(!Character.isLetter(temp.charAt(0))){
                    System.out.println("You must enter a letter not a symbol/number. Please try again!!!\n");
                    System.out.print("Enter your guess: ");
                    temp = sc.nextLine();
                }
            }
            if(used.contains(temp)){
                System.out.println("You have already guessed that letter, Please try another!!!\n");
            }
            else{
                check = false;
            }
        }
        System.out.print("\n\n");
        sc.close();
        return temp.toUpperCase();
    }
    public static void startGame(){
        System.out.println("\f");
        System.out.println("*********************************");
        System.out.println("*******LETS START THE GAME*******");
        System.out.println("*********************************");
        putPause(1500);
    }
    public static void usedLetters(String str){
        System.out.print("Used Letters: " );
        for(int i = 0; i < str.length(); i++){
            System.out.print(str.substring(i,i+1) + " ");
        }
        System.out.print("\n");
    }
    public static boolean exitMenu(boolean win, String word){
        boolean choice = false;
        Scanner sc = new Scanner(System.in);
        String str = "";
        while(!(str.equalsIgnoreCase("y")||str.equalsIgnoreCase("n"))){
            System.out.print("\f");
            if(win){
                System.out.println("*********************************");
                System.out.println("*****YOU ESCAPED THE GALLOWS*****");
                System.out.println("*********************************");
                System.out.println("******THE WORD/PHRASE WAS********");
                System.out.println(word);
                System.out.println("*********************************");
            }
            else{
                printBoard(7);
                System.out.println("*********************************");
                System.out.println("*********YOU WERE HANGED*********");
                System.out.println("*********************************");
                System.out.println("******THE WORD/PHRASE WAS********");
                System.out.println(word);
                System.out.println("*********************************");
            }
            System.out.println("Would you like to play again? (y/n)");
            str = sc.nextLine();
        }
        sc.close();
        return str.equalsIgnoreCase("y");
    }
    public static void printWord(String hiddenWord, String lettersGuessed){
        String result = "";
        boolean count = false;
        for(int i = 0; i < hiddenWord.length(); i++){
            count = false;
            if(hiddenWord.substring(i,i+1).equals(" ")){
                result += "  ";
                count = true;
            }
            for(int j = 0; j < lettersGuessed.length(); j++){
                if(hiddenWord.substring(i,i+1).equals(lettersGuessed.substring(j,j+1))){
                    result += hiddenWord.substring(i,i+1)+ " ";
                    count = true;
                }
            }
            if(!count){
                result += "_" + " ";
            }
        }
        System.out.println(result);
        System.out.print("\n");
    }
    public static String startMenu(){
        int result = 0;
        String theWord = "";
        Scanner scan = new Scanner(System.in);
        System.out.println("\f*********************************");
        System.out.println("***WELCOME TO THE HANGMAN GAME***");
        System.out.println("*********************************");
        System.out.print("Please Enter a Word: ");
        theWord = scan.nextLine();
        System.out.print("\fYour word has been stored!");
        theWord = theWord.toUpperCase();
        putPause(1500);
        scan.close();
        return theWord;
    }
    public static void printBoard(int missCount){
        System.out.print("\f");
        if(missCount == 0){
            System.out.println("    -----------   ");
            System.out.println("   |         |   ");
            System.out.println("             |   ");
            System.out.println("             |   ");
            System.out.println("             |   ");
            System.out.println("             |   ");
            System.out.println("  -------------  ");
        }
        else if(missCount == 1){
            System.out.println("    -----------   ");
            System.out.println("   |         |   ");
            System.out.println("   0         |   ");
            System.out.println("             |   ");
            System.out.println("             |   ");
            System.out.println("             |   ");
            System.out.println("  -------------  ");
        }
        else if(missCount == 2){
            System.out.println("    -----------   ");
            System.out.println("   |         |   ");
            System.out.println("   0         |   ");
            System.out.println("   |         |   ");
            System.out.println("             |   ");
            System.out.println("             |   ");
            System.out.println("  -------------  ");
        }
        else if(missCount == 3){
            System.out.println("    -----------   ");
            System.out.println("   |         |   ");
            System.out.println("   0         |   ");
            System.out.println("   |\\        |   ");
            System.out.println("             |   ");
            System.out.println("             |   ");
            System.out.println("  -------------  ");
        }
        else if(missCount == 4){
            System.out.println("    -----------   ");
            System.out.println("   |         |   ");
            System.out.println("   0         |   ");
            System.out.println("  /|\\        |   ");
            System.out.println("             |   ");
            System.out.println("             |   ");
            System.out.println("  -------------  ");
        }
        else if(missCount == 5){
            System.out.println("    -----------   ");
            System.out.println("   |         |   ");
            System.out.println("   0         |   ");
            System.out.println("  /|\\        |   ");
            System.out.println("   |         |   ");
            System.out.println("             |   ");
            System.out.println("  -------------  ");
        }
        else if(missCount == 6){
            System.out.println("    -----------   ");
            System.out.println("   |         |   ");
            System.out.println("   0         |   ");
            System.out.println("  /|\\        |   ");
            System.out.println("   |         |   ");
            System.out.println("    \\        |   ");
            System.out.println("  -------------  ");
        }
        else if(missCount == 7){
            System.out.println("    -----------   ");
            System.out.println("   |         |   ");
            System.out.println("   0         |   ");
            System.out.println("  /|\\        |   ");
            System.out.println("   |         |   ");
            System.out.println("  / \\        |   ");
            System.out.println("  -------------  ");
        }
        System.out.print("\n");
    }
    public static void putPause(int timeMS){
        try{
            Thread.sleep(timeMS);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
Copyright Nick Parlante 2017 - privacy