|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"log"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/gdamore/tcell/v2/encoding"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
WALL = '#'
|
|
|
|
SPACE = '.'
|
|
|
|
PATH_LIMIT = 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
type Position struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
screen tcell.Screen
|
|
|
|
level [][]rune
|
|
|
|
player Position
|
|
|
|
status string
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) Text(msg string) {
|
|
|
|
var comb []rune
|
|
|
|
for x, cell := range msg {
|
|
|
|
game.screen.SetContent(x, game.height, cell, comb, tcell.StyleDefault)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) Render() {
|
|
|
|
var comb []rune
|
|
|
|
game.screen.Clear()
|
|
|
|
|
|
|
|
for y, line := range game.level {
|
|
|
|
for x, cell := range line {
|
|
|
|
game.screen.SetContent(x, y, cell, comb, tcell.StyleDefault)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
game.Text(game.status)
|
|
|
|
|
|
|
|
game.screen.SetContent(game.player.x, game.player.y, '@', comb, tcell.StyleDefault)
|
|
|
|
|
|
|
|
game.screen.Show()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) Exit() {
|
|
|
|
game.screen.Fini()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) Occupied(x int, y int) bool {
|
|
|
|
return game.level[y][x] != '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) {
|
|
|
|
if !game.Occupied(game.player.x + x_delta, game.player.y + y_delta) {
|
|
|
|
game.player.x += x_delta
|
|
|
|
game.player.y += y_delta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) HandleKeys(ev *tcell.EventKey) {
|
|
|
|
switch ev.Key() {
|
|
|
|
case tcell.KeyEscape:
|
|
|
|
game.Exit()
|
|
|
|
case tcell.KeyUp:
|
|
|
|
game.MovePlayer(0, -1)
|
|
|
|
case tcell.KeyDown:
|
|
|
|
game.MovePlayer(0, 1)
|
|
|
|
case tcell.KeyRight:
|
|
|
|
game.MovePlayer(1, 0)
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
game.MovePlayer(-1, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ev.Rune() {
|
|
|
|
case 'q':
|
|
|
|
game.Exit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) HandleEvents() {
|
|
|
|
switch ev := game.screen.PollEvent().(type) {
|
|
|
|
case *tcell.EventResize:
|
|
|
|
game.screen.Sync()
|
|
|
|
case *tcell.EventKey:
|
|
|
|
game.HandleKeys(ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeGame() (*Game) {
|
|
|
|
var game Game
|
|
|
|
var err error
|
|
|
|
|
|
|
|
encoding.Register()
|
|
|
|
|
|
|
|
game.screen, err = tcell.NewScreen()
|
|
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
|
|
|
|
err = game.screen.Init()
|
|
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
|
|
|
|
game.player.x = 1
|
|
|
|
game.player.y = 1
|
|
|
|
|
|
|
|
return &game
|
|
|
|
}
|
|
|
|
|
|
|
|
func compass(x int, y int, offset int) []Position {
|
|
|
|
return []Position{
|
|
|
|
Position{x, y - offset},
|
|
|
|
Position{x, y + offset},
|
|
|
|
Position{x + offset, y},
|
|
|
|
Position{x - offset, y},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) Inbounds(pos Position) bool {
|
|
|
|
return pos.x >= 0 && pos.x < game.width && pos.y >= 0 && pos.y < game.height
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) Neighbors(near Position) []Position {
|
|
|
|
result := make([]Position, 0)
|
|
|
|
points := compass(near.x, near.y, 2)
|
|
|
|
|
|
|
|
for _, pos := range points {
|
|
|
|
if game.Inbounds(pos) {
|
|
|
|
result = append(result, pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) NeighborWalls(pos Position) []Position {
|
|
|
|
neighbors := game.Neighbors(pos)
|
|
|
|
result := make([]Position, 0)
|
|
|
|
|
|
|
|
for _, pos := range neighbors {
|
|
|
|
if game.level[pos.y][pos.x] == WALL {
|
|
|
|
result = append(result, pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) FindCoord() (*Position, *Position) {
|
|
|
|
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})
|
|
|
|
|
|
|
|
for _, pos := range found {
|
|
|
|
if game.level[pos.y][pos.x] == SPACE {
|
|
|
|
return &Position{x, y}, &pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) HuntAndKill() []Position {
|
|
|
|
on := Position{1, 1}
|
|
|
|
dead_ends := make([]Position, 0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
neighbors := game.NeighborWalls(on)
|
|
|
|
|
|
|
|
if len(neighbors) == 0 {
|
|
|
|
dead_ends = append(dead_ends, on)
|
|
|
|
on, found := game.FindCoord()
|
|
|
|
if on == nil { 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
|
|
|
|
game.status = fmt.Sprintf("DEAD END %d,%d", row, col)
|
|
|
|
} else {
|
|
|
|
nb := neighbors[rand.Int() % len(neighbors)]
|
|
|
|
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
|
|
|
|
game.status = "HUNTING!"
|
|
|
|
}
|
|
|
|
|
|
|
|
game.Render()
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
game.status = "FINISHED"
|
|
|
|
|
|
|
|
return dead_ends
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) MakeMap(width int, height int) {
|
|
|
|
game.width = width
|
|
|
|
game.height = height
|
|
|
|
row := strings.Repeat("#", width)
|
|
|
|
|
|
|
|
for i := 0 ; i < height; i++ {
|
|
|
|
as_runes := []rune(strings.Clone(row))
|
|
|
|
game.level = append(game.level, as_runes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this returns dead_ends
|
|
|
|
game.HuntAndKill()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This program just prints "Hello, World!". Press ESC to exit.
|
|
|
|
func main() {
|
|
|
|
game := MakeGame()
|
|
|
|
game.MakeMap(27,17)
|
|
|
|
game.Render()
|
|
|
|
|
|
|
|
for {
|
|
|
|
game.HandleEvents()
|
|
|
|
game.Render()
|
|
|
|
}
|
|
|
|
}
|