Return nothing when looking outside the bounds of 2D array?

D

Dave Howell

Shawn W_ wrote:
=20
I think I got it. I did away with storing my cell data in a hash; = rather=20
I will put an array in each cell of the 2D array, and add another=20
parameter to the def [] and def []=3D methods to access it.

I don't recommend scrapping working code, but in the future, you might =
want to think about doing something a little more ruby-flavored. =
Something like, oh, maybe this....


def HexCell < Array
blah blah
end

ary =3D HexArray(3,4)
=3D> (Array of HexCells)
ary[2,3].neighbor:)upperleft)
=3D> HexCell at [2,2]

Delta would be defined like this:

Delta=3D{:center =3D> [0,0], :upperright =3D> [0.5,-1], :right =
=3D> [1,0], :lowerright =3D> [0.5,1], :lowerleft =3D> [-0.5,1], :left =3D>=
[-1,0], :upperleft =3D> [-0.5,-1]]
 
D

Dave Howell

=20
=20
=20
However, it still falls over when I try to insert the above into my = main=20
program, as below:
=20
=20
def insert_teleporting_square
m =3D rand(@cols)
n =3D rand(@rows)
@world[m,n,0]["hex_id_A"] =3D "A "
@world[m,n,1]["hex_id_A"] =3D "H "
@world[m,n,2]["hex_id_A"] =3D "H "
@world[m,n,3]["hex_id_A"] =3D "H "
@world[m,n,4]["hex_id_A"] =3D "A "
@world[m,n,5]["hex_id_A"] =3D "A "
@world[m,n,6]["hex_id_A"] =3D "A "
@world[m,n,7]["hex_id_A"] =3D "A "
@world[m,n,8]["hex_id_A"] =3D "A "
end
=20
=20
=20
It falls over when it hits one of these two lines:
@world[m,n,1]["hex_id_A"] =3D "H "
@world[m,n,4]["hex_id_A"] =3D "A "
=20
With error as before:
undefined method `[]=3D' for nil:NilClass (NoMethodError)
=20
What my program does is generate an array, then populates each cell of=20=
that array with a hash. It looks like when it tries to access the hash=20=
values outside the array boundary it runs into problems? Do I need=20
another method inside the Array2D class to deal with hash access? If = so,=20
how can this be done? If not, can anyone see any other reason for the=20=

I think I got it. I did away with storing my cell data in a hash; = rather=20
I will put an array in each cell of the 2D array, and add another=20
parameter to the def [] and def []=3D methods to access it.

Forgive me, but this looks like you're just being too lazy, and your =
code's starting to really suffer. Instead of trying to make it work no =
matter how much stuff you throw at the array, start checking to make =
sure that you're not doing something illegal or irrational before trying =
to do it.
a[3,3,2] =3D> "-"
a[4,3,2]
=3D> nil

Because a[5,1] is out of bounds

So
a[3,3,2]=3D"Q" =3D>"Q"
a[4,3,2]=3D"Q"
=3D> undefined method `[]=3D' for nil:NilClass (NoMethodError)

Because that's like saying=20
nil=3D"Q"


One way you could check would be=20
@world[m,n,1]["hex_id_A"] =3D "H " if @world[m,n,1]

Or, to put it in context:

def insert_teleporting_square
m =3D rand(@cols)
n =3D rand(@rows)
(0..8).each {|index| @world[m,n,index]["hex_id_A"] =3D "A " if =
@world[m,n,index]}
(1..3).each {|index| @world[m,n,index]["hex_id_A"] =3D "H " if =
@world[m,n,index]}
end


Here's another way:

class HexArray
def neighbors(x,y)
(1..6).collect{|index| index if =
self[x,y,index]}.compact
end
end
a =3D HexArray.new(5,5)
a.neighbors(2,2) =3D> [1, 2, 3, 4, 5, 6]
a.neighbors(4,4) =3D> [1, 5, 6]
a.neighbors(2,2).each{|n| a[2,2,n]=3D"x"} =3D> [1, 2, 3, 4, 5, 6]
a.neighbors(4,4).each{|n| a[4,4,n]=3D"y"} =3D> [1, 5, 6]
a
- - - - -
- x x - -
- x - x -
- x x y y
- - - y -
 

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
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top