-
-
Save sometowngeek/f399f71d0a97e60af32d748a1b9149cf to your computer and use it in GitHub Desktop.
Class style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Whenever possible, only import specific functions from the library | |
from random import choice | |
from time import sleep | |
class Hangman: | |
""" | |
A Hangman class. | |
""" | |
_HANGMAN = ( | |
""" | |
------ | |
| | | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| -+- | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+- | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | | |
| | | |
| | | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | | |
| | | | |
| | | | |
| | |
---------- | |
""") | |
_WORDS = ("APPLE", "ORACLE", "MIMO", "TESLA") | |
_POSITIVE_SAYINGS = ("Well done!", "Awesome!", "You Legend!") | |
def __init__(self): | |
""" | |
The Python constructor for this class. | |
""" | |
self._word = choice(self._WORDS) | |
self._so_far = "-" * len(self._word) | |
self._used = [] | |
self._wrong_answers = 0 | |
def play(self): | |
""" | |
This is the main driver of the game. | |
Plays the game. | |
""" | |
self._reset_game() | |
self._start_game() | |
# The amount of incorrect answers should be no greater than the length | |
# of HANGMAN. | |
# | |
# Use the length of HANGMAN to ensure there's no index | |
# overflow error when printing result. | |
while self._wrong_answers < len(self._HANGMAN) and self._so_far != self._word: | |
self._print_current_progress() | |
guess = self._user_guess() | |
self._check_answer(guess) | |
self._print_result() | |
self._play_again() | |
# --------------------------------- | |
# "Private" methods | |
def _check_answer(self, guess): | |
""" | |
Checks to see if the user's guess is correct. | |
:param guess: User's guess | |
""" | |
if guess in self._word: | |
print(choice(self._POSITIVE_SAYINGS), "...Updating word so far...") | |
for i in range(len(self._word)): | |
if guess == self._word[i]: | |
# so_far is spliced this way: | |
# so_far [from the start : up until, but not including the | |
# position of the correctly guessed letter] | |
# + guessed letter | |
# + so_far [from the position next to the | |
# correctly guessed letter : to the end] | |
self._so_far = self._so_far[:i] + guess + self._so_far[i+1:] | |
else: | |
print("INCORRECT! Try again!") | |
self._wrong_answers += 1 | |
def _play_again(self): | |
""" | |
Asks the user if he or she would like to play again. | |
If the user wants to play again, calls play(). | |
Otherwise, thanks the user for playing. | |
""" | |
print("Would you like to play again?") | |
user_input = input("Enter Y for yes or N for no: ").upper() | |
if user_input == "Y": | |
self.play() | |
else: | |
print() | |
print("Thank you for playing!") | |
def _print_current_progress(self): | |
""" | |
Prints the current progress of the game. | |
""" | |
print() | |
print(self._HANGMAN[self._wrong_answers]) | |
print("Word so far: ", self._so_far) | |
print("Letters used: ", sorted(self._used)) | |
def _print_result(self): | |
""" | |
Prints the result (win or lose). | |
""" | |
sleep(1) | |
print() | |
print("Calculating result...") | |
sleep(1) | |
print() | |
if self._wrong_answers == len(self._HANGMAN): | |
print("UNLUCKY! Better luck next time!") | |
else: | |
print("WINNER! Congratulations!") | |
def _reset_game(self): | |
""" | |
Resets the game by calling the constructor. | |
""" | |
self.__init__() | |
def _start_game(self): | |
""" | |
Starts the game by printing an introduction | |
and asks the user to hit the ENTER key to continue. | |
""" | |
print() | |
print("\t\tWelcome to Hangman!") | |
print() | |
input("Press Enter to START:") | |
def _user_guess(self): | |
""" | |
Asks for the user to guess a letter. | |
:returns: User's guessed letter. | |
""" | |
guess = input("Guess a letter: ").upper() | |
sleep(1) # Time delay - allows user friendly reading | |
print() | |
while guess in self._used: | |
print("Try again... You've already used this letter") | |
guess = input("Guess a letter: ").upper() | |
sleep(1) | |
print() | |
self._used.append(guess) | |
return guess | |
if __name__ == '__main__': | |
game = Hangman() | |
game.play() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Whenever possible, only import specific functions from the library | |
from random import choice | |
from time import sleep | |
class Hangman: | |
""" | |
A Hangman class. | |
""" | |
_HANGMAN = ( | |
""" | |
------ | |
| | | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| -+- | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+- | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | | |
| | | |
| | | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | | |
| | | | |
| | | | |
| | |
---------- | |
""") | |
_WORDS = ("APPLE", "ORACLE", "MIMO", "TESLA") | |
_POSITIVE_SAYINGS = ("Well done!", "Awesome!", "You Legend!") | |
def __init__(self): | |
""" | |
The Python constructor for this class. | |
""" | |
self._word = choice(self._WORDS) | |
self._so_far = "-" * len(self._word) | |
self._used = [] | |
self._wrong_answers = 0 | |
def play(self): | |
""" | |
This is the main driver of the game. | |
Plays the game. | |
""" | |
self._reset_game() | |
self._start_game() | |
# The amount of incorrect answers should be no greater than the length | |
# of HANGMAN. | |
# | |
# Use the length of HANGMAN to ensure there's no index | |
# overflow error when printing result. | |
while self._wrong_answers < len(self._HANGMAN) and self._so_far != self._word: | |
self._print_current_progress() | |
guess = self._user_guess() | |
self._check_answer(guess) | |
self._wait() | |
self._print_result() | |
self._play_again() | |
# --------------------------------- | |
# "Private" methods | |
def _check_answer(self, guess): | |
""" | |
Checks to see if the user's guess is correct. | |
:param guess: User's guess | |
""" | |
if guess in self._word: | |
print(choice(self._POSITIVE_SAYINGS), "...Updating word so far...") | |
position = self._word.find(guess, 0) | |
while position >= 0: | |
self._so_far = self._so_far[:position] + guess + self._so_far[position+1:] | |
position = self._word.find(guess, position + 1) | |
else: | |
print("INCORRECT! Try again!") | |
self._wrong_answers += 1 | |
def _play_again(self): | |
""" | |
Asks the user if he or she would like to play again. | |
If the user wants to play again, calls play(). | |
Otherwise, thanks the user for playing. | |
""" | |
print("Would you like to play again?") | |
if input("Enter Y for yes or N for no: ").upper() == "Y": | |
self.play() | |
else: | |
print() | |
print("Thank you for playing!") | |
def _print_current_progress(self): | |
""" | |
Prints the current progress of the game. | |
""" | |
print() | |
print(self._HANGMAN[self._wrong_answers]) | |
print("Word so far: ", self._so_far) | |
print("Letters used: ", sorted(self._used)) | |
def _print_result(self): | |
""" | |
Prints the result (win or lose). | |
""" | |
self._wait() | |
print("Calculating result...") | |
self._wait() | |
if self._wrong_answers == len(self._HANGMAN): | |
print("UNLUCKY! Better luck next time!") | |
else: | |
print("WINNER! Congratulations!") | |
def _reset_game(self): | |
""" | |
Resets the game by calling the constructor. | |
""" | |
self.__init__() | |
def _start_game(self): | |
""" | |
Starts the game by printing an introduction | |
and asks the user to hit the ENTER key to continue. | |
""" | |
print() | |
print("\t\tWelcome to Hangman!") | |
print() | |
input("Press Enter to START:") | |
def _user_guess(self): | |
""" | |
Asks for the user to guess a letter. | |
:returns: User's guessed letter. | |
""" | |
guess = input("Guess a letter: ").upper() | |
while guess in self._used: | |
self._wait() | |
print("Try again... You've already used this letter") | |
guess = input("Guess a letter: ").upper() | |
self._wait() | |
self._used.append(guess) | |
return guess | |
def _wait(self): | |
sleep(1) # Time delay - allows user friendly reading | |
print() | |
if __name__ == '__main__': | |
game = Hangman() | |
game.play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment