/
main.go
65 lines (53 loc) · 1.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"github.com/nickdavies/go-astar/astar"
"github.com/nsf/termbox-go"
"math/rand"
"os"
"time"
)
func printString(col, row int, text string) {
for i, ch := range text {
termbox.SetCell(col+i, row, ch, termbox.ColorDefault, termbox.ColorDefault)
}
}
func showIntro() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
printString(10, 5, "After a long demon hunt, you finally arrive back home at the Isle of Hunters")
printString(10, 6, "to rest for the winter...")
termbox.Flush()
termbox.PollEvent()
}
func showGrid() {
cols, rows := termbox.Size()
a := astar.NewAStar(rows, cols)
seed := time.Now().UnixNano()
r := rand.New(rand.NewSource(seed))
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
source := astar.Point{Col: r.Intn(cols), Row: r.Intn(rows)}
target := astar.Point{Col: r.Intn(cols), Row: r.Intn(rows)}
for i := 0; i < 400; i++ {
wall := astar.Point{Col: r.Intn(cols), Row: r.Intn(rows)}
a.FillTile(wall, -1)
termbox.SetCell(wall.Col, wall.Row, '#', termbox.ColorDefault, termbox.ColorDefault)
}
path := a.FindPath(astar.NewPointToPoint(), []astar.Point{source}, []astar.Point{target})
for path != nil {
path = path.Parent
termbox.SetCell(path.Col, path.Row, '.', termbox.ColorDefault, termbox.ColorDefault)
path = nil //path.Parent
}
termbox.SetCell(source.Col, source.Row, 's', termbox.ColorDefault, termbox.ColorDefault)
termbox.SetCell(target.Col, target.Row, 't', termbox.ColorDefault, termbox.ColorDefault)
termbox.Flush()
termbox.PollEvent()
}
func main() {
err = termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
showIntro()
showGrid()
}