"""
File: hangman.py
Name: Allen Li
-----------------------------
This program plays hangman game.
Users sees a dashed word, trying to
correctly figure the un-dashed word out
by inputting one character each round.
If the user input is correct, show the
updated word on console. Players have N_TURNS
chances to try and win this game.
"""

import random

# This constant controls the number of guess the player has.
N_TURNS = 7


def main():
    """
    First, the computer randomly choose a word (answer) and a user will know how many alphabets in the word.
    Second, a user inputs his or her guess. We should make sure the format of guess is a single alphabets and turn
    the alphabet into upper case. If the user enter format that isn't meet our criteria, ask the user to enter a guess
    until we get a correct format of guess. If the user enter right format of guess, print what the word looks like
    after very guess and how many time of chance left for the user to guess.
    Third, ask user to enter a new guess. We do the same steps as mentioned above for every new guess. If the user run
    out of chance to guess, print'You are completely hung :('. If the user figure out the answer, print'You win!!'.
    """
    ans = random_word()
    length = len(ans)
    print('The word looks like: ', end="")
    for i in range(length):
        print('-', end='')
    print('\nYou have ' + str(N_TURNS) + ' guesses left.')
    # Step 2
    input_ch = input('Your guess: ')
    length_input_ch = len(input_ch)
    input_ch = input_ch.upper()
    if input_ch.isalpha() is True and length_input_ch == 1:
        count = 0
        guess = check(input_ch, ans)
        remain_guesses = add(count, ans, input_ch)
        count = remain_guesses
        print('You have ' + str(N_TURNS - remain_guesses) + ' guesses left.')
    else:
        print('Illegal format.')
        while True:
            input_ch = input('Your guess: ')
            length_input_ch = len(input_ch)
            input_ch = input_ch.upper()
            if input_ch.isalpha() is True and length_input_ch == 1:
                count = 0
                guess = check(input_ch, ans)
                remain_guesses = add(count, ans, input_ch)
                count = remain_guesses
                print('You have ' + str(N_TURNS - remain_guesses) + ' guesses left.')
                break
            else:
                print('Illegal format.')
    # Step 3
    while (N_TURNS - remain_guesses) > 0:
        input_ch = input('Your guess: ')
        input_ch = input_ch.upper()
        length_input_ch = len(input_ch)
        if input_ch.isalpha() is True and length_input_ch == 1:
            another_guess = check_again(input_ch, ans, guess, remain_guesses)
            guess = another_guess
            if guess != ans:
                if (N_TURNS - remain_guesses) > 1:
                    print('The word looks like: ' + guess)
                    remain_guesses = add(count, ans, input_ch)
                    count = remain_guesses
                    print('You have ' + str(N_TURNS - remain_guesses) + ' guesses left.')
                else:
                    print('You are completely hung :( ')
                    break
            else:
                print('You win!!')
                break
        else:
            print('Illegal format.')
    print('The word was: ' + str(ans))


def check(input_ch, ans):
    string = ""
    length = len(ans)
    length_test = len(input_ch)
    if ans.find(input_ch) != -1:
        print('Yor are correct!')
        print('The word looks like: ', end="")
        for j in range(length):
            if ans[j] == input_ch:
                string += input_ch
            else:
                string += "-"
        print(string)
    else:
        if input_ch.isalpha() is True and length_test == 1:
            print('There is no ' + str(input_ch) + "'s in the word.")
            print('The word looks like: ', end="")
            for k in range(length):
                string += "-"
            print(string)
        else:
            pass
    return string


def add(count, ans, input_ch):
    if ans.find(input_ch) == -1:
        count += 1
    else:
        if count == 0:
            pass
        else:
            count = count
    return count


def check_again(input_ch, ans, guess,remain_guesses):
    length = len(ans)
    string = ""
    if ans.find(input_ch) != -1:
        print('Yor are correct!')
        for l in range(length):
            if guess[l] == "-":
                if ans[l] == input_ch:
                    string += input_ch
                else:
                    string += guess[l]
            else:
                string += guess[l]
    else:
        print('There is no ' + str(input_ch) + "'s in the word.")
        if N_TURNS-remain_guesses > 1:
            for m in range(length):
                string += guess[m]
    return string


def random_word():
    num = random.choice(range(9))
    if num == 0:
        return "NOTORIOUS"
    elif num == 1:
        return "GLAMOROUS"
    elif num == 2:
        return "CAUTIOUS"
    elif num == 3:
        return "DEMOCRACY"
    elif num == 4:
        return "BOYCOTT"
    elif num == 5:
        return "ENTHUSIASTIC"
    elif num == 6:
        return "HOSPITALITY"
    elif num == 7:
        return "BUNDLE"
    elif num == 8:
        return "REFUND"


#####  DO NOT EDIT THE CODE BELOW THIS LINE  #####
if __name__ == '__main__':
    main()
