*From Chris Pine, Learn to Program

Hey All --
In the below source code they array is called in the method as x, y,
but the "if" statements are looking to [y][x].  Can anyone explain to
me why they are switched?  If you make them [x][y] it still outputs
the correct number (23).
Thanks in advance,
Jordan

## Start of Example

# 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,M],
           [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],
           [M,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 x > 10 && y > 10
   size = 0
  else
    if world[x][y] != ' land '
     return 0
    end
  end
# Either it ' s water or we already
# counted it, but either way, we don ' t
# want to count it now.


# So first we count this tile...
size = 1
world[x][y] = ' 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

puts continent_size(world, 5, 5)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to