Introduction

A word search puzzle is a game where words are hidden in a grid of letters. The objective of this puzzle is to find all the hidden words within the grid. In this tutorial, we will create a word search puzzle generator using C++. The program will generate a grid of letters and place the specified words into it either horizontally, vertically, or diagonally.

Objective

The goal of this program is to create a C++ application that generates a random word search puzzle. The puzzle will have words placed in random positions in the grid, and users can try to find the hidden words by scanning the grid.

Program Code

            #include 
            #include 
            #include 
            #include 
            #include 

            using namespace std;

            const int GRID_SIZE = 10; // Define the size of the grid
            const int MAX_WORDS = 5; // Maximum number of words to be placed in the grid

            // Function to print the grid
            void printGrid(vector<vector>& grid) {
                for (int i = 0; i < GRID_SIZE; i++) {
                    for (int j = 0; j < GRID_SIZE; j++) {
                        cout << grid[i][j] << " ";
                    }
                    cout << endl;
                }
            }

            // Function to check if a word can fit in the grid
            bool canPlaceWord(vector<vector>& grid, const string& word, int x, int y, int dx, int dy) {
                for (int i = 0; i < word.length(); i++) {
                    int nx = x + i * dx;
                    int ny = y + i * dy;
                    if (nx < 0 || nx >= GRID_SIZE || ny < 0 || ny >= GRID_SIZE || (grid[nx][ny] != ' ' && grid[nx][ny] != word[i])) {
                        return false;
                    }
                }
                return true;
            }

            // Function to place a word in the grid
            void placeWord(vector<vector>& grid, const string& word, int x, int y, int dx, int dy) {
                for (int i = 0; i < word.length(); i++) {
                    int nx = x + i * dx;
                    int ny = y + i * dy;
                    grid[nx][ny] = word[i];
                }
            }

            // Function to generate the word search puzzle
            void generatePuzzle(vector& words) {
                vector<vector> grid(GRID_SIZE, vector(GRID_SIZE, ' '));
                srand(time(0));

                // Try to place each word randomly in the grid
                for (const string& word : words) {
                    bool placed = false;
                    while (!placed) {
                        int x = rand() % GRID_SIZE;
                        int y = rand() % GRID_SIZE;
                        int direction = rand() % 8;
                        int dx = 0, dy = 0;

                        // Determine the direction of the word placement
                        switch (direction) {
                            case 0: dx = 1; dy = 0; break; // Horizontal right
                            case 1: dx = -1; dy = 0; break; // Horizontal left
                            case 2: dx = 0; dy = 1; break; // Vertical down
                            case 3: dx = 0; dy = -1; break; // Vertical up
                            case 4: dx = 1; dy = 1; break; // Diagonal right down
                            case 5: dx = -1; dy = -1; break; // Diagonal left up
                            case 6: dx = 1; dy = -1; break; // Diagonal right up
                            case 7: dx = -1; dy = 1; break; // Diagonal left down
                        }

                        // Check if the word can fit in the grid
                        if (canPlaceWord(grid, word, x, y, dx, dy)) {
                            placeWord(grid, word, x, y, dx, dy);
                            placed = true;
                        }
                    }
                }

                // Fill remaining empty spaces with random letters
                for (int i = 0; i < GRID_SIZE; i++) {
                    for (int j = 0; j < GRID_SIZE; j++) {
                        if (grid[i][j] == ' ') {
                            grid[i][j] = 'A' + rand() % 26;
                        }
                    }
                }

                // Print the final word search puzzle
                printGrid(grid);
            }

            int main() {
                // List of words to place in the word search
                vector words = {"HELLO", "WORLD", "PUZZLE", "CODE", "GAME"};

                cout << "Welcome to the Word Search Puzzle Generator!" << endl;
                generatePuzzle(words);

                return 0;
            }

Explanation of the Program Structure

The program starts by defining a 10×10 grid, where words will be placed randomly. The words are given in a list, and the program attempts to place each word in the grid in various possible directions (horizontally, vertically, or diagonally).

The main function initiates the puzzle generation. It first creates an empty grid, and then iteratively tries to place each word by checking if it fits at a random starting position in one of the eight directions. If a word fits, it’s placed in the grid, and once all words are placed, the remaining empty spaces are filled with random letters.

The key functions in the program are:

  • printGrid: Displays the grid of letters after all words have been placed.
  • canPlaceWord: Checks if a word can fit at a given position and direction.
  • placeWord: Places a word at a specific position in the grid.
  • generatePuzzle: Handles the overall logic of generating the word search puzzle, including word placement and filling empty spaces.

How to Run the Program

To run this program, follow these steps:

  1. Ensure that you have a C++ compiler installed on your system (such as GCC or MinGW).
  2. Create a new C++ file and paste the above code into it (for example, word_search.cpp).
  3. Open a terminal or command prompt and navigate to the folder where your C++ file is located.
  4. Compile the program using the following command:
    g++ word_search.cpp -o word_search
  5. Run the compiled program:
    ./word_search
  6. The word search puzzle will be displayed in the terminal.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)