|
|
@ -4,16 +4,39 @@ package main |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
"math/rand" |
|
|
|
|
|
|
|
"strings" |
|
|
|
"github.com/gdamore/tcell/v2" |
|
|
|
"github.com/gdamore/tcell/v2" |
|
|
|
"github.com/gdamore/tcell/v2/encoding" |
|
|
|
"github.com/gdamore/tcell/v2/encoding" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
|
|
|
WALL = '#' |
|
|
|
|
|
|
|
SPACE = '.' |
|
|
|
|
|
|
|
PATH_LIMIT = 1000 |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Position struct { |
|
|
|
|
|
|
|
x int |
|
|
|
|
|
|
|
y int |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Game struct { |
|
|
|
type Game struct { |
|
|
|
screen tcell.Screen |
|
|
|
screen tcell.Screen |
|
|
|
level []string |
|
|
|
level [][]rune |
|
|
|
player_x int |
|
|
|
player Position |
|
|
|
player_y int |
|
|
|
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() { |
|
|
|
func (game *Game) Render() { |
|
|
@ -26,7 +49,9 @@ func (game *Game) Render() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
game.screen.SetContent(game.player_x, game.player_y, '@', comb, tcell.StyleDefault) |
|
|
|
game.Text(game.status) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.screen.SetContent(game.player.x, game.player.y, '@', comb, tcell.StyleDefault) |
|
|
|
|
|
|
|
|
|
|
|
game.screen.Show() |
|
|
|
game.screen.Show() |
|
|
|
} |
|
|
|
} |
|
|
@ -41,9 +66,9 @@ func (game *Game) Occupied(x int, y int) bool { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
|
if !game.Occupied(game.player_x + x_delta, game.player_y + y_delta) { |
|
|
|
if !game.Occupied(game.player.x + x_delta, game.player.y + y_delta) { |
|
|
|
game.player_x += x_delta |
|
|
|
game.player.x += x_delta |
|
|
|
game.player_y += y_delta |
|
|
|
game.player.y += y_delta |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -88,30 +113,124 @@ 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_x = 1 |
|
|
|
game.player.x = 1 |
|
|
|
game.player_y = 1 |
|
|
|
game.player.y = 1 |
|
|
|
|
|
|
|
|
|
|
|
return &game |
|
|
|
return &game |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// This program just prints "Hello, World!". Press ESC to exit.
|
|
|
|
func compass(x int, y int, offset int) []Position { |
|
|
|
func main() { |
|
|
|
return []Position{ |
|
|
|
game := MakeGame() |
|
|
|
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) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
game.level = []string{ |
|
|
|
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() |
|
|
|
game.Render() |
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
for { |
|
|
|