Example #1
0
// StartAll finds all organisms driven by Cpu instances, and spawns a goroutine
// to begin executing each Cpu instance found.
func StartAll(g grid2d.Grid) {
	var locs []grid2d.Point
	g.Locations(&locs)
	for _, p := range locs {
		if o, ok := p.V.(*org.Organism); ok {
			if c, ok := o.Driver.(*Cpu); ok {
				go c.Run(o)
			}
		}
	}
}
Example #2
0
// Count counts the number of occupants in g that satisfy fn.
func Count(g grid2d.Grid, fn CounterFunc) int {
	var locs []grid2d.Point
	g.Locations(&locs)

	var count int
	for _, p := range locs {
		if fn(p.V) {
			count += 1
		}
	}
	return count
}
Example #3
0
// PrintWorld renders g to w.
func PrintWorld(w io.Writer, g grid2d.Grid) {
	points := locPool.Get().([]grid2d.Point)
	width, height, _ := g.Locations(&points)
	sort.Sort(byCoordinate(points))

	iy, ix := 0, -1
	addHeader(w, width)

	for _, p := range points {
		fillBefore(w, p.X, p.Y, width, &ix, &iy)
		writeRune(w, RuneForOccupant(p.V))
		ix += 1
	}
	locPool.Put(points)
	fillBefore(w, width, height-1, width, &ix, &iy)
	writeRune(w, rightRune)
	writeRune(w, '\n')
	addFooter(w, width)
}