'''
Created on Oct 15, 2010
Modified on March 13, 2012
Modified on October 21, 2012

@author: ola
@author: YOU (change me)
'''
import random

# you may need to add global state
# wordlist is all words from dictionary/file
# possiblewords is candidates for secret word, this changes
_wordlist = []
_possiblewords = []

#---------------------------------

def load_words(filename="kwords5.txt"):
    """
    Given the name of a file that contains a list of words, 
    one per line and all the same length, read the contents 
    of the file into a list of strings.
    Returns length of a word contained in the file (all same)
    """
    global _wordlist
    file = open(filename)
    _wordlist = [ word.strip() for word in file ]
    file.close()
    return len(_wordlist[0])

    
def start_game():
    """
    Set up any state that your code needs to play the game. 
    This includes initializing _possiblewords as a copy
    of all the words in _wordlist. You will also need
    to initialize other state needed when a new game starts.
    For example, you could create an empty list to represent the guesses made 
    during a game (the list would be added to by other functions in the
    module).
    Returns number of words the secret word could be.
    """
    global _wordlist, _possiblewords
    if len(_wordlist) == 0:
        load_words()
    _possiblewords = _wordlist[:]
    return len(_possiblewords)


def get_guess():
    """
    Choose a random word from _possiblewords, remove it from
    _possiblewords so it won't be guessed again, and return it.
    Update all state needed to indicate a guess has been made.
    """
    global _possiblewords
    
    return "bagel"

    
def process_common_last(common):
    """
    common is an int, the number of letters in common between the secret word, known
    by the human player and the computer's last guess. 
    Using this information this function changes the list _possiblewords to contain only
    words that contain the proper number of letters in common with the last guess
    Returns number of words remaining that could be secret word, e.g., the new
    length of _possiblewords
    """
    
    global _possiblewords
    return len(_possiblewords)

def guess_count():
    """
    Returns the number of guesses made by the computer since a new
    game was started
    """
    return 3

def commonCount(a,b):
    """
    Returns number of letters in common between given strings a and b,
    no matter where those letters occur within the strings
    """
    alist = list(a)
    blist = list(b)
    for c in alist:
        if c in blist:
            blist[blist.index(c)] = '*'
    return blist.count("*")
            
