I am looking for the below code to be able to print out and return my final_path
array from my #find_knight_path function
. It runs just fine with the puts lines I have, but as soon as I try to puts the final_path
array or print the return value of the function, I am getting infinite loop behavior in the console and I have no idea why.
I know the function is a little too big. That's my next item to tackle. Any insight is greatly appreciated!
Full code is here: Below code from this link is in lines 244 - 274.
def find_knight_path(graph, start_pos, end_pos)
queue = []
final_path = []
distance = 0
predecessor = nil
pos_start = graph.nodes[start_pos]
pos_start.distance = 0
pos_start.predecessor = nil
queue.push(pos_start)
final_path.push(pos_start)
while !queue.empty?
current_item = queue.shift
# puts "line 97 - What is current_item.value? #{current_item.value}"
distance += distance
predecessor = current_item
current_item.adjacent_nodes.each do |i|
i.distance = distance
i.predecessor = predecessor
queue.push(i)
end
puts "final_path value - #{current_item}"
puts "final_path length - #{final_path.length}"
final_path.push(current_item)
# p "final_path #{final_path}"
# p "queue #{queue}"
return final_path.reverse if current_item.value == end_pos
# puts "final_path length #{final_path.length}"
end
final_path.reverse
end