Help needed with my grid and code

  • Thread starter Joop Van den tillaart
  • Start date
J

Joop Van den tillaart

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 }
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 = yield @grid }
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 }
end

def map
size.times { |i| self = yield self }
end

alias_method :collect, :map

def to_a
result = []
size.times { |i| result << self }
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 }
end

def map
size.times { |i| self = yield self }
end

alias_method :collect, :map

def to_a
result = []
size.times { |i| result << self }
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!!!
 
J

Joop Van den tillaart

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top