Golang

 

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:

  1. Initialization: An empty grid is created with a given size (10×10 in this case), where each cell is initialized to a period (“.”).
  2. 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).
  3. Grid Filling: Once all words are placed, the empty spots are filled with random letters (A-Z).
  4. 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:

  1. Install Go programming language if you haven’t already. You can download it from here.
  2. Save the code above to a file named word_search.go.
  3. Open a terminal or command prompt and navigate to the folder where the file is located.
  4. Run the program by typing the following command: go run word_search.go.
  5. The program will output the generated word search puzzle to 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 :)