DEV Community

Palak Hirave
Palak Hirave

Posted on • Edited on

Day 7 of 100

Today I started off with writing algorithms. I thought of some simple game ideas and broke those down into algorithms. I wrote them on paper before finding draw.io and started to use that instead. Afterwards, I settled on making a hangman game and wrote the algorithm for it before I started building it. First off, I made a list with as many words as I could think of(this took more time than writing the actual program) and then started building the basics. I used for loops to replace the letters in the word with underscores and made a guess variable for the user to guess a letter. If it was in the word, the underscore would be replaced with the letter and if it wasn't the user would either lose a life or if the letter has already been guessed they wouldn't but would be told that they have already guessed it. All this was placed into a while loop that would run until the user guess the word or lost all their lives. Overall, it was quite a fun little project.

Hangman Game

import random

from hangman_words import word_list
from hangman_art import stages, logo

lives = 6

print(logo)

chosen_word = random.choice(word_list)
print(chosen_word)

placeholder = ""
word_length = len(chosen_word)
for position in range(word_length):
    placeholder += "_"
print("Word to guess: " + placeholder)

game_over = False
correct_letters = []

while not game_over:

    print(f"****************************{lives}/6 LIVES LEFT****************************")
    guess = input("Guess a letter: ").lower()

    if guess in correct_letters:
        print(f"You've already guessed {guess}")

    display = ""

    for letter in chosen_word:
        if letter == guess:
            display += letter
            correct_letters.append(guess)
        elif letter in correct_letters:
            display += letter
        else:
            display += "_"

    print("Word to guess: " + display)

    if guess not in chosen_word:
        lives -= 1
        print(f"You guessed {guess}, that's not in the word. You lose a life.")

        if lives == 0:
            game_over = True

            print(f"***********************IT WAS {chosen_word}! YOU LOSE**********************")

    if "_" not in display:
        game_over = True
        print("****************************YOU WIN****************************")

    print(stages[lives])
Enter fullscreen mode Exit fullscreen mode

Hangman_words

word_list = [
    'abruptly','absurd','abyss','affix','askew','avenue','awkward','axiom','azure',
    'bagpipes','bandwagon','banjo','bayou','beekeeper','bikini','blitz','blizzard','boggle','bookworm',
    'boxcar','boxful','buckaroo','buffalo','buffoon','buxom','buzzard','buzzing','buzzwords',
    'caliph','cobweb','cockiness','croquet','crypt','curacao','cycle',
    'daiquiri','dirndl','disavow','dizzying','duplex','dwarves',
    'embezzle','equip','espionage','euouae','exodus',
    'faking','fishhook','fixable','fjord','flapjack','flopping','fluffiness','flyby','foxglove','frazzled',
    'frizzled','fuchsia','funny',
    'gabby','galaxy','galvanize','gazebo','giaour','gizmo','glowworm','glyph','gnarly','gnostic','gossip','grogginess',
    'haiku','haphazard','hyphen',
    'iatrogenic','icebox','injury','ivory','ivy',
    'jackpot','jaundice','jawbreaker','jaywalk','jazziest','jazzy','jelly','jigsaw','jinx','jiujitsu','jockey',
    'jogging','joking','jovial','joyful','juicy','jukebox','jumbo',
    'kayak','kazoo','keyhole','khaki','kilobyte','kiosk','kitsch','kiwifruit','klutz','knapsack',
    'larynx','lengths','lucky','luxury','lymph',
    'marquis','matrix','megahertz','microwave','mnemonic','mystify',
    'naphtha','nightclub','nowadays','numbskull','nymph',
    'onyx','ovary','oxidize','oxygen',
    'pajama','peekaboo','phlegm','pixel','pizazz','pneumonia','polka','pshaw','psyche','puppy','puzzling',
    'quartz','queue','quips','quixotic','quiz', 'quizzes','quorum',
    'razzmatazz','rhubarb','rhythm','rickshaw',
    'schnapps','scratch','shiv','snazzy','sphinx','spritz','squawk','staff','strength','strengths',
    'stretch','stronghold','stymied','subway','swivel','syndrome',
    'thriftless','thumbscrew','topaz','transcript','transgress','transplant','triphthong','twelfth','twelfths',
    'unknown','unworthy','unzip','uptown',
    'vaporize','vixen','vodka','voodoo','vortex','voyeurism',
    'walkway','waltz','wave','wavy','waxy','wellspring','wheezy','whiskey','whizzing','whomever','wimpy',
    'witchcraft','wizard','woozy','wristwatch','wyvern',
    'xylophone',
    'yachtsman','yippee','yoked','youthful','yummy',
    'zephyr','zigzag', 'zigzagging','zilch','zipper','zodiac','zombie',
]
Enter fullscreen mode Exit fullscreen mode

Hangman_art

stages = [r'''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', r'''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', r'''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

logo = r''' 
 _                                             
| |                                            
| |__   __ _ _ __   __ _ _ __ ___   __ _ _ __  
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ 
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
                    __/ |                      
                   |___/    '''

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
aegisspecter profile image
Aegis-Specter

Can you share the code for that hangman game pls

Collapse
 
palakhirave profile image
Palak Hirave

Edited the post