NullVoxPopuli
NullVoxPopuli

Reputation: 65183

Ruby + Rails3: really weird assignment of array problem

In my test:

@board.disp_s
@board.state = [1,0,0,1,0,0,0,0,0]
@board.disp_s

corresponding output:

Layout:
nilnilnil 
nilnilnil 
nilnilnil 

Layout:
100 
100 
110 

now... I'm pretty sure I told it to be

100
100
000

code from my model: http://pastebin.com/2Mpu7tU7

I'm sure that none of my methods being called by the test are modifying the @board_layout.... so I'm confused.

Upvotes: 0

Views: 47

Answers (1)

Don Roby
Don Roby

Reputation: 41165

The disp_s method is not computing the indexes correctly.

  def disp_s
    puts "Layout:"
    WIDTH.times do |row|
      WIDTH.times do |col|
        print @board_layout[col * row + col]
      end
      puts " "
    end

The col * row + col should likely be WIDTH * row + col.

I haven't looked carefully enough at your code to be sure there are no other issues, but this would definitely print incorrect values.

Upvotes: 3

Related Questions