1

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'

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.