-
-
Save sometowngeek/ea748af5f0435f3c22b229400c9e3a7f to your computer and use it in GitHub Desktop.
Method 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 impot specific functions from the library | |
from random import choice | |
from time import sleep | |
HANGMAN = ( | |
""" | |
------ | |
| | | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| -+- | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+- | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | |
| | |
| | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | | |
| | | |
| | | |
| | |
---------- | |
""", | |
""" | |
------ | |
| | | |
| O | |
| /-+-/ | |
| | | |
| | | |
| | | | |
| | | | |
| | |
---------- | |
""") | |
WORDS = ("APPLE", "ORACLE", "MIMO", "TESLA") | |
POSITIVE_SAYINGS = ("Well done!", "Awesome!", "You Legend!") | |
def check_answer(guess, word, so_far, wrong_answers): | |
""" | |
Checks to see if the user's guess is correct. | |
:param guess: User's guess | |
:param word: Current word | |
:param so_far: Current progress | |
:param wrong_answers: Number of incorrect answers | |
:return: updated so_far, updated wrong_answers | |
""" | |
if guess in word: | |
print(choice(POSITIVE_SAYINGS), "...Updating word so far...") | |
for i in range(len(word)): | |
if guess == 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] | |
so_far = so_far[:i] + guess + so_far[i+1:] | |
else: | |
print("INCORRECT! Try again!") | |
wrong_answers += 1 | |
return so_far, wrong_answers | |
def play(): | |
""" | |
This is the main driver of the game. | |
Plays the game. | |
""" | |
word, so_far, used, wrong_answers = reset_game() | |
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 wrong_answers < len(HANGMAN) and so_far != word: | |
print_current_progress(wrong_answers, so_far, used) | |
used, guess = user_guess(used) | |
so_far, wrong_answers = check_answer(guess, word, so_far, wrong_answers) | |
sleep(1) | |
print_result(wrong_answers) | |
play_again() | |
print() | |
print("Thank you for playing!") | |
def play_again(): | |
""" | |
Asks the user if he or she would like to play again. | |
If the user wants to play again, calls play(). | |
:return: | |
""" | |
print("Would you like to play again?") | |
user_input = input("Enter Y for yes or N for no: ").upper() | |
if user_input == "Y": | |
play() | |
def print_current_progress(wrong_answers, so_far, used): | |
""" | |
Prints the current progress of the game. | |
:param wrong_answers: number of wrong answers (used as index for HANGMAN). | |
:param so_far: Current progress. | |
:param used: Letters the user guessed. | |
""" | |
print() | |
print(HANGMAN[wrong_answers]) | |
print("Word so far: ", so_far) | |
print("Letters used: ", sorted(used)) | |
def print_result(wrong_answers): | |
""" | |
Prints the result (win or lose). | |
:param wrong_answers: The amount of incorrect answers. | |
""" | |
print() | |
print("Calculating result...") | |
sleep(1) | |
print() | |
if wrong_answers == len(HANGMAN): | |
print("UNLUCKY! Better luck next time!") | |
else: | |
print("WINNER! Congratulations!") | |
def reset_game(): | |
""" | |
Resets the game by resetting the values and picking a new word. | |
:return: new word, reset so_far, reset used, reset wrong_answers | |
""" | |
word = choice(WORDS) | |
so_far = "-" * len(word) | |
used = [] | |
wrong_answers = 0 | |
return word, so_far, used, wrong_answers | |
def start_game(): | |
print("\t\tWelcome to Hangman!") | |
print() | |
input("Press Enter to START:") | |
def user_guess(used): | |
guess = input("Guess a letter: ").upper() | |
sleep(1) # Time delay - allows userfriendly reading | |
print() | |
while guess in used: | |
print("Try again... You've already used this letter") | |
guess = input("Guess a letter: ").upper() | |
sleep(1) | |
print() | |
used.append(guess) | |
return used, guess | |
if __name__ == '__main__': | |
play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment