9 Concurrent maze solver

 

This chapter covers

  • Spinning up goroutines as needed
  • Communicating between different goroutines
  • Loading and writing a PNG image
  • Manipulating images and colors using a Go library
  • Writing a GIF image
  • Using linked lists

The oldest representation of a maze found by archaeologists, from Paleolithic times, was engraved in a piece of mammoth ivory. In Indo-European mythology, mazes are often associated with engineers, such as Daedalus in Greece. Mazes are also used as a symbol for the difficult path of a life toward a god figure in the O’odham tradition in North America, in India in the Chakra-vyuha style, or in Europe on the floors of medieval churches to represent the way to salvation.

Solving a maze has been an interesting engineering exercise for ages, including physically with an autonomous robotic mouse (see micromouse competitions, e.g., https://ukmars.org/contests/micromouse/) or virtually using graph theory. There are countless algorithms, each optimized for different constraints: Are there loops? Is the target inside the maze, like a treasure, or on another side, like a liberating way out? Are there curves or only right-angled corners? In the case of multiple possible paths, do we need the shortest, the fastest, or the one that goes through a collection of bonus stars?

9.1 Maze generation

9.1.1 What is an image?

9.1.2 Maze constraints

9.2 Maze solver

9.2.1 Setup

9.2.2 Loading the maze image

9.2.3 Add the solver

9.3 Let’s go exploring!

9.3.1 Finding the entrance

9.3.2 Communicating new possible paths

9.3.3 Recording the explored path

9.3.4 Waiting for unexplored paths and starting a goroutine

9.3.5 Stop listening, we found it: Short version

9.3.6 Testing one goroutine’s logic

9.4 Show the result

9.5 Notify when the treasure is found

Summary