Last active
October 12, 2021 18:36
-
-
Save SyntaxError843/0db69298ad6a62634c2dc93e67ec8c24 to your computer and use it in GitHub Desktop.
Simple Hangman Game
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
# Hangman is a paper and pencil guessing game for 2 or more players. One player thinks of a word, | |
# Phrase or sentence and the other tries to guess it by suggesting letters, within a certain number of guesses. | |
# You are the moderator. Given an entry word or sentence, check the input letters in order. If the entry is completed, | |
# the game is won: display the resulting handman and the final entry. | |
# The player is allowed 5 mistakes, corresponding to hangman's head, body, left arm, right arm, left leg. At the 6th | |
# Mistake the right leg is drawn and the game is over. if a letter is repeated more than once, the repeating occurrences | |
# Are always considered as an error, even if the first time was correct. | |
import os; | |
import re; | |
import random; | |
# Function to clear console | |
clear_console = lambda: os.system('cls' if os.name in ('nt', 'dos') else 'clear'); | |
# Function to detect if char is valid | |
def is_valid_char( char ) : | |
return ( char.isalpha() ); | |
# Initialize list of possible entry words | |
possible_entry_words = [ | |
'Cheesecake', | |
'Hello World', | |
'I <3 Python', | |
'This Took Hours!!!', | |
'I\'m Running Out of Things to Say!', | |
# Feel free to add more! | |
]; | |
# Initialize entry word | |
entry_word = possible_entry_words[ random.randint( 0, len( possible_entry_words ) - 1 ) ]; | |
# Get blank version of entry word | |
blank_entry = re.sub( '[a-z]', '_', entry_word.lower() ); | |
# Get list of all unique characters in entry word | |
entry_letters = []; | |
# Loop through all characters in entry word and add them to the list | |
# If they aren't already added and are a valid character | |
for char in entry_word.lower() : | |
if ( char not in entry_letters and is_valid_char( char ) ) : entry_letters.append( char ); | |
# Initialize list of already guessed letters | |
letters_guessed = []; | |
# Initialize list of incorrect guesses | |
incorrect_guesses = []; | |
# Initialize mistakes count | |
mistakes_count = 0; | |
# Method to print title | |
def print_title() : | |
print( '-'*5 + ' H A N G M A N ' + '-'*5 + '\n' ); | |
# Print hangman method | |
def print_hangman() : | |
# Initialize hangman body parts | |
hangman_bodyparts = [ | |
'O', # Head | |
'|', # Body | |
'/', # Left arm | |
'\\', # Right arm | |
'/', # Left leg | |
'\\', # Right leg | |
]; | |
# Function to draw the head | |
def make_head() : | |
return ( ' '*5 + hangman_bodyparts[0] + '\n' ); | |
# Function to draw the body with optional arms | |
def make_body( arm_count:int = 0 ) : | |
if ( arm_count == 1 ) : | |
return ( ' '*4 + hangman_bodyparts[2] + hangman_bodyparts[1] + '\n' ); | |
elif ( arm_count == 2 ) : | |
return ( ' '*4 + hangman_bodyparts[2] + hangman_bodyparts[1] + hangman_bodyparts[3] + '\n' ); | |
else : | |
return ( ' '*5 + hangman_bodyparts[1] + '\n' ); | |
# Function to draw the legs | |
def make_legs( leg_count:int = 0 ) : | |
if ( leg_count == 1 ) : | |
return ( ' '*4 + hangman_bodyparts[4] + '\n' ); | |
elif ( leg_count == 2 ) : | |
return ( ' '*4 + hangman_bodyparts[4] + ' ' + hangman_bodyparts[5] + '\n' ); | |
else : | |
return '\n'; | |
# Function to make hangman drawing | |
def make_hangman() : | |
# Initialize hangman image | |
hangman_image = ''; | |
# Construct hangman based on mistakes count | |
if ( mistakes_count == 0 ) : hangman_image += ( '\n' + '\n' + '\n' ); | |
elif ( mistakes_count == 1 ) : hangman_image += ( make_head() + '\n' + '\n' ); | |
elif ( mistakes_count == 2 ) : hangman_image += ( make_head() + make_body() + '\n' ); | |
elif ( mistakes_count == 3 ) : hangman_image += ( make_head() + make_body(1) + '\n' ); | |
elif ( mistakes_count == 4 ) : hangman_image += ( make_head() + make_body(2) + '\n' ); | |
elif ( mistakes_count == 5 ) : hangman_image += ( make_head() + make_body(2) + make_legs(1) ); | |
elif ( mistakes_count == 6 ) : hangman_image += ( make_head() + make_body(2) + make_legs(2) ); | |
return hangman_image; | |
# Print out hangman drawing | |
print( make_hangman() ); | |
# Display results | |
def print_results() : | |
# Initialize display text | |
display_text = ''; | |
# Loop through chars in entry word and check if they have been guessed | |
for char in entry_word : | |
# If char is a letter that has been guessed or not a valid char then add it to display text | |
if ( char.lower() in letters_guessed or not is_valid_char( char ) ) : | |
display_text += char; | |
# If char has not been guessed yet then add an underscore | |
else : | |
display_text += '_'; | |
display_text += '\n\n'; | |
# Show a list of incorrect guesses | |
display_text += f'Incorrect Guesses: [ {", ".join( incorrect_guesses )} ]'.title(); | |
print( display_text + '\n' ); | |
# Get user input | |
def get_user_input() : | |
global mistakes_count; | |
# Get user input | |
user_input = input( 'Guess a letter: ' ).lower(); | |
# Validate user input | |
while( user_input == '' or user_input.isspace() or user_input.isdigit() or len(user_input) > 1 ) : | |
user_input = input( 'Please enter a single letter: ' ).lower(); | |
# Check if user already guessed the letter or if its wrong | |
if ( user_input in letters_guessed or user_input not in entry_letters ) : | |
# Increment mistakes count | |
mistakes_count += 1; | |
# Add the incorrect guess to a list if it hasn't been added already and is not a space nor a valid letter | |
if ( user_input not in incorrect_guesses + entry_letters and user_input != ' ' ) : | |
incorrect_guesses.append( user_input ); | |
else : | |
# If the user guessed a new letter, add it to the list | |
letters_guessed.append( user_input ); | |
# Check if game is over | |
def is_game_over() : | |
return ( mistakes_count > 5 or set( entry_letters ) == set( letters_guessed ) ); | |
# Method to display hangman game interface | |
def start_game() : | |
while ( True ) : | |
# Clear console | |
clear_console(); | |
# Display title | |
print_title(); | |
# Display hangman | |
print_hangman(); | |
# Display results | |
print_results(); | |
# Get user input if game is not over yet | |
if ( not is_game_over() ) : | |
# Get user input | |
get_user_input(); | |
else : | |
# Break if the game is over | |
break; | |
# Start game | |
start_game(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment