# Help needed with my grid and code

**URL:** <https://www.ruby-forum.com/t/help-needed-with-my-grid-and-code/107409>\
**Category:** Ruby\
**Created:** [June 21, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/help-needed-with-my-grid-and-code/107409 "2007-06-21T14:02:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tillaart36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tillaart36](https://www.ruby-forum.com/u/tillaart36)\
**Post date:** [June 21, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/help-needed-with-my-grid-and-code/107409/1 "2007-06-21T14:02:40Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![tillaart36](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tillaart36](https://www.ruby-forum.com/u/tillaart36)\
**Post date:** [June 21, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/help-needed-with-my-grid-and-code/107409/2 "2007-06-21T14:34:59Z")

</div>

So i adjusted some of my code and now it is a little bit better…the  
last part of the code is now:

grid.height.times do |y|  
grid.width.times do |x|  
if grid[x, y].distance\_road  
print grid[x, y].type  
print grid[x, y].distance\_road  
else  
print grid[x, y].type  
print grid[x, y].distance\_road

```
end

```

end  
print “\n”  
end

and generates:

0nil0nil0nil0nil0nil011101020304050111  
0nil0nil0nil0nil02011101020304020111  
0nil0nil0nil0302011101020303020111  
0nil0nil040302011101020303020111  
0nil05040302011101020303020111  
0101010101011101010101010111  
1111111111111111111111111111  
0202020202011101020202020111  
0303030302011101020303020111  
0404040302011101020303020111  
0505040302011101020303020111  
0605040302011101020303020111  
0605040302011101020303020111  
0605040302011102020303020111

Anyone has any pointers what im doing wrong? Thanks!
