def get_path(sx, sy, ex, ey)
map = []
for y in 0...@map_size[0]
map[y] = []
for x in 0...@map_size[1]
map[y][x] = 0
end
end
map[ey][ex] = 1
old_positions = []
new_positions = []
old_positions << [ex, ey]
depth = 2
depth.upto(100) { |step|
loop do
break if old_positions[0] == nil
x, y = old_positions.shift
return [true, map, step] if x == sx and y + 1 == sy
if passable?(x, y + 1) and map[y+1][x] == 0 then
map[y+1][x] = step
new_positions << [x, y+1]
end
return [true, map, step] if x-1 == sx and y == sy
if passable?(x - 1,y) and map[y][x-1] == 0 then
map[y][x-1] = step
new_positions << [x-1, y]
end
return [true, map, step] if x+1 == sx and y == sy
if passable?(x + 1,y) and map[y][x + 1] == 0 then
map[y][x + 1] = step
new_positions << [x+1, y]
end
return [true, map, step] if x == sx and y - 1 == sy
if passable?(x, y - 1) and map[y-1][x] == 0 then
map[y-1][x] = step
new_positions << [x, y-1]
end
end
old_positions = new_positions
new_positions = []
}
return [false, nil, nil]
end
path = [boolean: path_finished?, [map], current_step]
which player follows (goes from a to b, where b = a - 1. Fairly simple, but it works.Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill