# Hangman game
#

# -----------------------------------
# Helper code
# You don't need to understand this helper code,
# but you will have to know how to use the functions
# (so be sure to read the docstrings!)

import random
import string

WORDLIST_FILENAME = "/Users/deedeebanh/Documents/MITx_6.00.1.x/ProblemSet3/words.txt"

def loadWords():
    """
    Returns a list of valid words. Words are strings of lowercase letters.

    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    print("  ", len(wordlist), "words loaded.")
    return wordlist

def chooseWord(wordlist):
    """
    wordlist (list): list of words (strings)

    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code
# -----------------------------------

# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = loadWords()

def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    # FILL IN YOUR CODE HERE...

    isAinB = [item in lettersGuessed for item in secretWord]
    return (all(isAinB))


def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE...
    store = list('_'*len(secretWord)) #first set up ___ = length of secretWord
    for i in range(len(secretWord)):
        for j in range(len(lettersGuessed)):
            if lettersGuessed[j] == secretWord[i]:
                store[i] = lettersGuessed[j] #replace _ with the letter
    return (''.join(store))

def getAvailableLetters(lettersGuessed):
    '''
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''
    # FILL IN YOUR CODE HERE...
    diff = [item for item in (list(string.ascii_lowercase)) if item not in lettersGuessed]
    return (''.join(diff))

def hangman(secretWord):
    '''
    secretWord: string, the secret word to guess.

    Starts up an interactive game of Hangman.

    * At the start of the game, let the user know how many
      letters the secretWord contains.

    * Ask the user to supply one guess (i.e. letter) per round.

    * The user should receive feedback immediately after each guess
      about whether their guess appears in the computers word.

    * After each round, you should also display to the user the
      partially guessed word so far, as well as letters that the
      user has not yet guessed.

    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE...
    print('Welcome to the game, Hangman!')
    print('I am thinking of a word that is ' + str(len(secretWord)) + ' letters long.')
    print('-------------')
    numOfGuesses = 8
    lettersGuessed = list()
    while numOfGuesses > 0:
        print("You have " + str(numOfGuesses) + " guesses left.")
        print("Available letters: " + getAvailableLetters(lettersGuessed))

        var = input("Please guess a letter: ")
        var = var.lower()

        if var in lettersGuessed:
            print("Oops! You've already guessed that letter: " + getGuessedWord(secretWord, lettersGuessed))
        elif var not in secretWord:
            print("Oops! That letter is not in my word: " + getGuessedWord(secretWord, lettersGuessed))
            lettersGuessed.append(var)
            numOfGuesses -= 1
        else:
            lettersGuessed.append(var)
            print("Good Guess: " + getGuessedWord(secretWord, lettersGuessed))
        print("------------")
        if (isWordGuessed(secretWord, lettersGuessed) == True):
            print("Congratulations, you won!")
            return 1
        if (numOfGuesses == 0):
            print("Sorry, you ran out of guesses. The word was " + secretWord)
            return 1
    return 0

# When you've completed your hangman function, uncomment these two lines
# and run this file to test! (hint: you might want to pick your own
# secretWord while you're testing)

secretWord = chooseWord(wordlist).lower()
#secretWord = 'c'
hangman(secretWord)

# Hangman game
#

# -----------------------------------
# Helper code
# You don't need to understand this helper code,
# but you will have to know how to use the functions
# (so be sure to read the docstrings!)

import random
import string

WORDLIST_FILENAME = "/Users/deedeebanh/Documents/MITx_6.00.1.x/ProblemSet3/words.txt"

def loadWords():
    """
    Returns a list of valid words. Words are strings of lowercase letters.

    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    print("  ", len(wordlist), "words loaded.")
    return wordlist

def chooseWord(wordlist):
    """
    wordlist (list): list of words (strings)

    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code
# -----------------------------------

# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = loadWords()

def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    # FILL IN YOUR CODE HERE...

    isAinB = [item in lettersGuessed for item in secretWord]
    return (all(isAinB))


