Exemple #1
0
//execute move take a possible move to execute,
//keep track of it in the breadcrumbs and update coordination
func executeMove(move string, breadcrumbs *mazelib.Stack, visited map[mazelib.Coordinate]bool, co mazelib.Coordinate) (mazelib.Survey, mazelib.Coordinate, error) {
	//add the reverse move to the breadcrumbs stack
	breadcrumbs.Push(move)
	//perform the move
	survey, err := Move(move)
	//mark the new room as visited
	co = co.TranformByMove(move)
	visited[co] = true
	return survey, co, err
}
Exemple #2
0
func Iterate(survey mazelib.Survey, breadcrumbs *mazelib.Stack) {
	var err error
	co := mazelib.Coordinate{0, 0}

	//data structure to keep track of already visited rooms
	visited := make(map[mazelib.Coordinate]bool)
	//start from the cnter (0,0) and set it to visited
	visited[co] = true
	for true {

		//If solution found
		if err == mazelib.ErrVictory {
			fmt.Printf("Solution with %d steps found\n", breadcrumbs.Len())
			return
		}
		if err != nil {
			fmt.Printf("An error happend: |%v|\n", err)
			return
		}
		move, moveErr := getValidMove(survey, visited, co)

		//a move forward is possible
		if moveErr == nil {
			survey, co, err = executeMove(move, breadcrumbs, visited, co)
		} else if undoObj, stackErr := breadcrumbs.Pop(); stackErr != mazelib.ErrEmptyStack {
			// back up one step and try again
			undo := ReverseMove(undoObj.(string))
			survey, err = Move(undo)
			co = co.TranformByMove(undo)
		} else {
			fmt.Println("No solution found.")
			return
		}

	}
}