66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"os"
|
|
"test-maze/mazer"
|
|
)
|
|
|
|
const SIZE_X = 21
|
|
const SIZE_Y = 21
|
|
const SCALE_FACTOR = 5
|
|
const COLOR_SOLUTION_PATH = true
|
|
|
|
func main() {
|
|
matrix := mazer.GenerateMaze(SIZE_X, SIZE_Y)
|
|
|
|
var solutionPath [][]bool
|
|
if COLOR_SOLUTION_PATH {
|
|
solutionPath = mazer.FindSolutionPath(matrix)
|
|
}
|
|
|
|
img := image.NewRGBA(image.Rect(0, 0, SIZE_X*SCALE_FACTOR, SIZE_Y*SCALE_FACTOR))
|
|
|
|
white := color.RGBA{R: 255, G: 255, B: 255, A: 255}
|
|
black := color.RGBA{R: 0, G: 0, B: 0, A: 255}
|
|
green := color.RGBA{R: 0, G: 180, B: 0, A: 255}
|
|
|
|
for cellY := 0; cellY < SIZE_Y; cellY++ {
|
|
startY := cellY * SCALE_FACTOR
|
|
for cellX := 0; cellX < SIZE_X; cellX++ {
|
|
startX := cellX * SCALE_FACTOR
|
|
|
|
p := black
|
|
if matrix[cellY][cellX] == 1 {
|
|
p = white
|
|
if COLOR_SOLUTION_PATH && solutionPath != nil && solutionPath[cellY][cellX] {
|
|
p = green
|
|
}
|
|
}
|
|
|
|
for dy := 0; dy < SCALE_FACTOR; dy++ {
|
|
row := img.Pix[(startY+dy)*img.Stride:]
|
|
for dx := 0; dx < SCALE_FACTOR; dx++ {
|
|
offset := (startX + dx) * 4
|
|
row[offset+0] = p.R
|
|
row[offset+1] = p.G
|
|
row[offset+2] = p.B
|
|
row[offset+3] = p.A
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
f, err := os.Create("maze.png")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer f.Close()
|
|
|
|
if err := png.Encode(f, img); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|