Exemplo n.º 1
0
// On generates a maze on a given grid by using a sidewinder algorithm.
func (sw sidewinder) On(grid maze.Grid) {
	for _, row := range grid.EachRow() {
		run := make([]*maze.Cell, 0, len(row))

		for _, cell := range row {
			run = append(run, cell)

			atEasternBoundary := cell.East == nil
			atNorthanBoundary := cell.North == nil

			shouldCloseOut := atEasternBoundary ||
				(!atNorthanBoundary && sw.r.Intn(2) == 0)

			if shouldCloseOut {
				idx := sw.r.Intn(len(run))
				member := run[idx]
				if member.North != nil {
					member.Link(member.North)
				}
				run = run[:0]
			} else {
				cell.Link(cell.East)
			}
		}
	}
}