Skip to content

Anagram Game

Abstract

Anagram Game is a Python project that challenges users to unscramble words. It demonstrates string manipulation, randomization, and GUI development. This project is ideal for learning about word games, event handling, and user interaction in Python.

Prerequisites

  • Python 3.6 or above
  • random (built-in)
  • tkinter (for GUI, usually pre-installed)

Before you Start

Ensure Python is installed. No external packages are required. For GUI, make sure Tkinter is available.

Getting Started

  1. Create a folder named anagram-gameanagram-game.
  2. Create a file named anagram_game.pyanagram_game.py.
  3. Copy the code below into your file.
⚙️ Anagram Game
Anagram Game
"""
Anagram Game
 
A Python application that challenges users to form words from a scrambled set of letters.
Features include:
- Generating random anagrams.
- Validating user input against a dictionary.
- Keeping track of the score.
"""
 
import random
from tkinter import Tk, Label, Entry, Button, messagebox
 
# Sample word list
WORDS = ["python", "anagram", "challenge", "programming", "developer", "algorithm"]
 
 
class AnagramGame:
    def __init__(self, root):
        self.root = root
        self.root.title("Anagram Game")
 
        self.score = 0
        self.current_word = ""
        self.scrambled_word = ""
 
        Label(root, text="Unscramble the word:").grid(row=0, column=0, padx=10, pady=10)
        self.word_label = Label(root, text="", font=("Helvetica", 16))
        self.word_label.grid(row=1, column=0, padx=10, pady=10)
 
        Label(root, text="Your Answer:").grid(row=2, column=0, padx=10, pady=10)
        self.answer_entry = Entry(root, width=30)
        self.answer_entry.grid(row=3, column=0, padx=10, pady=10)
 
        Button(root, text="Submit", command=self.check_answer).grid(row=4, column=0, pady=10)
        Button(root, text="Next", command=self.next_word).grid(row=5, column=0, pady=10)
 
        self.next_word()
 
    def next_word(self):
        """Generate a new scrambled word."""
        self.current_word = random.choice(WORDS)
        self.scrambled_word = "".join(random.sample(self.current_word, len(self.current_word)))
        self.word_label.config(text=self.scrambled_word)
        self.answer_entry.delete(0, "end")
 
    def check_answer(self):
        """Check the user's answer and update the score."""
        user_answer = self.answer_entry.get().strip().lower()
        if user_answer == self.current_word:
            self.score += 1
            messagebox.showinfo("Correct!", f"Well done! Your score: {self.score}")
        else:
            messagebox.showerror("Incorrect", f"The correct word was: {self.current_word}")
        self.next_word()
 
 
def main():
    root = Tk()
    app = AnagramGame(root)
    root.mainloop()
 
 
if __name__ == "__main__":
    main()
 
Anagram Game
"""
Anagram Game
 
A Python application that challenges users to form words from a scrambled set of letters.
Features include:
- Generating random anagrams.
- Validating user input against a dictionary.
- Keeping track of the score.
"""
 
import random
from tkinter import Tk, Label, Entry, Button, messagebox
 
# Sample word list
WORDS = ["python", "anagram", "challenge", "programming", "developer", "algorithm"]
 
 
class AnagramGame:
    def __init__(self, root):
        self.root = root
        self.root.title("Anagram Game")
 
        self.score = 0
        self.current_word = ""
        self.scrambled_word = ""
 
        Label(root, text="Unscramble the word:").grid(row=0, column=0, padx=10, pady=10)
        self.word_label = Label(root, text="", font=("Helvetica", 16))
        self.word_label.grid(row=1, column=0, padx=10, pady=10)
 
        Label(root, text="Your Answer:").grid(row=2, column=0, padx=10, pady=10)
        self.answer_entry = Entry(root, width=30)
        self.answer_entry.grid(row=3, column=0, padx=10, pady=10)
 
        Button(root, text="Submit", command=self.check_answer).grid(row=4, column=0, pady=10)
        Button(root, text="Next", command=self.next_word).grid(row=5, column=0, pady=10)
 
        self.next_word()
 
    def next_word(self):
        """Generate a new scrambled word."""
        self.current_word = random.choice(WORDS)
        self.scrambled_word = "".join(random.sample(self.current_word, len(self.current_word)))
        self.word_label.config(text=self.scrambled_word)
        self.answer_entry.delete(0, "end")
 
    def check_answer(self):
        """Check the user's answer and update the score."""
        user_answer = self.answer_entry.get().strip().lower()
        if user_answer == self.current_word:
            self.score += 1
            messagebox.showinfo("Correct!", f"Well done! Your score: {self.score}")
        else:
            messagebox.showerror("Incorrect", f"The correct word was: {self.current_word}")
        self.next_word()
 
 
def main():
    root = Tk()
    app = AnagramGame(root)
    root.mainloop()
 
 
if __name__ == "__main__":
    main()
 
  1. Run the script: python anagram_game.pypython anagram_game.py

Explanation

Code Breakdown

  1. Import modules
import random
import tkinter as tk
import random
import tkinter as tk
  1. Word selection and scrambling
words = ['python', 'algorithm', 'function', 'variable']
word = random.choice(words)
anagram = ''.join(random.sample(word, len(word)))
words = ['python', 'algorithm', 'function', 'variable']
word = random.choice(words)
anagram = ''.join(random.sample(word, len(word)))
  1. GUI for game
root = tk.Tk()
root.title('Anagram Game')
# ...setup widgets for display and input...
root.mainloop()
root = tk.Tk()
root.title('Anagram Game')
# ...setup widgets for display and input...
root.mainloop()

Features

  • Random word selection
  • Scrambled word display
  • User input for guesses
  • Score tracking

How It Works

  • Selects a word and scrambles it
  • Displays anagram in GUI
  • User enters guess
  • Checks correctness and updates score

GUI Components

  • Label: Shows scrambled word
  • Entry box: For user guess
  • Button: Submits guess
  • Score display: Tracks correct answers

Use Cases

  • Improve vocabulary
  • Practice spelling
  • Learn string manipulation

Next Steps

You can enhance this project by:

  • Adding more words
  • Implementing difficulty levels
  • Adding timer for each round
  • Keeping high scores
  • Giving hints

Enhanced Version Ideas

def add_timer():
    # Limit time for each guess
    pass
 
def show_hint():
    # Display a hint for the word
    pass
def add_timer():
    # Limit time for each guess
    pass
 
def show_hint():
    # Display a hint for the word
    pass

Troubleshooting Tips

  • GUI not showing: Ensure Tkinter is installed
  • No words displayed: Check word list

Conclusion

This project teaches string manipulation, randomization, and GUI basics. Extend it for more features and challenge levels.

Was this page helpful?

Let us know how we did