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: build:
go build . go build .
run: build
./gorogue

@ -50,9 +50,7 @@ func (game *Game) Render() {
} }
game.Text(game.status) game.Text(game.status)
game.screen.SetContent(game.player.x, game.player.y, '@', comb, tcell.StyleDefault) game.screen.SetContent(game.player.x, game.player.y, '@', comb, tcell.StyleDefault)
game.screen.Show() game.screen.Show()
} }
@ -112,9 +110,7 @@ func MakeGame() (*Game) {
err = game.screen.Init() err = game.screen.Init()
if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) }
game.player = Position{1,1}
game.player.x = 1
game.player.y = 1
return &game 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, y + offset}, Position{x, y + offset},
Position{x + offset, y}, 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 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 y := 1; y < game.height ; y += 2 {
for x := 1; x < game.width ; x += 2 { for x := 1; x < game.width ; x += 2 {
if game.level[y][x] != WALL { if game.level[y][x] != WALL {
continue 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 { 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 { func (game *Game) HuntAndKill() []Position {
on := Position{1, 1} on := Position{1, 1}
found := Position{1,1}
dead_ends := make([]Position, 0) dead_ends := make([]Position, 0)
for { for {
@ -190,23 +190,26 @@ func (game *Game) HuntAndKill() []Position {
if len(neighbors) == 0 { if len(neighbors) == 0 {
dead_ends = append(dead_ends, on) dead_ends = append(dead_ends, on)
next_guess := game.FindCoord() if !game.FindCoord(&on, &found) {
if next_guess == nil { break } break
}
on = next_guess[0]
found := next_guess[1]
game.level[on.y][on.x] = SPACE game.level[on.y][on.x] = SPACE
row := (on.y + found.y) / 2 row := (on.y + found.y) / 2
col := (on.x + found.x) / 2 col := (on.x + found.x) / 2
game.level[row][col] = SPACE game.level[row][col] = SPACE
} else { } else {
rand_neighbor := rand.Int() % len(neighbors) rand_neighbor := rand.Int() % len(neighbors)
nb := neighbors[rand_neighbor] nb := neighbors[rand_neighbor]
game.level[nb.y][nb.x] = SPACE game.level[nb.y][nb.x] = SPACE
row := (nb.y + on.y) / 2 row := (nb.y + on.y) / 2
col := (nb.x + on.x) / 2 col := (nb.x + on.x) / 2
game.level[row][col] = SPACE game.level[row][col] = SPACE
on = nb on = nb
} }
} }

Loading…
Cancel
Save