Introduction
Word search puzzles are a popular and entertaining activity where words are hidden in a grid, and the goal is to find them. In this tutorial, we will guide you through creating a word search puzzle generator in C programming. This project helps in improving your skills in handling arrays, loops, and basic string manipulation in C.
Objective
The objective of this project is to generate a random word search puzzle, where words are placed in a grid, and the user can search for them. We will implement the logic to place the words horizontally, vertically, and diagonally, and fill the remaining spaces with random letters.
C Program Code: Word Search Puzzle Generator
#include
#include
#include
#include
#define GRID_SIZE 10
#define MAX_WORDS 5
// Function to check if a word fits in the grid at the specified location
int can_place_word(char grid[GRID_SIZE][GRID_SIZE], char word[], int x, int y, int dx, int dy) {
int len = strlen(word);
for (int i = 0; i < len; 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] != ' ') {
return 0;
}
}
return 1;
}
// Function to place a word in the grid at the specified location
void place_word(char grid[GRID_SIZE][GRID_SIZE], char word[], int x, int y, int dx, int dy) {
int len = strlen(word);
for (int i = 0; i < len; i++) {
grid[x + i * dx][y + i * dy] = word[i];
}
}
// Function to fill the grid with random letters
void fill_grid(char grid[GRID_SIZE][GRID_SIZE]) {
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;
}
}
}
}
// Function to print the grid
void print_grid(char grid[GRID_SIZE][GRID_SIZE]) {
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
printf("%c ", grid[i][j]);
}
printf("\n");
}
}
int main() {
char grid[GRID_SIZE][GRID_SIZE];
char words[MAX_WORDS][20] = {"CROSS", "SEARCH", "PUZZLE", "PROGRAM", "GENERATOR"};
int word_count = 5;
// Initialize grid with spaces
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
grid[i][j] = ' ';
}
}
srand(time(0));
for (int i = 0; i < word_count; i++) {
int placed = 0;
while (!placed) {
int x = rand() % GRID_SIZE;
int y = rand() % GRID_SIZE;
int direction = rand() % 8;
int dx = 0, dy = 0;
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 Down-Right
case 5: dx = -1; dy = -1; break; // Diagonal Up-Left
case 6: dx = 1; dy = -1; break; // Diagonal Down-Left
case 7: dx = -1; dy = 1; break; // Diagonal Up-Right
}
if (can_place_word(grid, words[i], x, y, dx, dy)) {
place_word(grid, words[i], x, y, dx, dy);
placed = 1;
}
}
}
fill_grid(grid);
printf("Word Search Puzzle:\n");
print_grid(grid);
return 0;
}
Explanation of the Program Structure
The program begins by defining a grid of size 10×10, which will be used to place the words. We then have a function that checks if a word can be placed in the grid at a specific location in a specified direction. If the word can be placed, it is then added to the grid, and we move on to the next word.
After placing all the words, the grid is filled with random letters in the empty spaces. Finally, the grid is printed to display the word search puzzle to the user.
How to Run the Program
Follow these steps to run the program:
- Install a C compiler like GCC if you don’t have one.
- Create a new C file (e.g.,
word_search.c) and paste the above code into the file. - Open your terminal or command prompt and navigate to the directory where your
word_search.cfile is located. - Compile the program using the command:
gcc word_search.c -o word_search - Run the program by typing:
./word_search

