On Sep 16, 2004, at 3:09 PM, Barry Sperling wrote:

>
> 	Reading the recent posts about ant-wars, I thought that, as a nuby, 
> it would be interesting to try to recreate the ant-wars engine.  I 
> looked at their code a little, and then started in.  I appreciate that 
> James Edward Gray II provided us with his code, but I'll look at it 
> after I've made more progress.

You'll enjoy it, it's a fun project.

> 	I decided to create 2-d arrays to hold the data for given positions ( 
> hexagonal blocks ).  One would hold the amount of food at a position, 
> another would hold the traversability of it, etc.  My first attempt 
> failed in a strange way and the point of this email is: why?

See inline comments below...

> Method 1
>
> cols = 10
> empty_row_int = Array.new( cols, 0 )

Here you create an Array object and store a reference to that object in 
empty_row_int.

> $food_layout = Array.new
> rows.times { $food_layout.push( empty_row_int ) }

And here, you set each row to that same reference you created above.  
You aren't making a bunch of unique rows, you're  reusing the same row 
over and over again.  Change one and you change them all.

To fix it, try creating a new array as you define each row.  Remember, 
Ruby variables just hold references to the actual objects, not the 
objects themselves.

Hope that helps.

James Edward Gray II

> puts $food_layout[ 0 ][ 0 ]  # 0
> $food_layout[ 0 ][ 0 ] = 6   # SET FOOD ITEMS IN HEXAGON
> puts $food_layout[ 0 ][ 0 ]  # 6
> puts $food_layout[ 1 ][ 0 ]  # 6   WRONG!