Skip to content

Instantly share code, notes, and snippets.

@Proteusiq
Created December 7, 2018 15:50
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 Proteusiq/e486372ae739ba2d3e7b91104770d2e4 to your computer and use it in GitHub Desktop.
Save Proteusiq/e486372ae739ba2d3e7b91104770d2e4 to your computer and use it in GitHub Desktop.
hangman2.py
import cmd
import random
import re
from hangword import words
class Hang(cmd.Cmd):
'''Command Line Hangman
Enter one letter or number per round.
Upper and lower cases are consider differently.
You have 6 attempts
Type:
play(p) to Play
quit to Quit
help to Help Menu
'''
prompt = '> '
def __init__(self):
self.count = 0
self.used = []
print('\n'*100)
print(self.__doc__)
super(Hang, self).__init__()
def win(self):
print('\nBravo! You rock!')
# reset count
self.count = 0
self.cmdloop()
def lost(self):
''''Hanged! Game Over'
'''
if self.count > 5:
self.hanger()
print(self.lost.__doc__)
print(f'\n The word was: {self.reveal}\n')
# reset count
self.count = 0
self.used = []
self.post_game()
else:
print(f'\n{6-self.count} attempt{"s" if self.count < 5 else ""} remaining')
def post_game(self):
'''Type:
play(p) to Continue
quit(q) to Exit
help for Help Menu
'''
print(self.post_game.__doc__)
self.cmdloop()
def replacer(self):
if not self.position:
self.position = []
while self.guess in self.selected:
self.position.append(self.selected.index(self.guess))
self.selected = self.selected.replace(self.guess, '_', 1)
return self.position
def hang(self):
self.guess = input('\nGuess a letter or a number:\n')
if self.guess.lower() == 'quit':
self.do_quit()
try:
assert len(self.guess) == 1
except AssertionError:
print('\nOnly one letter or number allowed:')
self.hang()
self.used.append(self.guess)
print(f'\n {"Used" if self.used else ""}: {sorted(self.used)}')
if self.guess in self.selected:
print(f'\n:) {self.guess} is here!')
self.position = self.replacer()
for i in self.position:
self.dash = f'{self.dash[:i]}{self.guess}{self.dash[i+1:]}'
else:
print(f'\nNope! {self.guess} is not there')
self.count+=1
self.position = []
print(self.dash)
return self.dash
def do_play(self,args):
'''Play Hangman
You have six wrong attempts to avoid hanging.
'''
self.category = {'words':words}
self.selected = random.choice(self.category['words'])
self.reveal = self.selected
self.dash = re.sub('\w','_',self.selected)
#print(self.selected)
self.position = None
print('\nHit:'
f'\n\tPopular Hangman Words with {len(self.reveal)} characters\n{self.dash}')
while '_' in self.dash:
self.hanger()
self.dash = self.hang()
self.lost()
self.win()
def do_quit(self, args):
'''Quit
Typing exit will end the game
'''
print('Thanks for playing!')
quit()
def hanger(self):
'''
Hangman Art :)
'''
_top = '\t ______\n\t !\t\t |'
_ground = '_'
_space = ' '
_pole = '\n\t\t\t |'
_head = '\n\t(ツ)\t |' if self.count > 0 else _pole
_belly = '|' if self.count > 1 else ' '
_left_h = '_/' if self.count > 2 else _space
_right_h = '\_' if self.count > 3 else _space
_left_l = _left_h if self.count > 4 else _space
_right_l = _right_h if self.count > 5 else _space
_body = (f'\n\t{_left_h}{_belly}{_right_h}\t |'
f'\n\t{_left_l} {_right_l}\t |')
print(f'{_top}{_head}{_body}{_pole*2}{_ground*5}')
# alias
do_p = do_play
do_q = do_quit
if __name__=='__main__':
game = Hang()
game.cmdloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment