Python 100 project #47: PyBites “Code Challenge 10 – Build a Hangman Game”

This is day 4 of PyBites Code Challenge. This time, it’s good old hangman. I’ve created this hangman a few times with python, but all f them using the different approach. THe game itself is very simple, still there are lots of approach.

 

Output:

$ python3 hangman.py 
###HANGMAN###
I will select one word, so you need to guess which word I selected.


New Game? (Y)es/(N)o: Y
Remaining guess: 7. Current word: _ _ _ _ _ _ _ _ _ ' _   _ _ _ _

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: s
Hit!
Remaining guess: 7. Current word: S _ _ _ _ _ _ _ _ ' s   _ _ s _

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: e
Hit!
Remaining guess: 7. Current word: S _ _ _ _ _ _ e _ ' s   _ _ s _

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: s
It's already guessed.
Remaining guess: 7. Current word: S _ _ _ _ _ _ e _ ' s   _ _ s _

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: i
Hit!
Remaining guess: 7. Current word: S _ _ i _ _ _ e _ ' s   _ i s _

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: r
Hit!
Remaining guess: 7. Current word: S _ _ i _ _ _ e r ' s   _ i s _

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: l
Hit!
Remaining guess: 7. Current word: S _ _ i _ _ l e r ' s   L i s _

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: t
Hit!
Remaining guess: 7. Current word: S _ _ i _ _ l e r ' s   L i s t

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: n
Hit!
Remaining guess: 7. Current word: S _ _ i n _ l e r ' s   L i s t

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: d
Hit!
Remaining guess: 7. Current word: S _ _ i n d l e r ' s   L i s t

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: c
Hit!
Remaining guess: 7. Current word: S c _ i n d l e r ' s   L i s t

        ________      
        |      |      
        |             
        |             
        |             
        |
Your guess?: h
Hit!
Congratulations! You cleared the game.


New Game? (Y)es/(N)o: N
Thank you for playing.

 

Code:

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):

    def __init__(self, word):
        self.word = word
        self.guessed_chars = []
        self.life = ALLOWED_GUESSES

    def submit_char(self, character):
        if character not in self.guessed_chars and character in self.word.lower():
            self.guessed_chars.append(character)
            return "Hit!"
        elif character in self.guessed_chars:
            return "It's already guessed."
        else:
            self.guessed_chars.append(character)
            self.life -= 1
            return "You missed."

    def current_word(self):
        result = []
        for c in self.word:
            if c.isalpha() and c.lower() not in self.guessed_chars:
                c = PLACEHOLDER
            result.append(c)
        return result

    def current_pos(self):
        return HANG_GRAPHICS[ALLOWED_GUESSES - self.life]

    def over(self):
        if self.life <= 0:
            return True
        else:
            return False

    def clear(self):
        if ''.join(self.current_word()) == self.word:
            return True
        else:
            return False


if __name__ == '__main__':

    print("###HANGMAN###")
    print("I will select one word, so you need to guess which word I selected.")
    while True:
        operation = ""
        while operation not in ["Y", "N"]:
            operation = input("\n\nNew Game? (Y)es/(N)o: ")

        if operation == "N":
            print("Thank you for playing.")
            sys.exit(0)

        game = Hangman(get_word())

        while not game.over() and not game.clear():
            print(f"Remaining guess: {game.life}. Current word: {' '.join(game.current_word())}")
            print(game.current_pos())
            result = game.submit_char(input("Your guess?: "))
            print(result)

        if game.clear():
            print("Congratulations! You cleared the game.")
        elif game.over():
            print("You lose...")