func solveMaze() { // Need to start with waking up to initialize a new maze // You'll probably want to set this to a named value and start by figuring // out which step to take next explored := make(map[common.Coordinate]Survey) src := common.NewCoordinate(0, 0) explored[src] = Survey{awake()} path := newPath() path.push(src) for { icarus, _ := path.top() if next, dir, found := pickNeighbor(icarus, explored); found { survey, err := Move(d2s[dir]) if err == mazelib.ErrVictory { return } if err != nil { panic(err) } path.push(next) explored[next] = Survey{survey} } else { dst, err := path.backtrack(explored) if err != nil { panic(err) } goback(icarus, dst, explored) } } }
// cutv tries to cut the rect vertically func (r rect) cutv(src, dst common.Coordinate) (rect, common.Coordinate, rect, common.Coordinate, bool) { if src.X == dst.X || r.W <= cutLimit { return rect{}, common.Coordinate{}, rect{}, common.Coordinate{}, false } cx := (src.X+dst.X)/2 + 1 cy := rand.Intn(r.H) + r.Y if cy == src.Y || cy == dst.Y { cy = rand.Intn(r.H) + r.Y } r1 := rect{X: r.X, Y: r.Y, W: cx - r.X, H: r.H} r2 := rect{X: cx, Y: r.Y, W: r.W - r1.W, H: r.H} if r1.contains(src) { return r1, common.NewCoordinate(cx-1, cy), r2, common.NewCoordinate(cx, cy), true } return r2, common.NewCoordinate(cx, cy), r1, common.NewCoordinate(cx-1, cy), true }
func createMaze() *Maze { m := emptyMaze() sx, sy := rand.Intn(m.Width()), rand.Intn(m.Height()) dx, dy := rand.Intn(m.Width()), rand.Intn(m.Height()) for dx == sx && dy == sy { dx, dy = rand.Intn(m.Width()), rand.Intn(m.Height()) } m.SetStartPoint(sx, sy) m.SetTreasure(dx, dy) m.addBoundary() m.buildMaze(common.NewCoordinate(sx, sy), common.NewCoordinate(dx, dy)) return m }
// cuth tries to cut the rect horizontally func (r rect) cuth(src, dst common.Coordinate) (rect, common.Coordinate, rect, common.Coordinate, bool) { if src.Y == dst.Y || r.H <= cutLimit { return rect{}, common.Coordinate{}, rect{}, common.Coordinate{}, false } cy := (src.Y+dst.Y)/2 + 1 cx := rand.Intn(r.W) + r.X if cx == src.X || cx == dst.X { cx = rand.Intn(r.W) + r.X } r1 := rect{X: r.X, Y: r.Y, W: r.W, H: cy - r.Y} r2 := rect{X: r.X, Y: cy, W: r.W, H: r.H - r1.H} if r1.contains(src) { return r1, common.NewCoordinate(cx, cy-1), r2, common.NewCoordinate(cx, cy), true } return r2, common.NewCoordinate(cx, cy), r1, common.NewCoordinate(cx, cy-1), true }