Created
October 31, 2012 09:59
-
-
Save eaorak/3986188 to your computer and use it in GitHub Desktop.
Python - Hangman Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Hangman game | |
import random | |
class HangMan(object): | |
# Hangman game | |
hang = [] | |
hang.append(' +---+') | |
hang.append(' | |') | |
hang.append(' |') | |
hang.append(' |') | |
hang.append(' |') | |
hang.append(' |') | |
hang.append('=======') | |
man = {} | |
man[0] = [' 0 |'] | |
man[1] = [' 0 |', ' | |'] | |
man[2] = [' 0 |', '/| |'] | |
man[3] = [' 0 |', '/|\\ |'] | |
man[4] = [' 0 |', '/|\\ |', '/ |'] | |
man[5] = [' 0 |', '/|\\ |', '/ \\ |'] | |
pics = [] | |
words = '''ant baboon badger bat bear beaver camel cat clam cobra cougar coyote | |
crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama | |
mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram | |
rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger | |
toad trout turkey turtle weasel whale wolf wombat zebra'''.split() | |
infStr='_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'*-_-*\'' | |
def __init__(self, *args, **kwargs): | |
i, j = 2, 0 | |
self.pics.append(self.hang[:]) | |
for ls in self.man.values(): | |
pic, j = self.hang[:], 0 | |
for m in ls: | |
pic[i + j] = m | |
j += 1 | |
self.pics.append(pic) | |
def pickWord(self): | |
return self.words[random.randint(0, len(self.words) - 1)] | |
def printPic(self, idx, wordLen): | |
for line in self.pics[idx]: | |
print(line) | |
def askAndEvaluate(self, word, result, missed): | |
guess = input() | |
if guess == None or len(guess) != 1 or (guess in result) or (guess in missed): | |
return None, False | |
i = 0 | |
right = guess in word | |
for c in word: | |
if c == guess: | |
result[i] = c | |
i += 1 | |
return guess, right | |
def info(self, info): | |
ln=len(self.infStr) | |
print(self.infStr[:-3]) | |
print(info) | |
print(self.infStr[3:]) | |
def start(self): | |
print('Welcome to Hangman !') | |
word = list(self.pickWord()) | |
result = list('*' * len(word)) | |
print('The word is: ', result) | |
success, i, missed = False, 0, [] | |
while i < len(self.pics)-1: | |
print('Guess the word: ', end='') | |
guess,right = self.askAndEvaluate(word, result, missed) | |
if guess == None: | |
print('You\'ve already entered this character.') | |
continue | |
print(''.join(result)) | |
if result == word: | |
self.info('Congratulations ! You\'ve just saved a life !') | |
success = True | |
break | |
if not right: | |
missed.append(guess) | |
i+=1 | |
self.printPic(i, len(word)) | |
print('Missed characters: ', missed) | |
if not success: | |
self.info('The word was \''+''.join(word)+'\' ! You\'ve just killed a man, yo !') | |
a = HangMan().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
It's just a simple hangman game.