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