Java

 

 

Learn how to create a fun Word Search Puzzle Generator using Java programming language!

Introduction

A Word Search puzzle is a popular game where words are hidden in a grid, and the player needs to find them. In this tutorial, we will write a simple Word Search Puzzle Generator in Java that generates a grid filled with random letters and places words of your choice in it. The goal is to provide you with an easy-to-understand Java program that you can use to create word search puzzles.

Objective

The objective of this program is to generate a word search puzzle that takes a list of words and places them in a grid, while filling the remaining spaces with random letters. The program will also display the puzzle and the list of words that need to be found.

Code: Word Search Puzzle Generator

import java.util.Random;
import java.util.List;
import java.util.ArrayList;

public class WordSearchGenerator {
    
    private static final char EMPTY_CHAR = '-';
    private static final int GRID_SIZE = 10;
    private char[][] grid;
    
    public WordSearchGenerator(List words) {
        grid = new char[GRID_SIZE][GRID_SIZE];
        fillGridWithEmpty();
        placeWords(words);
        fillEmptySpaces();
    }
    
    // Fills the grid with empty characters
    private void fillGridWithEmpty() {
        for (int i = 0; i < GRID_SIZE; i++) {
            for (int j = 0; j < GRID_SIZE; j++) {
                grid[i][j] = EMPTY_CHAR;
            }
        }
    }
    
    // Places words in the grid in random positions
    private void placeWords(List words) {
        Random random = new Random();
        
        for (String word : words) {
            boolean wordPlaced = false;
            while (!wordPlaced) {
                int row = random.nextInt(GRID_SIZE);
                int col = random.nextInt(GRID_SIZE);
                int direction = random.nextInt(2); // 0 - Horizontal, 1 - Vertical
                
                if (canPlaceWord(row, col, word, direction)) {
                    for (int i = 0; i < word.length(); i++) { if (direction == 0) { grid[row][col + i] = word.charAt(i); } else { grid[row + i][col] = word.charAt(i); } } wordPlaced = true; } } } } // Checks if a word can be placed in a given direction private boolean canPlaceWord(int row, int col, String word, int direction) { if (direction == 0) { // Horizontal if (col + word.length() > GRID_SIZE) return false;
            for (int i = 0; i < word.length(); i++) { if (grid[row][col + i] != EMPTY_CHAR) return false; } } else { // Vertical if (row + word.length() > GRID_SIZE) return false;
            for (int i = 0; i < word.length(); i++) {
                if (grid[row + i][col] != EMPTY_CHAR) return false;
            }
        }
        return true;
    }
    
    // Fill remaining empty spaces with random letters
    private void fillEmptySpaces() {
        Random random = new Random();
        for (int i = 0; i < GRID_SIZE; i++) {
            for (int j = 0; j < GRID_SIZE; j++) {
                if (grid[i][j] == EMPTY_CHAR) {
                    grid[i][j] = (char) ('A' + random.nextInt(26));
                }
            }
        }
    }
    
    // Displays the puzzle grid
    public void displayGrid() {
        for (int i = 0; i < GRID_SIZE; i++) {
            for (int j = 0; j < GRID_SIZE; j++) {
                System.out.print(grid[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        List words = new ArrayList<>();
        words.add("JAVA");
        words.add("PUZZLE");
        words.add("SEARCH");
        words.add("PROGRAM");
        
        WordSearchGenerator generator = new WordSearchGenerator(words);
        generator.displayGrid();
    }
}

Explanation of the Program

This Java program works in the following way:

  • Grid Initialization: The grid is created as a 2D array of characters (10×10 by default), initialized with the character `’-‘` to represent empty spaces.
  • Word Placement: A list of words is taken as input, and each word is placed randomly in the grid. The program tries to place each word either horizontally or vertically. If the word can’t fit, it tries another random position.
  • Random Letters: After placing all the words, any remaining empty spots are filled with random letters from the alphabet to complete the puzzle.
  • Display Grid: The grid is displayed in a readable format on the console.

How to Run the Program

Follow these steps to run the Word Search Puzzle Generator in Java:

  1. Copy the code into a file named WordSearchGenerator.java.
  2. Compile the program using the Java compiler:
    javac WordSearchGenerator.java
  3. Run the program using the Java interpreter:
    java WordSearchGenerator
  4. The puzzle will be displayed in the console, and you can try to find the words listed in the program.
© 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 :)