#!/usr/bin/env ruby

require "sp_ext"

s = 1000
b_dens = 0.1
t_dens = 0.01
map=[];
map[0] = "#"*(s+2)
s.times do |i|
  map[i+1] = "#"
  map[i+1] << " "*s << "#"
end
map << "#"*(s+2)

puts map.size

(s*s*b_dens).to_i.times do |i|
  map[rand(s)+1][rand(s)+1] = "#"[0]
end

targets = (s*s*t_dens)
targets = 0
targets.to_i.times do |i|
  map[rand(s)+1][rand(s)+1] = "@"[0]
end

10.times do |i|
  map[rand(s)+1][rand(s)+1] = i.to_s[0]
end

#puts map

def find(m,a)
  i = 0
  x = nil
  y = 0
  for s in m
    break if (x = s.index(a))
    y+=1;
  end
  [x,y]
end

def conv(m)
  puts "conversion..."
  s1 = "@ #0-9"
  s2 = "    "
  s2[0],s2[1],s2[2],s2[3] = 0,1,255,1
  m = m.map { |s| s.tr(s1,s2) }
  puts "conversion ready."
  m
end

sp = PathSearch.new(map[0].size,map.size,conv(map));

10.times do
  10.times do |i|
    x,y = find(map,i.to_s)
    if( x )
      puts "source found : #{x},#{y}"
      result = sp.unweighted_search(x,y)
      puts result
   
      #map3 = map.map{ |s| s.tr("0-9"," ") }
      pt = [x,y]
      i=0
      #map3[pt[1]][pt[0]] = "."[0] while sp.next(pt)
      i+=1 while sp.next(pt)
      puts i
    #puts map3
    end
  end
end
