Skip to content

Anagram Game

Abstract

Create an Anagram Game that challenges users to form words from a scrambled set of letters. The app generates random anagrams, validates user input, and keeps track of the score. This project demonstrates string manipulation, randomization, and GUI development in Python.

Prerequisites

  • Python 3.6 or above
  • Text Editor or IDE
  • Basic understanding of Python syntax
  • Familiarity with Tkinter for GUI development
  • Knowledge of string operations

Getting Started

Creating a new project

  1. Create a new project folder and name it anagram_gameanagram_game.
  2. Create a new file inside the folder and name it anagram_game.pyanagram_game.py.
  3. Open the project folder in your favorite text editor or IDE.
  4. Copy the code below and paste it into the anagram_game.pyanagram_game.py file.

Write the code

⚙️ anagram_game.py
anagram_game.py
"""
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.py
"""
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()
 

Key Features

  • Generate random anagrams from a word list
  • Validate user input against the original word
  • Keep track of the score
  • GUI interface for user interaction

Explanation

Generating Anagrams

The app randomly selects a word and scrambles its letters:

anagram_game.py
self.current_word = random.choice(WORDS)
self.scrambled_word = "".join(random.sample(self.current_word, len(self.current_word)))
anagram_game.py
self.current_word = random.choice(WORDS)
self.scrambled_word = "".join(random.sample(self.current_word, len(self.current_word)))

Checking Answers

User input is validated against the original word, and the score is updated:

anagram_game.py
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}")
anagram_game.py
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}")

Running the Application

  1. Save the file.
  2. Run the application:
python anagram_game.py
python anagram_game.py

Conclusion

This Anagram Game project is a fun way to practice string manipulation and GUI development in Python. You can extend it by adding a larger word list, hints, or a timer feature.

Was this page helpful?

Let us know how we did