def __init__(self): self.guesses = 0 self.used_letters = [] self.secret_word = list(get_word().lower()) self.to_guess_word = [ PLACEHOLDER if char in ASCII else char for char in self.secret_word ] self.start_game()
def __init__(self, word=None): if word is None: word = get_word() self.word = word self.secret_word = list(word.lower()) self.allowed_characters = ASCII self.guessed_letters = [] self.allowed_guesses = ALLOWED_GUESSES self.graphics_counter = 0 self.won = False self.game_over = False
def new_game(self): """ Ask if the user wants to play a new game reinitialize the game or if not then quit the game. """ response = input('Would you like to play a again? ') if response in {'Yes', 'yes', 'Y', 'y'}: self.__init__(get_word()) elif response in {'No', 'no', 'N', 'n'}: sys.exit(1) else: self.new_game()
def __init__(self, word=None): """Setup: get word or pick random one, set a placeholders list, ascii only, skip (display) punctuation, init state vars""" if word is None: word = get_word() self.secret_word = list(word.lower()) self.guessed_word = [PLACEHOLDER if c in ASCII else c for c in self.secret_word] # status variables self.num_guesses = 0 self.num_wrong_guesses = 0 self.letters_used = set() self.wrong_letters = set()
def guess(self, character: str): print(f'You guessed {colored(character,"green")}.') result = self._hangman.send(character.lower()) return result class NoChance(Exception): pass if __name__ == '__main__': if len(sys.argv) > 1: word = sys.argv[1] else: word = get_word() # init / call program han_man = Hangman(word) while True: print(f'Current puzzle: {han_man.current_puzzle()}') input_word = input('Please input the character: ')[0] try: if han_man.guess(input_word): print( f'You did it! The right answer is: \n{han_man.current_puzzle()}' ) break except NoChance: print('You have used up all your chance. Here is the answer:') print(f'{han_man.current_puzzle(result=True)}')
if letter in self.word.open.lower(): self.word.open_letter(letter) self.guessed_letters += [letter] else: self.tries_left -= 1 print(next(self.hang_graphics), end='\n\n') self.guessed_letters += [letter] def print_result(self): if PLACEHOLDER not in self.word.secret: print('You win!') print('Word: ', self.word.open) else: print('You lose') print('Word: ', self.word.open) def _print_secret(self): print(*self.word.secret, end='\n\n') def _get_letter(self): letter = input('input letter: ').lower() if letter in ASCII and len(letter) == 1 or letter == self.exit: return letter if __name__ == '__main__': word = sys.argv[1] if len(sys.argv) > 1 else get_word() hangman = Hangman(word) hangman.play() hangman.print_result()
from string import ascii_lowercase import sys from movies import get_movie as get_word # keep interface generic from graphics import hang_graphics ASCII = list(ascii_lowercase) HANG_GRAPHICS = list(hang_graphics()) ALLOWED_GUESSES = len(HANG_GRAPHICS) PLACEHOLDER = '_' class Hangman(object): pass # or use functions ... if __name__ == '__main__': if len(sys.argv) > 1: word = sys.argv[1] else: word = get_word() print(word) # init / call program
def reset(self) -> None: self.word = get_word() self.guessed = set() self.tries = 1 self.clear_screen()
print("\t* Incorrect Guesses: ", str(self.guess_no), "/", str(ALLOWED_GUESSES), " *") print("\t* ", HANG_GRAPHICS[self.guess_no]) print("\n") if (self.check_win()): return False elif (self.check_loss()): return False else: return True if __name__ == '__main__': if len(sys.argv) > 1: solution = sys.argv[1] else: solution = get_word() while True: game = Hangman(solution) play_again = input("\t* Would You Like to Play again [yY/nN] ? ") if play_again not in ["Y", "y"]: if play_again not in ["N", "n"]: print("\t* You failed a simple question, no games for you!") exit() else: print("\t* Thanks For Playing.") exit() else: solution = get_word()