def _hangman_popper(self): """print a hangman graph if guess is not right""" guessed_character = set() graphics = hang_graphics() graph = next(graphics) while True: guess = yield False if guess not in guessed_character: guessed_character.add(guess) if guess in self._word: print( f'{colored(len(self._word[guess]),"green")} of {colored(guess,"green")} in the word.' ) for index in self._word.pop(guess): self._guess[index] = guess if not self._word: yield True else: print(f'{colored(guess,"green")} is not in the word!\n' f'{graph}\n') try: graph = next(graphics) except StopIteration: raise NoChance else: print( f'You have guessed {colored(guess,"green")} before, please choose another character.\n' )
def __init__(self, word): self.turn = hang_graphics() self.allowed = len(list(hang_graphics())) self.word = word self.letters = [x for x in self.word.lower()] self.blanks = ['_'] * len(self.word)
"""Play Hangman with movie titles.""" from graphics import hang_graphics from movies import get_movie as get_word # keep interface generic from string import ascii_uppercase from string import digits import sys ASCII = list(ascii_uppercase) HANG_GRAPHICS = list(hang_graphics()) ALLOWED_GUESSES = len(HANG_GRAPHICS) PLACEHOLDER = '_' class Hangman(object): """Define the game.""" def __init__(self, word='password', guess=0, win=False): """Set up the game.""" self.word = word.upper() self.guess = guess self.masked = Hangman.mask_word(self.word) self.chosen = [] self.win = win def mask_word(word): """Create masked word string.""" word_list = word.split() for word, value in enumerate(word_list): mask_word = "" for char in value:
def __init__(self, word): self.word = SecretWord(word.strip()) self.tries_left = ALLOWED_GUESSES self.hang_graphics = hang_graphics() self.guessed_letters = [] self.exit = 'exit'
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
import sys from os import name, system from string import ascii_lowercase from typing import List, Set from graphics import hang_graphics from movies import get_movie as get_word # keep interface generic ASCII: List[str] = list(ascii_lowercase) HANG_GRAPHICS: List[str] = list(hang_graphics()) ALLOWED_GUESSES: int = len(HANG_GRAPHICS) PLACEHOLDER: str = "_" class Hangman(object): def __init__(self, word: str) -> None: self.guessed: Set[str] = set() self.word: str = word self.tries: int = 1 def __str__(self) -> str: return HANG_GRAPHICS[self.tries - 1] + "\n" + "".join(self.mask) def clear_screen(self) -> None: """Clears the screen""" _: int = system("cls" if name == "nt" else "clear") info: str = f"tries: {self.tries}/{ALLOWED_GUESSES}" print(f"{'Welcome to Hangman!':^50}") print() print(f"{info:>50}") print(f"guesses: {', '.join(self.guessed):<50}")
import unittest import graphics GRAPHICS = list(graphics.hang_graphics()) class TestGraphics(unittest.TestCase): def test_len(self): self.assertEqual(len(GRAPHICS), 8) def test_for_non_empty_values(self): for c, graphic in enumerate(GRAPHICS): self.assertIsNotNone(graphic) if __name__ == '__main__': unittest.main()
from string import ascii_lowercase import sys from movies import get_movie as get_word from graphics import hang_graphics ASCII = list(ascii_lowercase) HANG_GRAPHICS = hang_graphics() def check_input_char(char, finish_word, present_word): if char in present_word: print("You already know this character") return True if char in finish_word: for index, character in enumerate(finish_word): if character == char: present_word[index] = character if present_word == finish_word: print(f"You WIN !\nAnswer is: {word}") exit(0) return True def main(word): finish_word = ([char for char in word]) present_word = (['_' if c in ASCII else c for c in word])