From: Austin Ziegler Date: 2004-09-18T00:30:29+09:00 Subject: Re: 2-d arrays On Fri, 17 Sep 2004 05:09:56 +0900, 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. > > 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? > Method 1 > cols = 10 > empty_row_int = Array.new( cols, 0 ) > $food_layout = Array.new > rows.times { $food_layout.push( empty_row_int ) } > 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! Try: cols = 10 rows = 10 food_layout = Array.new(rows) { Array.new(cols, 0) } 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] # -> 0 -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca : as of this email, I have [ 6 ] Gmail invitations