Beispiel #1
0
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()
}
Beispiel #2
0
func main() {

	var start_t int64
	var end_t int64

	var seed int64 = 0

	// Setup the aStar structs
	ast := astar.NewAStar(50, 50)

	p2p := astar.NewPointToPoint()
	p2l := astar.NewListToPoint(true)

	// Generate a random map
	grid, source, target, me := GenerateRandomMap(ast, seed, 50, 600, 24, 100000)
	PrintGrid(grid)

	// Route from source to target (point to point)
	start_t = time.Now().UnixNano()
	end := ast.FindPath(p2p, source, target)
	end_t = time.Now().UnixNano()

	first_path_t := float64(end_t-start_t) / float64(time.Millisecond)

	DrawPath(grid, end, "*")
	PrintGrid(grid)

	// record path as array so it can be used in the next search
	p := end
	path := make([]astar.Point, 0)
	for p != nil {
		path = append(path, p.Point)
		p = p.Parent
	}

	start_t = time.Now().UnixNano()
	end = ast.FindPath(p2l, path, me)
	end_t = time.Now().UnixNano()
	second_path_t := float64(end_t-start_t) / float64(time.Millisecond)

	DrawPath(grid, end, ".")
	PrintGrid(grid)

	fmt.Println("me", me)
	fmt.Println("end", end)
	fmt.Println("end_grid", grid[end.Row][end.Col])
	fmt.Println(first_path_t)
	fmt.Println(second_path_t)
}