100 Days of Code: Day 7 - Cybersecurity Hangman Game

Challenge: 100 Days of Code with Python

Day: 7

Introduction

Welcome to Day 7 of my 100 Days of Code challenge with Python! Today, I created a fun and educational Hangman game focused on cybersecurity terms. This project was a great way to apply various Python concepts and improve my problem-solving skills. Let's dive into what I learned and how the game works.

What I Learned

Day 7 Goals: What We Will Make by the End of the Day

Setting clear goals for the day helps in understanding what the end project will look like and provides a roadmap for the tasks ahead.

How to Break a Complex Problem Down into a Flow Chart

Learning to break down complex problems into smaller, manageable steps using flow charts is crucial. It helps in visualizing the entire process and tackling each part systematically.

Picking a Random Word and Checking Answers

Creating functionality to randomly select a word from a list and checking if the user's guessed letter is in the word involved understanding how to work with lists and random selections in Python.

Replacing Blanks with Guesses

Developing the logic to replace the blanks in the displayed word with the user's correct guesses involved working with list indices and updating values.

Checking if the Player has Won

Creating the logic to check if the player has correctly guessed the entire word involved verifying if there are any blanks left in the display.

Keeping Track of the Player's Lives

Adding functionality to keep track of the player's remaining lives involved decrementing a counter each time the player made an incorrect guess.

Improving the User Experience

Enhancing the game by improving the user interface and experience involved adding visual elements and making the game more interactive.

Adding ASCII Art and Improving the UI

Adding ASCII art to the game made it more engaging and visually appealing. It involved creating different stages of the hangman and displaying them based on the player's progress.

The Benefits of Daily Practice

Understanding the importance of consistent daily practice in learning and mastering new skills. Daily practice helps in reinforcing concepts, building muscle memory, and achieving continuous improvement.

Project: Cybersecurity Hangman Game

For Day 7, I created a Hangman game with a focus on cybersecurity terms. This project was not only fun but also educational, reinforcing important Python concepts and introducing new cybersecurity vocabulary.

How the Game Works

The game randomly selects a word from a list of cybersecurity terms. The player must guess the letters in the word before running out of lives. The game features engaging ASCII art and a user-friendly interface to enhance the playing experience.

Key Features

  • Random Word Selection: The game selects a random word related to cybersecurity.

  • ASCII Art: Fun and engaging ASCII art for different stages of the Hangman game.

  • User Interaction: Players guess letters and receive feedback on their progress.

  • Cybersecurity Terms: The word list includes various cybersecurity-related terms to enhance learning.

Code and Explanation

Here's a brief look at the code and what each part does:

hangman_art.py

# hangman_art.py

logo = """

_

| |

| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __

| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \

| | | | (_| | | | | (_| | | | | | | (_| | | | |

|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|

__/ |

|___/

"""

stages = ['''

+---+

| |

O |

/|\ |

/ \ |

|

=========

''', '''

+---+

| |

O |

/|\ |

/ |

|

=========

''', '''

+---+

| |

O |

/|\ |

|

|

=========

''', '''

+---+

| |

O |

/| |

|

|

=========

''', '''

+---+

| |

O |

| |

|

|

=========

''', '''

+---+

| |

O |

|

|

|

=========

''', '''

+---+

| |

|

|

|

|

=========

''']

hangman_words.py

# hangman_words.py

word_list = [

'firewall', 'encryption', 'malware', 'phishing', 'ransomware',

'antivirus', 'breach', 'cyberattack', 'hacker', 'keylogger',

'password', 'spoofing', 'spyware', 'trojan', 'worm',

'backdoor', 'botnet', 'bruteforce', 'cipher', 'data breach',

'ddos', 'exploit', 'hashing', 'honeypot', 'intrusion',

'keystroke', 'malicious', 'payload', 'penetration', 'protocol',

'rootkit', 'session', 'smishing', 'social engineering', 'threat',

'token', 'two factor', 'vulnerability', 'whitelist', 'zero day'

]

main.py

# main.py

import random

from hangman_words import word_list

from hangman_art import logo, stages

# Display the hangman logo at the start of the game

print(logo)

# Randomly choose a word from the word list

chosen_word = random.choice(word_list)

word_length = len(chosen_word)

# Create a list to keep track of guessed letters

display = ["_"] * word_length

# Initialize game state variables

end_of_game = False

lives = 6

# Game loop

while not end_of_game:

# Ask the user to guess a letter

guess = input("Guess a letter: ").lower()

# Check if the user has already guessed this letter

if guess in display:

print(f"You've already guessed {guess}")

else:

# Check guessed letter

for position in range(word_length):

letter = chosen_word[position]

if letter == guess:

display[position] = letter

# If the guessed letter is not in the chosen word

if guess not in chosen_word:

print(f"You guessed {guess}, that's not in the word. You lose a life.")

lives -= 1

if lives == 0:

end_of_game = True

print("You lose.")

print(f"The word was: {chosen_word}")

# Join all the elements in the list and turn it into a string

print(" ".join(display))

# Check if the user has got all the letters

if "_" not in display:

end_of_game = True

print("You win.")

# Display the current stage of the hangman

print(stages[lives])

Conclusion

Creating the Cybersecurity Hangman game was a rewarding experience that reinforced my understanding of Python and introduced me to new cybersecurity vocabulary. The process of breaking down complex problems into smaller steps and consistently practicing coding has been incredibly beneficial. Stay tuned for more exciting projects in the upcoming days!

Stay Connected

Follow my progress and join me on this coding journey:

Happy coding!

Previous
Previous

100 Days of Code: Recap of Days 1-7

Next
Next

100 Days of Code: Day 6 - Solving Reeborg's World Game Puzzles