Word Search Puzzle Generator in Python

 

 

 

Introduction

A word search puzzle is a fun and educational activity where words are hidden within a grid of letters.
The goal is to find all the hidden words, which can be arranged vertically, horizontally, or diagonally.
In this tutorial, we’ll create a Python program that generates a random word search puzzle based on a given list of words.

Objective

The objective of this tutorial is to teach you how to generate a word search puzzle in Python. By the end of this tutorial, you will be able to create a custom puzzle grid, populate it with words, and display it for the player to solve.

Python Code: Word Search Puzzle Generator

import random

def create_word_search(grid_size, words):
    # Create an empty grid
    grid = [['' for _ in range(grid_size)] for _ in range(grid_size)]
    
    # Function to check if a word fits in a specific direction
    def can_place_word(word, row, col, direction):
        if direction == 'horizontal':
            if col + len(word) > grid_size:
                return False
            for i in range(len(word)):
                if grid[row][col + i] not in ('', word[i]):
                    return False
        elif direction == 'vertical':
            if row + len(word) > grid_size:
                return False
            for i in range(len(word)):
                if grid[row + i][col] not in ('', word[i]):
                    return False
        elif direction == 'diagonal':
            if row + len(word) > grid_size or col + len(word) > grid_size:
                return False
            for i in range(len(word)):
                if grid[row + i][col + i] not in ('', word[i]):
                    return False
        return True
    
    # Function to place a word in the grid
    def place_word(word):
        directions = ['horizontal', 'vertical', 'diagonal']
        placed = False
        while not placed:
            row = random.randint(0, grid_size - 1)
            col = random.randint(0, grid_size - 1)
            direction = random.choice(directions)
            if can_place_word(word, row, col, direction):
                if direction == 'horizontal':
                    for i in range(len(word)):
                        grid[row][col + i] = word[i]
                elif direction == 'vertical':
                    for i in range(len(word)):
                        grid[row + i][col] = word[i]
                elif direction == 'diagonal':
                    for i in range(len(word)):
                        grid[row + i][col + i] = word[i]
                placed = True
    
    # Place each word in the grid
    for word in words:
        place_word(word)
    
    # Fill empty spaces with random letters
    for row in range(grid_size):
        for col in range(grid_size):
            if grid[row][col] == '':
                grid[row][col] = random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
    
    return grid

def print_grid(grid):
    for row in grid:
        print(' '.join(row))

# Example usage
grid_size = 10
words = ['PYTHON', 'PUZZLE', 'SEARCH', 'GRID', 'PROGRAM', 'CODE']
grid = create_word_search(grid_size, words)

print("Word Search Puzzle:")
print_grid(grid)

Explanation of the Program

This Python program creates a word search puzzle by following these steps:

  1. Create an empty grid: The program initializes a square grid of the given size (grid_size), filled with empty strings.
  2. Check if a word can be placed: The can_place_word function checks if a word can be placed in the grid in one of three possible directions: horizontal, vertical, or diagonal.
  3. Place the word in the grid: The place_word function randomly selects a starting position and direction and places the word in the grid if it fits.
  4. Fill remaining spaces: After placing all the words, the program fills any empty spaces in the grid with random letters.
  5. Display the grid: The print_grid function prints the final word search grid to the console.

How to Run the Program

To run this program:

    1. Ensure you have Python installed on your computer. If not, download and install Python from the official Python website.
    2. Copy the provided Python code into a file and save it with a .py extension (e.g., word_search.py).
    3. Open a terminal or command prompt, navigate to the directory where the Python file is saved, and run the following command:
python word_search.py
  1. The word search puzzle will be displayed in the console, and you can use the generated grid to solve the puzzle.

 

© 2025 Learn Programming. All rights reserved.

 

Leave a Reply

Your email address will not be published. Required fields are marked *