mazesolver/internal/getmaze/getmaze.go
semblanceofsense 1beb950e13 implement bot
2025-01-31 18:58:07 -07:00

103 lines
2.0 KiB
Go

package getMaze
import (
"errors"
"fmt"
"image/color"
"image/png"
"io"
"net/http"
"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)
res, err := http.Get(imagepath)
if err != nil {
return returnMaze, err
}
data, err := io.ReadAll(res.Body)
if err != nil {
return returnMaze, err
}
defer res.Body.Close()
err = os.WriteFile("/tmp/maze.png", data, 0755)
if err != nil {
return returnMaze, err
}
imagereader, err := os.Open("/tmp/maze.png")
if err != nil {
return returnMaze, err
}
image, err := png.Decode(imagereader)
if err != nil {
fmt.Println("Here")
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("]")
}
}