Cleaned up the code some after debugging for a while.

master
Zed A. Shaw 6 days ago
parent 306fe54a87
commit 65d390af57
  1. 3
      Makefile
  2. 35
      main.go

@ -1,3 +1,6 @@
build:
go build .
run: build
./gorogue

@ -50,9 +50,7 @@ func (game *Game) Render() {
}
game.Text(game.status)
game.screen.SetContent(game.player.x, game.player.y, '@', comb, tcell.StyleDefault)
game.screen.Show()
}
@ -112,9 +110,7 @@ func MakeGame() (*Game) {
err = game.screen.Init()
if err != nil { log.Fatal(err) }
game.player.x = 1
game.player.y = 1
game.player = Position{1,1}
return &game
}
@ -124,7 +120,7 @@ func compass(x int, y int, offset int) []Position {
Position{x, y - offset},
Position{x, y + offset},
Position{x + offset, y},
Position{x - offset, y},
Position{ x - offset, y},
}
}
@ -160,28 +156,32 @@ func (game *Game) NeighborWalls(pos Position) []Position {
return result
}
func (game *Game) FindCoord() []Position {
func (game *Game) FindCoord(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
}
found := game.Neighbors(Position{x, y})
neighbors := game.Neighbors(Position{x, y})
for _, pos := range found {
for _, pos := range neighbors {
if game.level[pos.y][pos.x] == SPACE {
return []Position{Position{x, y}, pos}
*on = Position{x, y}
*found = pos
return true
}
}
}
}
return nil
return false
}
func (game *Game) HuntAndKill() []Position {
on := Position{1, 1}
found := Position{1,1}
dead_ends := make([]Position, 0)
for {
@ -190,23 +190,26 @@ func (game *Game) HuntAndKill() []Position {
if len(neighbors) == 0 {
dead_ends = append(dead_ends, on)
next_guess := game.FindCoord()
if next_guess == nil { break }
on = next_guess[0]
found := next_guess[1]
if !game.FindCoord(&on, &found) {
break
}
game.level[on.y][on.x] = SPACE
row := (on.y + found.y) / 2
col := (on.x + found.x) / 2
game.level[row][col] = SPACE
} else {
rand_neighbor := rand.Int() % len(neighbors)
nb := neighbors[rand_neighbor]
game.level[nb.y][nb.x] = SPACE
row := (nb.y + on.y) / 2
col := (nb.x + on.x) / 2
game.level[row][col] = SPACE
on = nb
}
}

Loading…
Cancel
Save