Exemplo n.º 1
0
Arquivo: icarus.go Projeto: mouk/gc6
//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
}
Exemplo n.º 2
0
Arquivo: icarus.go Projeto: mouk/gc6
//return a possible move toward not already visited room
func getValidMove(survey mazelib.Survey, visited map[mazelib.Coordinate]bool, co mazelib.Coordinate) (string, error) {
	if move := "down"; !survey.Bottom && !visited[co.TranformByMove(move)] {
		return move, nil
	}
	if move := "right"; !survey.Right && !visited[co.TranformByMove(move)] {
		return move, nil
	}
	if move := "up"; !survey.Top && !visited[co.TranformByMove(move)] {
		return move, nil
	}
	if move := "left"; !survey.Left && !visited[co.TranformByMove(move)] {
		return move, nil
	}
	return "", errors.New("No move is allowed")
}
Exemplo n.º 3
0
Arquivo: icarus.go Projeto: mouk/gc6
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
		}

	}
}