samples/13_path_finding_algorithms/07_heuristic_with_walls/app/main.rb
def heuristic_one_step_forward
    # Stop the search if the target has been found
    return if heuristic.came_from.has_key?(grid.target)

    # If the search has not begun
    if heuristic.came_from.empty?
      # Setup the search to begin from the star
      heuristic.frontier << grid.star
      heuristic.came_from[grid.star] = nil
    end

    # One step in the heuristic search

    # Unless there are no more cells to explore from
    unless heuristic.frontier.empty?
      # Get the next cell to explore from
      new_frontier = heuristic.frontier.shift
      # For each of its neighbors
      adjacent_neighbors(new_frontier).each do |neighbor|
        # That have not been visited and are not walls
        unless heuristic.came_from.has_key?(neighbor) || grid.walls.has_key?(neighbor)
          # Add them to the frontier and mark them as visited
          heuristic.frontier << neighbor
          heuristic.came_from[neighbor] = new_frontier
        end
      end
    end

    # Sort the frontier so that cells that are in a zigzag pattern are prioritized over those in an line
    heuristic.frontier = heuristic.frontier.sort_by {| cell | proximity_to_star(cell) }
    # Sort the frontier so cells that are close to the target are then prioritized
    heuristic.frontier = heuristic.frontier.sort_by {| cell | heuristic_heuristic(cell)  }

    # If the search found the target
    if heuristic.came_from.has_key?(grid.target)
      # Calculate the path between the target and star
      heuristic_calc_path
    end
  end
# Moves the heuristic search forward one step
# Can be called from tick while the animation is playing
# Can also be called when recalculating the searches after the user edited the grid
Moves the heuristic search forward one step
Can be called from tick while the animation is playing
Can also be called when recalculating the searches after the user edited the grid
Moves the heuristic search forward one step Can be called from tick while the animation is playing Can also be called when recalculating the searches after the user edited the grid
def heuristic_one_step_forward
    return if heuristic.came_from.has_key?(grid.target)
    if heuristic.came_from.empty?
      heuristic.frontier << grid.star
      heuristic.came_from[grid.star] = nil
    end
    unless heuristic.frontier.empty?
      new_frontier = heuristic.frontier.shift
      adjacent_neighbors(new_frontier).each do |neighbor|
        unless heuristic.came_from.has_key?(neighbor) || grid.walls.has_key?(neighbor)
          heuristic.frontier << neighbor
          heuristic.came_from[neighbor] = new_frontier
        end
      end
    end
    heuristic.frontier = heuristic.frontier.sort_by {| cell | proximity_to_star(cell) }
    heuristic.frontier = heuristic.frontier.sort_by {| cell | heuristic_heuristic(cell)  }
    if heuristic.came_from.has_key?(grid.target)
      heuristic_calc_path
    end
  end
def heuristic_one_step_forward return if heuristic . came_from . has_key? ( grid . target ) if heuristic . came_from . empty? heuristic . frontier << grid . star heuristic . came_from [ grid . star ] = nil end unless heuristic . frontier . empty? new_frontier = heuristic . frontier . shift adjacent_neighbors ( new_frontier ) . each do | neighbor | unless heuristic . came_from . has_key? ( neighbor ) || grid . walls . has_key? ( neighbor ) heuristic . frontier << neighbor heuristic . came_from [ neighbor ] = new_frontier end end end heuristic . frontier = heuristic . frontier . sort_by { | cell | proximity_to_star ( cell ) } heuristic . frontier = heuristic . frontier . sort_by { | cell | heuristic_heuristic ( cell ) } if heuristic . came_from . has_key? ( grid . target ) heuristic_calc_path end end
Moves the heuristic search forward one step
Can be called from tick while the animation is playing
Can also be called when recalculating the searches after the user edited the grid
Moves the heuristic search forward one step Can be called from tick while the animation is playing Can also be called when recalculating the searches after the user edited the grid
# Stop the search if the target has been found # If the search has not begun # Setup the search to begin from the star # One step in the heuristic search # Unless there are no more cells to explore from # Get the next cell to explore from # For each of its neighbors # That have not been visited and are not walls # Add them to the frontier and mark them as visited # Sort the frontier so that cells that are in a zigzag pattern are prioritized over those in an line # Sort the frontier so cells that are close to the target are then prioritized # If the search found the target # Calculate the path between the target and star