Skip to content

Instantly share code, notes, and snippets.

@magical
Last active April 25, 2018 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magical/14d254592448525644cf9f2a2a3747da to your computer and use it in GitHub Desktop.
Save magical/14d254592448525644cf9f2a2a3747da to your computer and use it in GitHub Desktop.
LUG hangman, team 2
# Team 2
from __future__ import print_function
def prompt(question):
question = question.lstrip(' ') + ' '
answer = raw_input(question).strip()
while not answer:
answer = raw_input(question).strip()
return answer
class Hangman:
def __init__(self, numLetters):
self.numLetters = numLetters
self.guessed = set()
self._numWrong = 0
self.vowels = True #want to guess vowels first
self.word = ['_']*numLetters
def guess(self):
"""returns a letter guess"""
if float(len(self.guessed))/float(self.numLetters) > (.381):
self.vowels = False
if self.vowels:
for v in ['e', 'a', 'o', 'i', 'u']:
if not v in self.guessed:
self.guessed.add(v)
return v
self.vowels = False
letterfreq = ['t','n','s','h','r','d','l','c','m','w','f','g','y','p','b','v','k','j','x','q']
for x in letterfreq:
if not x in self.guessed:
self.guessed.add(x)
return x
return 'z'
def wrong(self, letter):
"""called after an incorrect guess"""
self._numWrong += 1
def numWrong(self):
return self._numWrong
def correct(self, letter, positions):
"""called with a list of positions after a correct guess"""
for pos in positions:
if not 1 <= pos <= self.numLetters:
print("invalid position:", pos)
continue
if self.word[pos-1] != '_':
print("already guessed position", pos)
continue
self.word[pos-1] = letter
def done(self):
""""returns true when the game is over"""
return self.won() or self.numWrong() >= 6
def positions(self):
"""returns the list of unguessed positions"""
return [i+1 for i, c in enumerate(self.word) if c == '_']
def won(self):
return all(c != '_' for c in self.word)
def build_dict():
fin = open('/usr/share/dict/words')
words = dict()
index = 0
for line in fin:
words[line.strip()] = index
index += 1
fin.close()
return words
def invert_dict(d):
inverse = dict()
for key in d:
inverse.setdefault(d[key], []).append(key)
return inverse
def main():
print("Welcome to hangman")
print()
# startup: ask number of letters
n = prompt("How many letters is your word?")
numLetters = int(n)
ai = Hangman(numLetters)
while not ai.done():
hangman = hangmen[ai.numWrong()]
print(graphics)
print(hangman)
print()
print(" ".join(ai.word))
letter = ai.guess()
answer = prompt("Does it contain an "+letter+"? [y/n]")
if answer.startswith("y"):
left_positions = ",".join(map(str, ai.positions()))
positions = prompt("What positions? [%s]" % left_positions)
try:
positions = map(int, positions.split(","))
except ValueError:
print("not an int")
ai.correct(letter, positions)
else:
print(":(")
ai.wrong(letter)
print()
if ai.won():
print("Boo-yah!")
else:
print("Ded :(")
print()
print("Thanks for playing.")
graphics=r'''
888
888
888
88888b. 8888b. 88888b. .d88b. 88888b.d88b. 8888b. 88888b.
888 "88b "88b888 "88bd88P"88b888 "888 "88b "88b888 "88b
888 888.d888888888 888888 888888 888 888.d888888888 888
888 888888 888888 888Y88b 888888 888 888888 888888 888
888 888"Y888888888 888 "Y88888888 888 888"Y888888888 888
888
Y8b d88P
"Y88P" '''
hangman6 = r'''
___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \
| | || `/,|
| | (\\`_.'
| | .-`--'.
| | /Y . . Y\
| | // | | \\
| | // | . | \\
| | ') | | (`
| | || ||
| | || ||
| | || ||
| | || ||
| | / | | \
""""""""""|_`-' `-' |"""|
|"|"""""""\ \ '"|"|
| | \ \ | |
: : \ \ : :
. . `' . .'''
hangman5 = r'''
___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \
| | || `/,|
| | (\\`_.'
| | .-`--'.
| | /Y . . Y\
| | // | | \\
| | // | . | \\
| | ') | | (`
| | ||
| | ||
| | ||
| | ||
| | / |
""""""""""|_`-' `-' |"""|
|"|"""""""\ \ '"|"|
| | \ \ | |
: : \ \ : :
. . `' . .'''
hangman4 = r'''
___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \
| | || `/,|
| | (\\`_.'
| | .-`--'.
| | /Y . . Y\
| | // | | \\
| | // | . | \\
| | ') | | (`
| |
| |
| |
| |
| |
""""""""""|_`-' `-' |"""|
|"|"""""""\ \ '"|"|
| | \ \ | |
: : \ \ : :
. . `' . .'''
hangman3 = r'''
___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \
| | || `/,|
| | (\\`_.'
| | .-`--'.
| | /Y . .
| | // | |
| | // | . |
| | ') | |
| |
| |
| |
| |
| |
""""""""""|_`-' `-' |"""|
|"|"""""""\ \ '"|"|
| | \ \ | |
: : \ \ : :
. . `' . .'''
hangman2 = r'''
___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \
| | || `/,|
| | (\\`_.'
| | .-`--'.
| | . .
| | | |
| | | . |
| | | |
| |
| |
| |
| |
| |
""""""""""|_`-' `-' |"""|
|"|"""""""\ \ '"|"|
| | \ \ | |
: : \ \ : :
. . `' . .'''
hangman1 = r'''
___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \
| | || `/,|
| | (\\`_.'
| | --
| |
| |
| |
| |
| |
| |
| |
| |
| |
""""""""""|_`-' `-' |"""|
|"|"""""""\ \ '"|"|
| | \ \ | |
: : \ \ : :
. . `' . .
'''
hangman0 = r'''
___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||
| |/ ||
| | ||
| | (\\)
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
""""""""""|_`-' `-' |"""|
|"|"""""""\ \ '"|"|
| | \ \ | |
: : \ \ : :
. . `' . .
'''
hangmen = [hangman6, hangman5, hangman4, hangman3, hangman2, hangman1, hangman0]
hangmen.reverse()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment