Ejemplo n.º 1
0
func spawn(gen *chunk.Gen, chunks []*chunk.Chunk, pegIdx int, chunkIdx int) {
	for {
		pegs := gen.OpenPegs()
		if len(pegs) == 0 {
			return
		}
		peg := pegs[pegIdx%len(pegs)]
		fits := []chunk.OffsetChunk{}
		// Add the chunks that fit in the lattice
		for _, oc := range gen.FittingChunks(peg, chunks) {
			fits = append(fits, oc)
		}
		if len(fits) == 0 {
			gen.ClosePeg(peg)
			continue
		}
		gen.AddChunk(fits[chunkIdx%len(fits)])
		return
	}
}
Ejemplo n.º 2
0
func draw(gen *chunk.Gen, pegIdx int, chunkIdx int) {
	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
	min := image.Pt(math.MaxInt32, math.MaxInt32)
	for pt, _ := range gen.Map() {
		if pt.X < min.X {
			min.X = pt.X
		}
		if pt.Y < min.Y {
			min.Y = pt.Y
		}
	}

	min = min.Sub(image.Pt(4, 4))

	for pt, cell := range gen.Map() {
		nPegs := len(gen.PegsAt(pt))
		foreground := termbox.ColorWhite
		if len(gen.OpenPegs()) == 0 {
			foreground = termbox.ColorYellow
		}
		background := termbox.ColorBlack
		switch nPegs {
		case 0:
		case 1:
			background = termbox.ColorBlue
		case 2:
			background = termbox.ColorGreen
		default:
			background = termbox.ColorRed
		}
		screenPt := pt.Sub(min)
		termbox.SetCell(screenPt.X, screenPt.Y, rune(cell),
			foreground, background)
	}

	if pegIdx < len(gen.OpenPegs()) {
		fits := []chunk.OffsetChunk{}
		// Add the chunks that fit in the lattice
		for _, oc := range gen.FittingChunks(gen.OpenPegs()[pegIdx], chunks) {
			fits = append(fits, oc)
		}
		if len(fits) == 0 {
			gen.ClosePeg(gen.OpenPegs()[pegIdx])
		} else {
			oc := fits[chunkIdx%len(fits)]
			for y := oc.Bounds().Min.Y; y < oc.Bounds().Max.Y; y++ {
				for x := oc.Bounds().Min.X; x < oc.Bounds().Max.X; x++ {
					pt := image.Pt(x, y)
					screenPt := pt.Sub(min)
					if c, ok := oc.At(pt); ok {
						termbox.SetCell(screenPt.X, screenPt.Y, rune(c),
							termbox.ColorRed, termbox.ColorBlack)
					}
				}
			}
		}
	}

	termbox.Flush()
}