def initialise():
##  Initialise all the variables needed
    origWord = ""
    word = ""
    count = 0
    tries = []
    array = []
    wordComp = []
    data = ()
    letter = letters

##  Open file that contains words, one word per line, with '\n' at the end of each line
##  put all words into a tuple named data
    wordFile = open(path, 'r')
    allLines = wordFile.readlines()
    wordFile.close()
    data = list(allLines)

##  Randomly select a word from the data tuple, and put it into a variable called origWord
    numWords = len(data)
    R = int(numWords*random())
    origWord = data[R]

##  Ensure all leters are lowercase
    origWord = origWord.lower()

##  Make a list from the word, and remove the last element (being a '\n')
    wordListOrig = list(origWord)   
    word = wordListOrig[:-1]
    wordList = list(word)

##  Create a "blanks" list, to display the blank chars, and later to store the guesses
    wordListLen = len(wordList)
    for I in range(len(word)):
        wordComp.insert(I, "_")

##  Send the list "array" to graphics to be initialised
    graphics(array)

##  Send vars wordList, wordListLen, wordComp, count, tries, array, letter to the
##  function "gameLogic"
    gameLogic(wordList, wordListLen, wordComp, count, tries, array, letter)


def graphics(array):
##  Create a nested list structure, called Array, being 12 elements X 12 elements
    for I in range(12):
            array.insert(I, [])
            for J in range(12):
                array[I].insert(I, " ")

##  "draw" the gallows, and 'noose'
    for I in range(12):
        array[I][0] = "X"

    for I in range(7):
        array[0][I] = "X"
        
    array[1][6] = "X"
    
    return array


def printArray(array):
##  Collect each row element into a variable, and then print the variable for each row
##  in the whole array
    lines = ""
    print
    for I in range(12):
            lines = ""
            for J in range(12):
                lines = lines + (array[I][J])
            print lines
    print


def printStats(tries, wordComp):
##  Print "statistics" of the game, being tries, and successes
    tryString = ""
    wordCompString = ""
    
    for I in range(len(tries)):
        tryString = tryString + (tries[I]) + ", "

    for I in range(len(wordComp)):
        wordCompString = wordCompString + (wordComp[I]) + " "
        
    print tryString
    print wordCompString

    
def addBodyParts(array, count):
##  Add body parts to the "graphics" array, then send modified array to printArray()
##  Head
    if count == 1:
        for I in range(5, 8):
            array[2][I] = "-"
        array[3][4] = "("
        array[3][5] = "."
        array[3][7] = "."
        array[3][8] = ")"
        for I in range(5, 8):
            array[4][I] = "-"
##  Body
    elif count == 2:
        for I in range(5, 9):
            array[I][6] = "X"

##  Left arm        
    elif count == 3:
        for I in range(3, 7):
            array[I][I-1] = "\\"

##  Right arm            
    elif count == 4:
        array[3][10] = "/"
        array[4][9] = "/"
        array[5][8] = "/"
        array[6][7] = "/"

##  Left leg
    elif count == 5:
        array[9][5] = "/"
        array[10][4] = "/"

##  Right leg
    elif count == 6:
        array[9][7] = "\\"
        array[10][8] = "\\"

##  Right hand
    elif count == 7:
        array[2][10] = "\\"

##  Left hand
    elif count == 8:
        array[2][2] = "/"

##  Left foot
    elif count == 9:
        array[11][9] = "\\"
        array[11][10] = "_"

##  Right foot
    elif count == 10:
        array[11][2] = "_"
        array[11][3] = "/"

##  Goto printArray(), and print the modified array
    printArray(array)


def gameLogic(wordList, wordListLen, wordComp, count, tries, array, letter):
##  Initialise gameplay, print the array, and print statistics
    printArray(array)
    printStats(tries, wordComp)

## Here we go!!! (I can't be bothered cluttering the below code with comments,
## it's not too difficult to figure what's going on...
    while wordList != wordComp and count != 10:
        print
        guess = raw_input("Please enter a letter: ")
        guess = list(guess)
        for X in range(len(guess)):
            if guess[X] != "":
                if guess[X] in letter:
                    if guess[X] not in tries:
                        if guess[X] not in wordList:
                            count = (count + 1)
                            tries.extend(guess[X])
                            addBodyParts(array, count)
                            print "Your guess is not in the word"
                            printStats(tries, wordComp)
                        else:
                            printArray(array)
                            for I in range(wordListLen):
                                if guess[X] == wordList[I]:
                                    wordComp[I] = guess[X]
                            print "your guess is in the word"
                            printStats(tries, wordComp)
                    else:
                        printArray(array)
                        print
                        print "You have already guessed that letter, try again"
                        print
                        printStats(tries, wordComp)
                else:
                    print
                    print "Guess not valid, please enter a lowercase letter"
                    print
            else:
                print
                print "You have not entered anything, please enter a lowercase letter"
                print

    if wordList == wordComp:
        print
        print "You have completed the word"
        print

    charString = ""
    for I in range(wordListLen):
        charString = charString + wordList[I]
    print charString
    

if __name__=="__main__":
##  Import the appropriate python modules (all are standard)
    from random import random
    from string import letters
    from os import path

##  Make a header, giving recognition to those who I have "borrowed" from
    header = ('HANGMAN', 'Inspired by Program in the book:',
            '"BASIC Computer Games - Microcomputer Edition"',
            'Creative Computing', 'Morristown, New Jersey 1981',
              'Edited by: David H. Ahl')
    
##  Print the said header
    stringLen = (len(header[2]) + 30)
    for I in range(len(header)):
        print header[I].center(stringLen)

##  Ensure we know where to get the word list file
    if path.isfile("/usr/share/dict/linux.words") == 1:
        path = "/usr/share/dict/words"
    elif path.isfile("c:\words\linux.words") == 1:
        path = "c:\words\linux.words"
    else:
        path = raw_input("Please enter the path for the wordfile (c:\\temp\linux.words): ")

##  Enter the loop that allows turn, after turn (re-try) gameplay
    restart = ""
    while restart != 'n':
        initialise()
        print
        restart = raw_input("Would you like to try again? (y/n): ")