转置5x5阵列时的索引错误

时间:2015-04-14 06:06:21

标签: ruby arrays transpose

鉴于以下“宾果板”阵列:

board =        [[47, 44, "X", 8, 88],
                [22, 69, "X", 65, 73],
                [83, 85, "X", 89, 57],
                [25, 31, "X", 68, 51],
                [75, 70, "X", 80, 83]]

并运行以下方法以查看我是否有宾果游戏:

def bingo(board)
  if board.each_index {|e| board[e].uniq!} .any? {|row| row == ["X"]} == true
    p "BINGO!"
  elsif
    board = board.transpose
    board.each_index {|e| board[e].uniq!} .any? {|row| row == ["X"]} == true
    p "BINGO!"
  elsif (board[0][0] && board[1][1] && board[2][2] && board[3][3] && board[4][4]) == "X"
    p "BINGO!"
  elsif board[0][4] && board[1][3] && board[2][2] && board[3][1] && board[4][0] == "X"
    p "BINGO!"
  else
    p "no bingo"
  end
end

我遇到了以下错误:

`transpose': element size differs (5 should be 4) (IndexError)

是什么给出的?为什么认为5是4?

1 个答案:

答案 0 :(得分:3)

我相信你的第二行,board[e].uniq!会破坏性地更改嵌套在原始board数组中的每个数组...因此,每行中不再有五个对象。