Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Hangman console version Python 3.4
- # http://pastebin.com/B9E5UEkg
- # get dictionary.txt from github or elsewhere call it "dictionary.txt" in same folder as script
- # https://github.com/dmcguinness/Hangman/blob/master/dictionary.txt
- import random
- import os
- version = "1.1"
- class clsDictionary:
- def __init__(self):
- if os.path.exists("dictionary.txt"): # check if dictionary file in place
- fso = open("dictionary.txt", "r")
- self.wordLength = [] # define list
- for i in range(26): # create 25 lists within the wordLength[] list
- self.wordLength.append([])
- for line in fso: # read each line in dictionary and put into correct list
- if len(line) > 0: # ignore empty lines
- if "-" not in line: # ignore hyphenated words
- self.wordLength[len(line)].append(line)
- fso.close() # close text file
- self.report = "" # no error so nothing to report
- else: # file missing
- self.report = "File 'dictionary.txt' not found"
- def getError(self): # property get: error text
- return self.report
- def getWord(self, length): # method: get a word from the dictionary
- returnValue = ""
- if length > 0 and length < 26: # word must be between 1 and 26 characters
- listLength = len(self.wordLength[length])
- if listLength > 0:
- return self.wordLength[length][random.randrange(listLength)].upper() # get word in uppercase
- else:
- self.report = "Error: word length requested == 0 or > 24"
- return returnValue
- class clsHangman:
- #0_________________
- #1| _________ |
- #2| | / | |
- #3| |/ O |
- #4| | /|\ |
- #5| | | |
- #6| | / \ |
- #7| ___|___ |
- #8|_______________|
- def __init__(self):
- self.stage = 0
- self.lettersAvailable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- self.lettersChosen = ""
- self.completed = False
- self.progress = ""
- self.fail = False
- self.word = ""
- self.row = [[],[],[],[],[],[],[],[],[]] # setup array elements for graphics
- for i in range(9):
- for j in range(12):
- self.row[i].append([])
- #stage 0 - new game
- self.row[0][0] = "_________________"
- self.row[1][0] = "| |"
- self.row[2][0] = "| |"
- self.row[3][0] = "| |"
- self.row[4][0] = "| |"
- self.row[5][0] = "| |"
- self.row[6][0] = "| |"
- self.row[7][0] = "| |"
- self.row[8][0] = "|_______________|"
- # stage 1 - 1 mistake
- self.row[0][1] = "_________________"
- self.row[1][1] = "| |"
- self.row[2][1] = "| |"
- self.row[3][1] = "| |"
- self.row[4][1] = "| |"
- self.row[5][1] = "| |"
- self.row[6][1] = "| |"
- self.row[7][1] = "| _______ |"
- self.row[8][1] = "|_______________|"
- # stage 2 - 2 mistakes
- self.row[0][2] = "_________________"
- self.row[1][2] = "| |"
- self.row[2][2] = "| | |"
- self.row[3][2] = "| | |"
- self.row[4][2] = "| | |"
- self.row[5][2] = "| | |"
- self.row[6][2] = "| | |"
- self.row[7][2] = "| ___|___ |"
- self.row[8][2] = "|_______________|"
- self.row[0][3] = "_________________"
- self.row[1][3] = "| _________ |"
- self.row[2][3] = "| | / |"
- self.row[3][3] = "| |/ |"
- self.row[4][3] = "| | |"
- self.row[5][3] = "| | |"
- self.row[6][3] = "| | |"
- self.row[7][3] = "| ___|___ |"
- self.row[8][3] = "|_______________|"
- self.row[0][4] = "_________________"
- self.row[1][4] = "| _________ |"
- self.row[2][4] = "| | / | |"
- self.row[3][4] = "| |/ |"
- self.row[4][4] = "| | |"
- self.row[5][4] = "| | |"
- self.row[6][4] = "| | |"
- self.row[7][4] = "| ___|___ |"
- self.row[8][4] = "|_______________|"
- self.row[0][5] = "_________________"
- self.row[1][5] = "| _________ |"
- self.row[2][5] = "| | / | |"
- self.row[3][5] = "| |/ O |"
- self.row[4][5] = "| | |"
- self.row[5][5] = "| | |"
- self.row[6][5] = "| | |"
- self.row[7][5] = "| ___|___ |"
- self.row[8][5] = "|_______________|"
- self.row[0][6] = "_________________"
- self.row[1][6] = "| _________ |"
- self.row[2][6] = "| | / | |"
- self.row[3][6] = "| |/ O |"
- self.row[4][6] = "| | / |"
- self.row[5][6] = "| | |"
- self.row[6][6] = "| | |"
- self.row[7][6] = "| ___|___ |"
- self.row[8][6] = "|_______________|"
- self.row[0][7] = "_________________"
- self.row[1][7] = "| _________ |"
- self.row[2][7] = "| | / | |"
- self.row[3][7] = "| |/ O |"
- self.row[4][7] = "| | /| |"
- self.row[5][7] = "| | |"
- self.row[6][7] = "| | |"
- self.row[7][7] = "| ___|___ |"
- self.row[8][7] = "|_______________|"
- self.row[0][8] = "_________________"
- self.row[1][8] = "| _________ |"
- self.row[2][8] = "| | / | |"
- self.row[3][8] = "| |/ O |"
- self.row[4][8] = "| | /|" + chr(92) + " |" # backspace causes printing errors so use ascii code
- self.row[5][8] = "| | |"
- self.row[6][8] = "| | |"
- self.row[7][8] = "| ___|___ |"
- self.row[8][8] = "|_______________|"
- self.row[0][9] = "_________________"
- self.row[1][9] = "| _________ |"
- self.row[2][9] = "| | / | |"
- self.row[3][9] = "| |/ O |"
- self.row[4][9] = "| | /|" + chr(92) + " |"
- self.row[5][9] = "| | | |"
- self.row[6][9] = "| | |"
- self.row[7][9] = "| ___|___ |"
- self.row[8][9] = "|_______________|"
- self.row[0][10] = "_________________"
- self.row[1][10] = "| _________ |"
- self.row[2][10] = "| | / | |"
- self.row[3][10] = "| |/ O |"
- self.row[4][10] = "| | /|" + chr(92) + " |"
- self.row[5][10] = "| | | |"
- self.row[6][10] = "| | / |"
- self.row[7][10] = "| ___|___ |"
- self.row[8][10] = "|_______________|"
- # stage 11 - 11 mistakes - end of game
- self.row[0][11] = "_________________"
- self.row[1][11] = "| _________ |"
- self.row[2][11] = "| | / | |"
- self.row[3][11] = "| |/ O |"
- self.row[4][11] = "| | /|" + chr(92) + " |"
- self.row[5][11] = "| | | |"
- self.row[6][11] = "| | / " + chr(92) + " |"
- self.row[7][11] = "| ___|___ |"
- self.row[8][11] = "|_______________|"
- def printStage(self): # method: print graphics for current stage
- for i in range(9):
- print(self.row[i][self.stage])
- wordOut = ""
- for i in range(len(self.progress)):
- wordOut = wordOut + " " + self.progress[i]
- print(wordOut)
- def setWord(self, newWord): # property set: use word from clsDictionary object
- self.word = newWord
- self.progress = ""
- for i in range(len(newWord) - 1): # create a string of underscores as placeholders for letters
- self.progress = self.progress + '_'
- def getWord(self): # property get: current word used in game
- return self.word
- def getLettersAvailable(self): # property get: list of letters mot yet tried
- return self.lettersAvailable
- def getLettersChosen(self): # property get: list of letters already tried
- return self.lettersChosen
- def getCompleted(self): # property get: game complete, either pass or fail
- return self.completed
- def checkGuess(self, guess): # method: check letter entered by user
- if guess not in self.lettersChosen: # letter not used before
- if guess in self.word: # letter is in game word!
- #add letter to self.progress (replace underscore characters with letters)
- position = self.word.find(guess) # -1 = not found, 0 = beginning
- while position >= 0: # found in game word
- if position == 0: # first letter of word, so substitute letter for first underscore
- front = guess
- middle = ""
- end = self.progress[1:]
- else: # not first letter, calculate new string with letter in place of underscore
- front = self.progress[:position]
- middle = guess
- if position == len(self.word) - 1: # last letter. len() is larger by 1, and position starts at 0, so -2 needed
- end = ""
- else:
- end = self.progress[position + 1:]
- self.progress = front + middle + end
- position = self.word.find(guess, position + 1)
- if '_' not in self.progress: # game completed as all underscores are gone
- self.completed = True
- else:
- #draw next stage of hangman
- if self.stage == 10: # game over, word not found
- self.fail = True
- self.completed = True
- self.stage = self.stage + 1 # make sure next / final graphic is drawn
- self.lettersChosen = ''.join(sorted(self.lettersChosen + guess)) # add last letter to list, sort list and return
- position = self.lettersAvailable.find(guess) # remove last letter from list of available letters
- self.lettersAvailable = self.lettersAvailable[:position] + self.lettersAvailable[position + 1:]
- def getFail(self): # property get: game over with fail :(
- return self.fail
- def printMenu(wordlist, game):
- os.system('cls') # clear screen
- print("Welcome to Hangman version " + version)
- game.setWord(wordlist.getWord(random.randrange(5,15))) # get a word from the dictionary
- print("Guess the " + str(len(game.getWord()) - 1) + " letter word I have chosen")
- def play(game):
- while True:
- game.printStage()
- print("Letters Used:" + game.getLettersChosen())
- print("Letters Available: " + game.getLettersAvailable())
- print
- while True:
- guess = input("Choose a letter: ")
- if len(guess) > 0:
- if (ord(guess) > 64 and ord(guess) < 91) or (ord(guess) > 96 and ord(guess) < 123):
- guess = guess.upper()
- break
- game.checkGuess(guess)
- if game.getCompleted():
- break
- def main():
- wordlist = clsDictionary() # create instance of clsDictionary, which will read text file and sort into lists by word length
- if wordlist.getError() == "": # no errors opening dictionary
- while True:
- game = clsHangman() # create instance of clsHangman as a game object
- printMenu(wordlist, game) # print introduction and get word from dictionary object
- play(game) # play the game
- game.printStage() # print final stage
- if game.getFail():
- print ("Sorry you did not guess the word")
- print("it was: ", game.getWord())
- else:
- print("Well Done!")
- answer = input("Do you want to play again? Y/N >")
- if answer.upper() != "Y": # any key to quit, Y to continue
- os.system('cls')
- break
- else: # error opening dictionary. ? deleted/moved/renamed
- print(wordlist.getError())
- # program runs from here
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement