A Go version of the https://lcthw.dev/learn-code-the-hard-way/curseyou-python-rogue that makes a tiny Rogue in Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.3 KiB

package main
import (
"math/rand"
"time"
)
func (game *Game) HuntNext(on *Position, found *Position) bool {
for y := 1; y < game.Height ; y += 2 {
for x := 1; x < game.Width ; x += 2 {
if game.Level[y][x] != WALL {
continue
}
neighbors := game.Neighbors(Position{x, y})
for _, pos := range neighbors {
if game.Level[pos.Y][pos.X] == SPACE {
*on = Position{x, y}
*found = pos
return true
}
}
}
}
return false
}
func (game *Game) HAKStep(from Position, to Position) {
game.Level[from.Y][from.X] = SPACE
row := (from.Y + to.Y) / 2
col := (from.X + to.X) / 2
game.Level[row][col] = SPACE
}
func (game *Game) NewMaze() []Position {
on := Position{1, 1}
found := Position{1,1}
dead_ends := make([]Position, 0)
for {
neighbors := game.NeighborWalls(on)
if len(neighbors) == 0 {
dead_ends = append(dead_ends, on)
if !game.HuntNext(&on, &found) {
break
}
game.HAKStep(on, found)
} else {
rand_neighbor := rand.Int() % len(neighbors)
nb := neighbors[rand_neighbor]
game.HAKStep(nb, on)
on = nb
}
if SHOW_RENDER {
game.Render()
time.Sleep(50 * time.Millisecond)
}
}
return dead_ends
}