S
Shawn W_
I'm trying to manipulate a 2D array. I have created a method that looks
in adjacent cells of my 2D array and returns the contents found there.
When my method tries to look for cells to the left of the far left
column, Ruby will naturally 'wrap around' and return the far right
column, because array index 0 - 1 = -1, which Ruby interprets as the
last element of an array, in this case the far right of my rectangular
2D array. Same for beyond the top row.
What I want to do is replicate this behaviour for looking beyond the
bottom row and far right column. In other words, I want my 2D array to
wrap around top and bottom, left and right (in truth I only want left
and right to wrap, and top and bottom to be blocked, but for now I'd
just settle for full wrap).
What would be the easiest solution. Perhaps modification or additions to
the Array class in my program? Perhaps some addtions to my Array2D
class?
in adjacent cells of my 2D array and returns the contents found there.
When my method tries to look for cells to the left of the far left
column, Ruby will naturally 'wrap around' and return the far right
column, because array index 0 - 1 = -1, which Ruby interprets as the
last element of an array, in this case the far right of my rectangular
2D array. Same for beyond the top row.
What I want to do is replicate this behaviour for looking beyond the
bottom row and far right column. In other words, I want my 2D array to
wrap around top and bottom, left and right (in truth I only want left
and right to wrap, and top and bottom to be blocked, but for now I'd
just settle for full wrap).
What would be the easiest solution. Perhaps modification or additions to
the Array class in my program? Perhaps some addtions to my Array2D
class?