Initial maze solver

This commit is contained in:
semblanceofsense 2025-01-31 15:50:32 -07:00
commit 40408f54ec
8 changed files with 244 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
testing/
bin/

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module mazesolver
go 1.23.5
require golang.org/x/image v0.23.0 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=
golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=

View File

@ -0,0 +1,80 @@
package getMaze
import (
"errors"
"fmt"
"image/color"
"image/png"
"os"
)
/*
Pixel types:
-1 - explored
0 - unexplored
5 - wall
9 - end
*/
type Point struct {
X int;
Y int;
Value int
}
type Maze [][]Point
func GetMaze(imagepath string) (Maze, error) {
returnMaze := *new(Maze)
imagereader, err := os.Open(imagepath)
if err != nil {
return *new(Maze), err
}
image, err := png.Decode(imagereader)
if err != nil {
return *new(Maze), err
}
for y := image.Bounds().Min.Y; y < image.Bounds().Max.Y; y += 10 {
newRow := make([]Point, 0)
for x := image.Bounds().Min.X; x < image.Bounds().Max.X; x+= 10 {
typ := 0
switch image.At(x, y) {
case color.RGBA{ R: 255, G: 0, B: 0, A: 255 }:
typ = 0
case color.RGBA{ R: 0, G: 0, B: 255, A: 255 }:
typ = 9
case color.RGBA{ R: 255, G: 255, B: 255, A: 255 }:
typ = 0
case color.RGBA{ R: 0, G: 0, B: 0, A: 255 }:
typ = 5
default:
fmt.Println(image.At(x, y))
return *new(Maze), errors.New("bad color")
}
newPoint := Point{
X: x / 10,
Y: y / 10,
Value: typ,
}
newRow = append(newRow, newPoint)
}
returnMaze = append(returnMaze, newRow)
}
return returnMaze, err
}
func PrintMaze(maze Maze) {
for _, v := range maze {
fmt.Print("[")
for _, vv := range v {
fmt.Print(vv.Value)
fmt.Print(", ")
}
fmt.Println("]")
}
}

View File

@ -0,0 +1,50 @@
package outputmaze
import (
"errors"
"image"
"image/color"
"image/png"
getMaze "mazesolver/internal/getmaze"
"os"
)
func EditMaze(points []getMaze.Point, oldPath, newPath string) (string, error) {
imagereader, err := os.Open(oldPath)
if err != nil {
return "", err
}
image, err := png.Decode(imagereader)
if err != nil {
return "", err
}
for _, v := range points {
updateColor(image, v, color.RGBA{0, 255, 0, 255})
}
f, err := os.Create(newPath)
if err != nil {
return "", err
}
png.Encode(f, image)
return newPath, nil
}
type Changeable interface {
Set(x, y int, c color.Color)
}
func updateColor(image image.Image, p getMaze.Point, color color.Color) error {
for i := p.Y * 10; i < p.Y * 10 + 10; i++ {
for ii := p.X * 10; ii < p.X * 10 + 10; ii++ {
if cimg, ok := image.(Changeable); ok {
cimg.Set(ii, i, color)
} else {
return errors.New("Image not changeable")
}
}
}
return nil
}

View File

@ -0,0 +1,51 @@
package solvemaze
import (
"mazesolver/internal/getmaze"
)
func FindPath(maze getMaze.Maze) []getMaze.Point {
returnSlice := make([]getMaze.Point, 0)
debugMaze := GetDebugMaze(maze)
findPathRecursive(maze[1][1], maze, &returnSlice, debugMaze)
return returnSlice
}
func findPathRecursive(next getMaze.Point, maze getMaze.Maze, slice *[]getMaze.Point, debugMaze getMaze.Maze) bool {
maze[next.Y][next.X].Value = -1
debugMaze[next.Y][next.X].Value = 1
for _, v := range getAdjacent(next, maze) {
switch v.Value {
case -1:
continue
case 5:
continue
case 0:
if (findPathRecursive(v, maze, slice, debugMaze)) {
*slice = append(*slice, v)
return true
}
case 9:
*slice = append(*slice, v)
return true
}
}
return false
}
func getAdjacent(point getMaze.Point, maze getMaze.Maze) []getMaze.Point {
returnSlice := make([]getMaze.Point, 0)
returnSlice = append(returnSlice, maze[point.Y + 1][point.X])
returnSlice = append(returnSlice, maze[point.Y - 1][point.X])
returnSlice = append(returnSlice, maze[point.Y][point.X + 1])
returnSlice = append(returnSlice, maze[point.Y][point.X - 1])
return returnSlice
}
func GetDebugMaze(maze getMaze.Maze) getMaze.Maze {
a := make(getMaze.Maze, len(maze))
for i := range a {
a[i] = make([]getMaze.Point, len(maze[0]))
}
return a
}

29
mazesolver.go Normal file
View File

@ -0,0 +1,29 @@
package main
import (
"fmt"
"log"
getMaze "mazesolver/internal/getmaze"
"mazesolver/internal/outputmaze"
"mazesolver/internal/solvemaze"
)
func main() {
path := "/tmp/maze.png"
maze, err := getMaze.GetMaze(path)
if err != nil {
log.Fatal(err)
}
p := solvemaze.FindPath(maze)
if err != nil {
log.Fatal(err)
}
newpath := "/tmp/outputmaze.png"
_, err = outputmaze.EditMaze(p, path, newpath)
if err != nil {
log.Fatal(err)
}
fmt.Println(newpath)
}

25
util/queue/queue.go Normal file
View File

@ -0,0 +1,25 @@
package queue
type QueueInterface interface {
enqueue(int)
dequeue() int
}
type Queue[T any] []T
func (q Queue[T]) Enqueue(v T) {
q = append(q, v)
}
func (q Queue[T]) Dequeue() T {
x := q[0]
q = q[1:]
return x
}
func (q Queue[T]) IsEmpty() bool {
if len(q) == 0 {
return true
}
return false;
}