Initial
This commit is contained in:
42
main.go
Normal file
42
main.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/png"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
const SIZE_X = 128
|
||||||
|
const SIZE_Y = 128
|
||||||
|
const CELL_SIZE = 16
|
||||||
|
const CELL_SCALE = 8
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, SIZE_X*CELL_SCALE, SIZE_Y*CELL_SCALE))
|
||||||
|
|
||||||
|
white := color.RGBA{R: 255, G: 255, B: 255, A: 255}
|
||||||
|
black := color.RGBA{R: 0, G: 0, B: 0, A: 255}
|
||||||
|
|
||||||
|
for y := 0; y < SIZE_Y*CELL_SCALE; y++ {
|
||||||
|
for x := 0; x < SIZE_X*CELL_SCALE; x++ {
|
||||||
|
stripeIndex := (x / CELL_SIZE) % 2
|
||||||
|
if stripeIndex == 0 {
|
||||||
|
img.SetRGBA(x, y, white)
|
||||||
|
} else {
|
||||||
|
img.SetRGBA(x, y, black)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create("stripes.png")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if err := png.Encode(f, img); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
124
mazer/maze.go
Normal file
124
mazer/maze.go
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
package mazer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateMaze(width int, height int) {
|
||||||
|
|
||||||
|
// Init array
|
||||||
|
matrix := make([][]int, height)
|
||||||
|
// 2. Allocate each inner slice (width)
|
||||||
|
for i := range matrix {
|
||||||
|
matrix[i] = make([]int, width)
|
||||||
|
}
|
||||||
|
|
||||||
|
startPoint := rand.IntN(width + 1)
|
||||||
|
matrix[0][startPoint] = 1
|
||||||
|
|
||||||
|
// Start moving until out of moves
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func pickRandomDirection(matrix [][]int, posX int, posY int) []int {
|
||||||
|
height := len(matrix)
|
||||||
|
if posY == height-1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
validDirs := getValidDirections(matrix, posX, posY)
|
||||||
|
if len(validDirs) == 0 {
|
||||||
|
return []int{0, 1}
|
||||||
|
}
|
||||||
|
|
||||||
|
return validDirs[rand.IntN(len(validDirs))]
|
||||||
|
}
|
||||||
|
|
||||||
|
func getValidDirections(matrix [][]int, posX int, posY int) [][]int {
|
||||||
|
height := len(matrix)
|
||||||
|
width := len(matrix[0])
|
||||||
|
validDirs := make([][]int, 0, 4)
|
||||||
|
|
||||||
|
possibleDirs := [][]int{
|
||||||
|
{0, 1}, // Down
|
||||||
|
{-1, 0}, // Left
|
||||||
|
{1, 0}, // Right
|
||||||
|
{0, -1}, // Up
|
||||||
|
}
|
||||||
|
prevX, prevY, hasPrev := getPreviousPathCell(matrix, posX, posY)
|
||||||
|
if hasPrev && prevX == posX && prevY == posY+1 {
|
||||||
|
// If we just moved up, force the next step to be side/down (no repeated up).
|
||||||
|
possibleDirs = [][]int{
|
||||||
|
{-1, 0}, // Left
|
||||||
|
{1, 0}, // Right
|
||||||
|
{0, 1}, // Down
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
neighborDirs := [][]int{
|
||||||
|
{0, -1},
|
||||||
|
{0, 1},
|
||||||
|
{-1, 0},
|
||||||
|
{1, 0},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dir := range possibleDirs {
|
||||||
|
newX := posX + dir[0]
|
||||||
|
newY := posY + dir[1]
|
||||||
|
|
||||||
|
if newX < 0 || newX >= width || newY < 0 || newY >= height {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if matrix[newY][newX] == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
tooClose := false
|
||||||
|
for _, neighborDir := range neighborDirs {
|
||||||
|
neighborX := newX + neighborDir[0]
|
||||||
|
neighborY := newY + neighborDir[1]
|
||||||
|
|
||||||
|
if neighborX < 0 || neighborX >= width || neighborY < 0 || neighborY >= height {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if neighborX == posX && neighborY == posY {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if matrix[neighborY][neighborX] == 1 {
|
||||||
|
tooClose = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tooClose {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
validDirs = append(validDirs, dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
return validDirs
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPreviousPathCell(matrix [][]int, posX int, posY int) (int, int, bool) {
|
||||||
|
height := len(matrix)
|
||||||
|
width := len(matrix[0])
|
||||||
|
dirs := [][]int{
|
||||||
|
{0, -1},
|
||||||
|
{0, 1},
|
||||||
|
{-1, 0},
|
||||||
|
{1, 0},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dir := range dirs {
|
||||||
|
x := posX + dir[0]
|
||||||
|
y := posY + dir[1]
|
||||||
|
if x < 0 || x >= width || y < 0 || y >= height {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if matrix[y][x] == 1 {
|
||||||
|
return x, y, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
BIN
stripes.png
Normal file
BIN
stripes.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
Reference in New Issue
Block a user