开发者

Ruby, can't convert fixnum into array? but it is an array

boardwidth is 3

i represend a 3x3 matrix by a 1x9 array

the test:

  test "the row checking to see if we have a winner (incorrect)" do
    board = Board.new

    board.state = [0,0,1,0,0,1,0,0,1]
    assert false ==开发者_开发百科board.check_rows_for_winner
  end

relevant code

  @board_layout = []

 def init_board
    @board_layout = Array.new(@@board_width * @@board_width)
  end

  def state=(custom_board)
    @board_layout = custom_board
  end


def check_rows_for_winner
  self.width.times do |row|
    if @board_layout.transpose[row].uniq.size == 1 then 
      return true
    end
  end

  return false
end

error:

TypeError: can't convert Fixnum into Array
    app/models/board.rb:39:in `transpose'
    app/models/board.rb:39:in `check_rows_for_winner'
    app/models/board.rb:38:in `times'
    app/models/board.rb:38:in `check_rows_for_winner'


In order to use @array.tranpose, your @array needs to be array of arrays. With normal array you get this error message.

Edit:

In your test you are setting the following for the @board_layout:

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

and when you do

@board_layout.transpose[row]

You'll get the error message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