Dive into the Fun World of Crossword with Python

Dive into the Fun World of Crossword with Python

Crossword puzzles are a timeless way to challenge the mind and have fun at the same time. Whether you're on a break, commuting, or just looking for something engaging, crosswords can be your go-to game. But what if you could create your own simple crossword game using Python? Sounds interesting, right? Let’s walk through a basic Python program that allows you to do just that!

The Concept

The game is simple: the player is given a few letters of a randomly selected word, and they have to guess the word. If they guess it correctly, they win; otherwise, they lose. The player is given 10 chances to guess the word.

The Code Breakdown

Let's start with the essentials. Below is the Python code to create this fun crossword game.

import random

# Taking player's name as input
name = input("Enter your name: ")

print(f"Welcome {name} to the Game")

# List of words for the game
words = ["apple", "guess", "television", "macbook", "dell", "india", "python", 
         "sunset", "sunrise", "mobile", "earphones", "opensource"]

# Corresponding hints for each word in the same order as in the words list
hints = [
    "A fruit red in color that keeps the doctor away!!!",
    "What are you making in this game!!!",
    "Medium through which we watch daily soap!!!",
    "A laptop company that has its own OS!!!",
    "A company that sells laptops!!!",
    "Country known for its diverse culture and heritage!!!",
    "A popular programming language that is easy to learn!!!",
    "The time in the evening when the sun disappears below the horizon!!!",
    "The time in the morning when the sun appears above the horizon!!!",
    "A handheld electronic device used for communication!!!",
    "Small speakers that you put in your ears to listen to music!!!",
    "Software that is available for free and can be modified by anyone!!!"
]

# Selecting a random word and its hint from the list
index = random.randint(0, len(words) - 1)
word = words[index]
hint = hints[index]

# Display the hint for the selected word
print(f"Hint: {hint}")

# Selecting random indexes of the word to reveal initially
indexes = random.sample(range(0, len(word)), min(3, len(word)))  # Ensure not more than the word length

# Preparing initial guesses with some letters revealed
guesses = ""

for i in indexes:
    guesses += word[i]

# Setting the initial number of chances
chance = 10

# Variable to control the game loop
play = "Yes"

# Function to restart the game
def playAgain():
    global play

    play = input("Do you want to Play Again (Yes / No): ")

    if play.lower() == "yes":
        global word, hint, chance, guesses   

        chance = 10

        name = input("Enter your name: ")

        print(f"Welcome {name} to the Game")

        # Selecting a new random word and its hint
        index = random.randint(0, len(words) - 1)
        word = words[index]
        hint = hints[index]

        print(f"Hint: {hint}")

        # Revealing some letters from the new word
        indexes = random.sample(range(0, len(word)), min(3, len(word)))
        guesses = ""
        for i in indexes:
            guesses += word[i]

    else:
        print("Thanks for Playing, Bye!!")  

# Main game loop
while play.lower() == "yes":
    while chance > 0:
        won = True
        for ch in word:
            if ch in guesses:
                print(ch, end=" ")
            else:
                print("_", end=" ")
                won = False

        if won:
            print(f"\nCongrats! You Won!!")
            print(f"The word is {word}")
            print(f"You guessed {word}")
            print(f"Your score is {chance * 10}")
            playAgain()
            break

        guess = input("\nEnter your guess: ")
        guesses += guess

        if guess not in word:
            chance -= 1
            print("Wrong Guess")
            print(f"You have {chance} chances left")

        if chance == 0:
            print("\nYou LOSE!!")
            print(f"The word was {word}")
            print(f"You guessed {guesses}")
            playAgain()
            break

How It Works

  1. Initialization: The game starts by asking the player to enter their name, and then it welcomes them.

  2. Word Selection: A word is randomly chosen from a predefined list of words. Based on the selected word, a relevant hint is provided to the player.

  3. Clues: The program randomly selects three letters from the word to give the player some clues.

  4. Gameplay:

    • The player is given 10 chances to guess the word.

    • The game displays the word with blanks (_) for the letters the player has not guessed yet.

    • The player can guess one letter at a time.

    • If the guessed letter is correct, it is revealed in the word. If not, the player loses a chance.

    • The game continues until the player either guesses the word correctly or runs out of chances.

  5. Winning or Losing:

    • If the player guesses the word within the allowed chances, they win, and their score is displayed.

    • If the player fails, the correct word is revealed.

  6. Play Again: After each game, the player is asked if they want to play again. If they choose "Yes", the game restarts with a new word.

Sample Output

    1. Welcome Message: The game starts by asking the player’s name and welcoming them.

        Enter your name: Rohan
        Welcome Rohan to the Game
      
      1. Hint Display: A random word is selected from the list along with its hint.

        Hint: A laptop company that has its own OS!!!
        _ _ c _ _ _ _
        
      2. Gameplay: The player is prompted to guess letters of the hidden word. If they guess a letter correctly, it reveals the letter in the appropriate positions. Otherwise, it reduces the number of chances left.

        Enter your guess: m
        m _ c _ _ _ _
        
        Enter your guess: a
        m a c _ _ _ _
        
        Enter your guess: b
        m a c b _ _ _
        
        Enter your guess: o
        m a c b o o _
        
        Enter your guess: k
        m a c b o o k
        
      3. Winning Message: If the player guesses the entire word, they receive a congratulatory message, along with their score based on the remaining chances.

        Congrats! You Won!!
        The word is macbook
        You guessed macbook
        Your score is 70
        
      4. Play Again Option: After the game ends, the player is given an option to play again.

        Do you want to Play Again (Yes / No): No
        Thanks for Playing, Bye!!
        

Losing Scenario

If the player fails to guess the word within the given chances:

  1. Chances Decrement: Each wrong guess reduces the chances.

     Enter your guess: z
     Wrong Guess
     You have 9 chances left
    
  2. Game Over: If all chances are used, the player is informed that they have lost, and the correct word is revealed.

     You LOSE!!
     The word was television
     You guessed tvzyz...
    

The script will then prompt whether they want to play again, starting the process anew with a different word and hint if they choose "Yes".

Conclusion

This simple game is not only a fun way to pass the time, but it's also a great project for beginners to practice Python. It helps reinforce concepts like loops, conditionals, and randomization. Plus, you can easily expand it by adding more words, better hints, or even a scoring system that tracks multiple games.

So, fire up your Python environment and give it a try. You might just surprise yourself with how much fun coding a crossword game can be! Happy coding! 🎉

Thanks for reading!!