def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE...
    store = list('_'*len(secretWord)) #first set up ___ = length of secretWord
    for i in range(len(secretWord)):
        for j in range(len(lettersGuessed)):
            if lettersGuessed[j] == secretWord[i]:
                store[i] = lettersGuessed[j] #replace _ with the letter
    return (''.join(store))

def getAvailableLetters(lettersGuessed):
    '''
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''
    # FILL IN YOUR CODE HERE...
    diff = [item for item in (list(string.ascii_lowercase)) if item not in lettersGuessed]
    return (''.join(diff))

def hangman(secretWord):
    '''
    secretWord: string, the secret word to guess.

    Starts up an interactive game of Hangman.

    * At the start of the game, let the user know how many
      letters the secretWord contains.

    * Ask the user to supply one guess (i.e. letter) per round.

    * The user should receive feedback immediately after each guess
      about whether their guess appears in the computers word.

    * After each round, you should also display to the user the
      partially guessed word so far, as well as letters that the
      user has not yet guessed.

    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE...
    print('Welcome to the game, Hangman!')
    print('I am thinking of a word that is ' + str(len(secretWord)) + ' letters long.')
    print('-------------')
    numOfGuesses = 8
    lettersGuessed = list()
    while numOfGuesses > 0:
        print("You have " + str(numOfGuesses) + " guesses left.")
        print("Available letters: " + getAvailableLetters(lettersGuessed))

        var = input("Please guess a letter: ")
        var = var.lower()

        if var in lettersGuessed:
            print("Oops! You've already guessed that letter: " + getGuessedWord(secretWord, lettersGuessed))
        elif var not in secretWord:
            print("Oops! That letter is not in my word: " + getGuessedWord(secretWord, lettersGuessed))
            lettersGuessed.append(var)
            numOfGuesses -= 1
        else:
            lettersGuessed.append(var)
            print("Good Guess: " + getGuessedWord(secretWord, lettersGuessed))
        print("------------")
        if (isWordGuessed(secretWord, lettersGuessed) == True):
            print("Congratulations, you won!")
            return 1
        if (numOfGuesses == 0):
            print("Sorry, you ran out of guesses. The word was " + secretWord)
            return 1
    return 0

# When you've completed your hangman function, uncomment these two lines
# and run this file to test! (hint: you might want to pick your own
# secretWord while you're testing)

secretWord = chooseWord(wordlist).lower()
#secretWord = 'c'
hangman(secretWord)

# Hangman game # # ----------------------------------- # Helper code # You don't need to understand this helper code, # but you will have to know how to use the functions # (so be sure to read the docstrings!) Returns a list of valid words. Words are strings of lowercase letters.      Depending on the size of the word list, this function may     take a while to finish. # inFile: file # line: string # wordlist: list of strings wordlist (list): list of words (strings)      Returns a word from wordlist at random # end of helper code # ----------------------------------- # Load the list of words into the variable wordlist # so that it can be accessed from anywhere in the program secretWord: string, the word the user is guessing     lettersGuessed: list, what letters have been guessed so far     returns: boolean, True if all the letters of secretWord are in lettersGuessed;       False otherwise # FILL IN YOUR CODE HERE... secretWord: string, the word the user is guessing     lettersGuessed: list, what letters have been guessed so far     returns: string, comprised of letters and underscores that represents       what letters in secretWord have been guessed so far. # FILL IN YOUR CODE HERE... #first set up ___ = length of secretWord #replace _ with the letter lettersGuessed: list, what letters have been guessed so far     returns: string, comprised of letters that represents what letters have not       yet been guessed. # FILL IN YOUR CODE HERE... secretWord: string, the secret word to guess.      Starts up an interactive game of Hangman.      * At the start of the game, let the user know how many       letters the secretWord contains.      * Ask the user to supply one guess (i.e. letter) per round.      * The user should receive feedback immediately after each guess       about whether their guess appears in the computers word.      * After each round, you should also display to the user the       partially guessed word so far, as well as letters that the       user has not yet guessed.      Follows the other limitations detailed in the problem write-up. # FILL IN YOUR CODE HERE... # When you've completed your hangman function, uncomment these two lines # and run this file to test! (hint: you might want to pick your own # secretWord while you're testing) #secretWord = 'c'