Introduction
Creating word search puzzles can be a fun way to practice coding and logical thinking. In this tutorial, we will build a simple Word Search Puzzle Generator using the Go programming language. The program will generate a random puzzle grid and place a list of given words within the grid. The remaining empty spaces in the grid will be filled with random letters.
Objective
The objective of this program is to demonstrate how to generate a word search puzzle, where words can be placed in various directions (horizontally, vertically, or diagonally). The program will also make sure that words don’t overlap or exceed the boundaries of the grid. This is a great exercise to understand string manipulation, loops, and random number generation in Go.
Word Search Puzzle Generator Code
package main
import (
"fmt"
"math/rand"
"time"
"strings"
)
func createPuzzle(gridSize int, words []string) [][]string {
// Initialize empty grid
grid := make([][]string, gridSize)
for i := range grid {
grid[i] = make([]string, gridSize)
for j := range grid[i] {
grid[i][j] = "."
}
}
// Place words in grid
for _, word := range words {
placed := false
for !placed {
direction := rand.Intn(8) // 8 possible directions
row := rand.Intn(gridSize)
col := rand.Intn(gridSize)
// Check if word can fit in the selected direction
if canPlaceWord(grid, row, col, direction, word) {
placeWord(grid, row, col, direction, word)
placed = true
}
}
}
// Fill in random letters in the empty spaces
for i := 0; i < gridSize; i++ {
for j := 0; j < gridSize; j++ { if grid[i][j] == "." { grid[i][j] = string('A' + rand.Intn(26)) } } } return grid } func canPlaceWord(grid [][]string, row, col, direction int, word string) bool { gridSize := len(grid) wordLen := len(word) // Check if word fits in the grid switch direction { case 0: // Horizontal right if col+wordLen > gridSize {
return false
}
for i := 0; i < wordLen; i++ {
if grid[row][col+i] != "." && grid[row][col+i] != string(word[i]) {
return false
}
}
case 1: // Horizontal left
if col-wordLen < -1 {
return false
}
for i := 0; i < wordLen; i++ { if grid[row][col-i] != "." && grid[row][col-i] != string(word[i]) { return false } } case 2: // Vertical down if row+wordLen > gridSize {
return false
}
for i := 0; i < wordLen; i++ {
if grid[row+i][col] != "." && grid[row+i][col] != string(word[i]) {
return false
}
}
case 3: // Vertical up
if row-wordLen < -1 {
return false
}
for i := 0; i < wordLen; i++ { if grid[row-i][col] != "." && grid[row-i][col] != string(word[i]) { return false } } case 4: // Diagonal down-right if row+wordLen > gridSize || col+wordLen > gridSize {
return false
}
for i := 0; i < wordLen; i++ { if grid[row+i][col+i] != "." && grid[row+i][col+i] != string(word[i]) { return false } } case 5: // Diagonal down-left if row+wordLen > gridSize || col-wordLen < -1 {
return false
}
for i := 0; i < wordLen; i++ {
if grid[row+i][col-i] != "." && grid[row+i][col-i] != string(word[i]) {
return false
}
}
case 6: // Diagonal up-right
if row-wordLen < -1 || col+wordLen > gridSize {
return false
}
for i := 0; i < wordLen; i++ {
if grid[row-i][col+i] != "." && grid[row-i][col+i] != string(word[i]) {
return false
}
}
case 7: // Diagonal up-left
if row-wordLen < -1 || col-wordLen < -1 {
return false
}
for i := 0; i < wordLen; i++ {
if grid[row-i][col-i] != "." && grid[row-i][col-i] != string(word[i]) {
return false
}
}
}
return true
}
func placeWord(grid [][]string, row, col, direction int, word string) {
switch direction {
case 0: // Horizontal right
for i := 0; i < len(word); i++ {
grid[row][col+i] = string(word[i])
}
case 1: // Horizontal left
for i := 0; i < len(word); i++ {
grid[row][col-i] = string(word[i])
}
case 2: // Vertical down
for i := 0; i < len(word); i++ {
grid[row+i][col] = string(word[i])
}
case 3: // Vertical up
for i := 0; i < len(word); i++ {
grid[row-i][col] = string(word[i])
}
case 4: // Diagonal down-right
for i := 0; i < len(word); i++ {
grid[row+i][col+i] = string(word[i])
}
case 5: // Diagonal down-left
for i := 0; i < len(word); i++ {
grid[row+i][col-i] = string(word[i])
}
case 6: // Diagonal up-right
for i := 0; i < len(word); i++ {
grid[row-i][col+i] = string(word[i])
}
case 7: // Diagonal up-left
for i := 0; i < len(word); i++ {
grid[row-i][col-i] = string(word[i])
}
}
}
func printPuzzle(grid [][]string) {
for _, row := range grid {
fmt.Println(strings.Join(row, " "))
}
}
func main() {
rand.Seed(time.Now().UnixNano())
words := []string{"GOLANG", "PUZZLE", "CODING", "FUN", "CHALLENGE", "ALGORITHM"}
gridSize := 10
puzzle := createPuzzle(gridSize, words)
printPuzzle(puzzle)
}
Explanation of the Program
This Go program generates a word search puzzle by following a few simple steps:
- Initialization: An empty grid is created with a given size (10×10 in this case), where each cell is initialized to a period (“.”).
- Word Placement: Words from the given list are placed randomly in the grid. The program tries to place each word in one of eight directions (horizontally, vertically, or diagonally).
- Grid Filling: Once all words are placed, the empty spots are filled with random letters (A-Z).
- Output: The program prints the final grid, showing the word search puzzle.
How to Run the Program
Follow these steps to run the Word Search Puzzle Generator:
- Install Go programming language if you haven’t already. You can download it from here.
- Save the code above to a file named
word_search.go
. - Open a terminal or command prompt and navigate to the folder where the file is located.
- Run the program by typing the following command:
go run word_search.go
. - The program will output the generated word search puzzle to the terminal.