# Beginner Help with code example from book

**URL:** <https://www.ruby-forum.com/t/beginner-help-with-code-example-from-book/130578>\
**Category:** Ruby\
**Created:** [February 29, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/beginner-help-with-code-example-from-book/130578 "2008-02-29T15:55:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_LSCasale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@Joseph\_LSCasale](https://www.ruby-forum.com/u/Joseph_LSCasale)\
**Post date:** [February 29, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/beginner-help-with-code-example-from-book/130578/1 "2008-02-29T15:55:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Joseph\_LSCasale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@Joseph\_LSCasale](https://www.ruby-forum.com/u/Joseph_LSCasale)\
**Post date:** [February 29, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/beginner-help-with-code-example-from-book/130578/2 "2008-02-29T16:37:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Joseph\_LSCasale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@Joseph\_LSCasale](https://www.ruby-forum.com/u/Joseph_LSCasale)\
**Post date:** [February 29, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/beginner-help-with-code-example-from-book/130578/3 "2008-02-29T17:59:01Z")

</div>

> 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/](http://www.ruby-forum.com/).

Appreciate the pointer, thanks for bearing with me 🙂  
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

---

<div class="post-metadata">

**Author:** ![Joseph\_LSCasale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@Joseph\_LSCasale](https://www.ruby-forum.com/u/Joseph_LSCasale)\
**Post date:** [February 29, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/beginner-help-with-code-example-from-book/130578/4 "2008-02-29T19:58:58Z")

</div>

On Fri, Feb 29, 2008 at 10:58 AM, Joseph L. Casale  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.ruby-forum.com/).
> 
> Appreciate the pointer, thanks for bearing with me 🙂  
> 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

---

<div class="post-metadata">

**Author:** ![Joseph\_LSCasale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@Joseph\_LSCasale](https://www.ruby-forum.com/u/Joseph_LSCasale)\
**Post date:** [March 1, 2008, 12:37am UTC](https://www.ruby-forum.com/t/beginner-help-with-code-example-from-book/130578/5 "2008-03-01T00:37:09Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Joseph\_LSCasale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@Joseph\_LSCasale](https://www.ruby-forum.com/u/Joseph_LSCasale)\
**Post date:** [February 29, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/beginner-help-with-code-example-from-book/130578/6 "2008-02-29T19:41:40Z")

</div>

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].
