From: Joop Van den tillaart Date: 2007-06-21T21:02:44+09:00 Subject: Help needed with my grid and code Hey guys, Im in need of some help here...i have a lot of code (with help of a teacher) and now i need to figure out how this code works and i have to improve it. The code works out a grid of 14 x 14 cells...then it places some roads (cell type 1 cell distance to road = 0) in the last column, the 6th row and the 6th column: grid.column(13).each do |cell| cell.type = 1 cell.distance_road = 0 end grid.column(6).each do |cell| cell.type = 1 cell.distance_road = 0 end grid.row(6).each do |cell| cell.type = 1 cell.distance_road = 0 end What i need to have is the distance of each cell to these road cells...But since im a noob at ruby maybe someone can help me by overlooking the code and see what is wrong. Because till now it prints out this: xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 xx xx xx xx xx xx 00 Where you can see 00 as roads (why isnt this 10 via): grid.column(13).each do |cell| cell.type = 1 cell.distance_road = 0 and it has to be something like this: 06 05 04 03 02 01 00 01 02 03 03 02 01 00 05 05 04 03 02 01 00 01 02 03 03 02 01 00 04 04 04 03 02 01 00 01 02 03 03 02 01 00 03 03 03 03 02 01 00 01 02 03 03 02 01 00 02 02 02 02 02 01 00 01 02 02 02 02 01 00 01 01 01 01 01 01 00 01 01 01 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 01 01 01 00 01 01 01 01 01 01 00 02 02 02 02 02 01 00 01 02 02 02 02 01 00 03 03 03 03 02 01 00 01 02 03 03 02 01 00 04 04 04 03 02 01 00 01 02 03 03 02 01 00 05 05 04 03 02 01 00 01 02 03 03 02 01 00 06 05 04 03 02 01 00 01 02 03 03 02 01 00 06 05 04 03 02 01 00 01 02 03 03 02 01 00 Where 00 are those cells representing roads and the other cells all have type 0 and the second digit is the distance to the nearest roadcell... please help me because im stuck! Here's the total ruby code (maybe its a lot but i hope some ruby experts can read a lot faster through the code as i can): class Grid attr :width attr :height def initialize(width, height) @grid = Array.new(width * height) @width = width @height = height end def [](x, y) raise IndexError.new("Index (#{x}, #{y}) out of range") if x < 0 || y < 0 || x >= @width || y >= @width @grid[y * width + x] end def []=(x, y, value) raise IndexError.new("Index (#{x}, #{y}) out of range") if x < 0 || y < 0 || x >= @width || y >= @width @grid[y * width + x] = value end def column(column) Column.new(self, column) end def row(row) Row.new(self, row) end def neighbours(x, y) result = [] (x - 1).step(x + 1) do |xx| (y - 1).step(y + 1) do |yy| next if x == xx && y == yy begin result << self[xx, yy] rescue IndexError end end end result end def each (@width * @height).times { |i| yield @grid[i] } end def each_index @height.times do |y| @width.times do |x| yield x, y end end end def map (@width * @height).times { |i| @grid[i] = yield @grid[i] } end alias_method :collect, :map def to_a result = [] @height.times do |y| row = [] @width.times { |x| row << self[x, y] } result << row end result end def inspect "Grid#{to_a.inspect}" end private class Column def initialize(grid, column) @grid = grid @column = column end def [](i) @grid[@column, i] end def []=(i, value) @grid[@column, i] = value end def size @grid.height end def each size.times { |i| yield self[i] } end def map size.times { |i| self[i] = yield self [i] } end alias_method :collect, :map def to_a result = [] size.times { |i| result << self[i] } result end def inspect "Grid.Column#{to.inspect}" end end class Row def initialize(grid, row) @grid = grid @row = row end def [](i) @grid[i, @row] end def []=(i, value) @grid[i, @row] = value end def size @grid.width end def each size.times { |i| yield self[i] } end def map size.times { |i| self[i] = yield self[i] } end alias_method :collect, :map def to_a result = [] size.times { |i| result << self[i] } result end def inspect "Grid.Row#{to_a.inspect}" end end end class Cell attr_accessor :type attr_accessor :distance_road def inspect "Cell[#{type}, #{distance_road || 'nil'}]" end end # Voorbeeld met wegen uit email grid = Grid.new(14, 14) # Zet alle cellen op 0 grid.map do cell = Cell.new cell.type = 0 cell end # Maak de 3 wegen grid.column(13).each do |cell| cell.type = 1 cell.distance_road = 0 end grid.column(6).each do |cell| cell.type = 1 cell.distance_road = 0 end grid.row(6).each do |cell| cell.type = 1 cell.distance_road = 0 end p grid 0.times do grid.each_index do |x, y| #next if grid[x, y].distance_road n = grid.neighbours(x, y) c = grid[x, y] n.each do |cell| if cell.distance_road if c.distance_road.nil? || c.distance_road > cell.distance_road c = cell end end end if c.distance_road grid[x, y].distance_road = c.distance_road + 1 end end end grid.height.times do |y| grid.width.times do |x| if grid[x, y].distance_road print " %02d " % grid[x, y].distance_road else print" xx " end end print "\n" end I hope someone can help me thanks!!! -- Posted via http://www.ruby-forum.com/.