import java.util.Scanner;
import java.util.Random;
class HangedMan {
    public static void main(String[] args) { // main 
        boolean welcome = welcomeMessage(); // welcoming user with a message
        String word = selectRandomWord(); // selecting a random word from the list
        String hiddenWord = generateHiddenWord(word); // generating hidden word
        char guess = getInput(); // first time character input
        boolean playing = play(word, guess, hiddenWord); // the main function 
    }

    public static boolean play(String word, char guess, String hiddenWord) {
        int attempts = 10;
        while (attempts <= 10 && attempts > 0) {

            System.out.println("\nYou have " + attempts + " attempts left");
            boolean firstCheck = checkBoolean(guess, word);

            if (firstCheck) { // if letter is found in the word

                int position = getPosition(guess, word); // get the position of the letter in the word
                hiddenWord = updateSecret(guess, position, hiddenWord); // update the hiddenWord with the position of the letter in the word
                System.out.println("\nWhat you've found so far: " + hiddenWord + "\n"); // showing the updated hidden word to the user
                if (hiddenWord.equals(word)) { // checking if the user has won
                    System.out.println("You have WON");
                    break;
                }
                guess = getInput(); // if the game is not won yet, continuing
            } else { // if the letter is NOT in the word.
                System.out.println("\nNope! There is no letter "+guess+" in the word. Try again!");
                 attempts--; // taking an attempt off
                guess = getInput();
            }
        }
        return true;
    }

    public static boolean welcomeMessage() {
        System.out.println("\n \n");
        System.out.println( "           ______________________________________" );     
        System.out.println( "  ________|                                      |_______" );
        System.out.println( "  \\       |                   ITC                |      /" );
        System.out.println( "   \\      |                HANGEDMAN             |     /" );
        System.out.println( "   /      |______________________________________|     \\ " );
        System.out.println( "  /__________)                                (_________\\ " );
        System.out.println("\n \n");
        return true;
    }

    public static String generateHiddenWord(String w) {
        String asterix = "";
        for (int i = 0; i < w.length(); i++) {
            asterix += "*";
        }
        System.out.println("\nThe secret word has "+asterix.length()+ " letters! Good luck \n" );
        System.out.println(asterix+"\n" );
        return asterix;
    }

    public static String updateSecret(char g, int p, String s) {
        String newDisplaySecret = s.substring(0, p) + g + s.substring(p + 1);
        return newDisplaySecret;
    }

    public static int getPosition(char g, String w) {
        int position = w.indexOf(g);
        return position;
    }

    public static boolean checkBoolean(char g, String w) {
        if (w.toLowerCase().indexOf(g) != -1) {
            System.out.println("Great! You found a new letter! \n");
            return true;
        } else {
            return false;
        }
    }

    public static String selectRandomWord() {
        String[] allWords = {
            "tower",
            "goldstar",
            "water",
            "towel",
            "plant"
        };
        Random r = new Random();
        int randomNumber = r.nextInt(allWords.length);
        return allWords[randomNumber];
    }

    public static char getInput() {
        System.out.print("What is your guess? ");
        Scanner in = new Scanner(System.in);
        char character = in.nextLine().toLowerCase().charAt(0);
        return character;
    }
}