Beginner Help with code example from book

I am trying to understand how the following code knows not to count a
tile that has been previously counted, its an example from Chris P.'s
Learn to Program from Pragmatic Bookshelf. I understand how the first
size = size + … recursively starts counting, but for example when you
go to the next size = … how is the possible overlap accounted for?

Thanks!
jlc

These are just to make the map

easier for me to read. “M” is

visually more dense than “o”.

M = ’ land’
o = ’ water’
world = [[o,o,o,o,o,o,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,o,o,M,o,M,M,o,o,o,o],
[o,o,o,o,M,M,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,o],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,o,o,o]]
def continent_size world, x, y
if world[y][x] != ’ land’

Either it’ s water or we already

counted it, but either way, we don’ t

want to count it now.

return 0
end

So first we count this tile…

size = 1
world[y][x] = ’ counted land’

…then we count all of the

neighboring eight tiles (and,

of course, their neighbors by

way of the recursion).

size = size + continent_size(world, x-1, y-1)
size = size + continent_size(world, x , y-1)
size = size + continent_size(world, x+1, y-1)
size = size + continent_size(world, x-1, y )
size = size + continent_size(world, x+1, y )
size = size + continent_size(world, x-1, y+1)
size = size + continent_size(world, x , y+1)
size = size + continent_size(world, x+1, y+1)
size
end

Joseph L. Casale wrote:

I am trying to understand how the following code knows not to count a
tile that has been previously counted, its an example from Chris P.'s

def continent_size world, x, y
if world[y][x] != ’ land’

Either it’ s water or we already

counted it, but either way, we don’ t

want to count it now.

return 0
end
:
world[y][x] = ’ counted land’

The world is changed. When ’ land’ is counted, it turns into ’ counted
land’ so on iteration, the “if” sees that it is not land and returns 0.

The world is changed. When ’ land’ is counted, it turns into ’ counted
land’ so on iteration, the “if” sees that it is not land and returns 0.


Posted via http://www.ruby-forum.com/.

Appreciate the pointer, thanks for bearing with me :slight_smile:
Why has the author reversed the “X” and “Y”? on a square world it would
not matter, but… Was there any significance to this?

Thanks again!
jlc

On Fri, Feb 29, 2008 at 10:58 AM, Joseph L. Casale
[email protected] wrote:

The world is changed. When ’ land’ is counted, it turns into ’ counted
land’ so on iteration, the “if” sees that it is not land and returns 0.


Posted via http://www.ruby-forum.com/.

Appreciate the pointer, thanks for bearing with me :slight_smile:
Why has the author reversed the “X” and “Y”? on a square world it would not matter, but… Was there any significance to this?

I’m pretty sure it doesn’t matter on any surface as long all tiles are
described in 2 dimensions (which they would have to, if you use a grid
of x’s and y’s.) Think, for example, the surface of a 3-D globe cut
up into 4-sided tiles of different size. The only drawback is that
you have to pick poles to topologically shrink a sphere into a surface
(this is often done by turning it into a torus/donut, but doesn’t have
to be)

I haven’t read Pine’s book, but the x’s and y’s switcheroo can be
explained, I suppose, like this…

On a mathematical graph, a larger x means further right, and a larger
y means further up. In a computer image, you have to remember, of
course, larger y usually means further down.

So, think of [0,3]. On an xy grid, that point would lie 3 units up on
the vertical axis.

For a multi-dimensional array [0,3] refers to the zeroth array, third
item (or, first array and fourth item, depending on how your mind
works).

In your visual multi-dimensional array world map, that would be the
fourth tile to the right, which would – at least for most people –
be the x dimension, and not the y.

Hope that makes some sense,
Todd

For a multi-dimensional array [0,3] refers to the zeroth array, third
item (or, first array and fourth item, depending on how your mind
works).

In your visual multi-dimensional array world map, that would be the
fourth tile to the right, which would – at least for most people –
be the x dimension, and not the y.

Hope that makes some sense,
Todd

Mark, Todd,
Thanks for your time,
I really appreciate it!
jlc

Joseph L. Casale wrote:

Why has the author reversed the “X” and “Y”? on a square world it would
not matter, but… Was there any significance to this?

Because of the way the world has been defined. The definition is visual
and so it is natural to look at it and think of “x” as across and “y” as
up and down. That means that “y” refers to the row and “x” as the item
in the row. The world is an array of rows, so the row index comes
first. Hence [y][x].