无法将Fixnum转换为数组(TypeError)Ruby

时间:2015-04-08 13:12:56

标签: ruby arrays loops

我收到错误:

minesweeper.rb:32:in '|': can't convert Fixnum into Array (TypeError)
   from minesweeper.rb:32:in 'block in create_hint_board'
   from minesweeper.rb:31:in 'each_index'
   from minesweeper.rb:31:in 'create_hint_board'
   from minesweeper.rb:68:in '(main)'

尝试检查2D数组中的值,并将1添加到与该索引位置相邻的所有单元格。该错误发生在subarray2 = board | i |。我试图迭代整个2D数组

整个代码是

    #def load_board(file)
#   gameboard = File.readlines(file)[1..-1]
#       gameboard.map! do |line|
#           line.split.map(&:to_s)
#       end
#   $globalarray = gameboard
#end

$globalarray = [['*','.','.','.'],['.','.','*','.'],['.','.','.','.']]

def pp_board(board)
    puts Array.new(board[0].size*2+1, '-').join('')
    board.each do |row|
        puts "|" + row.join("|") + "|"
        puts Array.new(row.size*2+1, '-').join('')
    end
end

def create_hint_board(board)
    board = $globalarray
    $globalarray.each_index do |i|
        subarray = $globalarray[i]
            subarray.each_index do |j|
                if $globalarray[i][j] != '*'
                    board[i][j].to_i
                    board[i][j] = 0                 
                end
                puts "#{String(i)},#{String(j)} is #{board[i][j]}" 
            end
        end
    board.each_index do |i| 
        subarray2 = board|i|
            subarray2.each_index do |j|
                if board[i][j] == '*'
                    board[i+1][j] = board[i+1][j]+1
                    board[i+1][j+1] = board[i+1][j+1]+1
                    board[i+1][j-1] = board[i+1][j-1]+1
                    board[i][j-1] = board[i][j-1]+1
                    board[i][j+1] = board[i][j+1]+1
                    board[i-1][j] = board[i-1][j]+1
                    board[i-1][j+1] = board[i-1][j+1]+1
                    board[i-1][j-1] = board[i-1][j-1]+1
                end
            end
    end

puts "new array is "
puts board  
end

=begin
#def copy_to_blank(board)
#   $newarrayblank = $newarray
#   $newarrayblank.each_index do |i|
#       subarray = $newarrayblank[i]
#           subarray.each_index do |j|
#               $newarrayblank[i][j] = '.'
#               puts "#{String(i)},#{String(j)} is #{$newarrayblank[i][j]}" 
#           end
#       end
#end

#load_board("mines.txt")
blank = [[]]
=end
puts "Original array is"
puts $globalarray
create_hint_board($globalarray)
#pp_board($globalarray)
#create_hint_board($globalarray)
#puts "new array is"
#pp_board($newarray)
#puts "new blank board is"
#copy_to_blank(blank)
#puts $newarrayblank
#pp_board($newarrayblank)
=begin

puts "Input Guess"
value1 = gets.split(" ")
row_guess = value1[0].to_i
col_guess = value1[1].to_i

puts $newarray[row_guess][col_guess]

while $newarray[row_guess][col_guess] != '*'

    if $newarray[row_guess][col_guess] != '*'
        puts "You guessed row #{row_guess} and column #{col_guess}."
        puts $newarray[row_guess][col_guess]
        #$newarrayblank[row_guess][col_guess] = $newarray[row_guess][col_guess]
        #pp_board($newarrayblank)
        puts "Input your guess in coordinates, separated by a blank space, or press q to quit."
        value1 = gets.split(" ")
        row_guess = value1[0].to_i
        col_guess = value1[1].to_i

    elsif $newarray[row_guess][col_guess] == '*'
        puts "You guessed row #{row_guess} and column #{col_guess}."
        puts "You hit a mine!"
        puts "Game Over"
    end
end
=end

给我带来麻烦的地方是

board.each_index do |i| 
            subarray2 = board|i|
                subarray2.each_index do |j|
                    if board[i][j] == '*'
                        board[i+1][j] = board[i+1][j]+1
                        board[i+1][j+1] = board[i+1][j+1]+1
                        board[i+1][j-1] = board[i+1][j-1]+1
                        board[i][j-1] = board[i][j-1]+1
                        board[i][j+1] = board[i][j+1]+1
                        board[i-1][j] = board[i-1][j]+1
                        board[i-1][j+1] = board[i-1][j+1]+1
                        board[i-1][j-1] = board[i-1][j-1]+1
                    end
                end
        end

我还试过移动上面的添加部分,作为if之下的elsif声明,就像这样

def create_hint_board(board)
    board = $globalarray
    $globalarray.each_index do |i|
        subarray = $globalarray[i]
            subarray.each_index do |j|
                if $globalarray[i][j] != '*'
                    board[i][j].to_i
                    board[i][j] = 0                 
                elsif board[i][j] == '*'
                    board[i+1][j] = board[i+1][j]+1
                    board[i+1][j+1] = board[i+1][j+1]+1
                    board[i+1][j-1] = board[i+1][j-1]+1
                    board[i][j-1] = board[i][j-1]+1
                    board[i][j+1] = board[i][j+1]+1
                    board[i-1][j] = board[i-1][j]+1
                    board[i-1][j+1] = board[i-1][j+1]+1
                    board[i-1][j-1] = board[i-1][j-1]+1
                end
            end
                puts "#{String(i)},#{String(j)} is #{board[i][j]}" 
    end
end

这会导致错误消息:

minesweeper.rb:28:in '+': can't convert Fixnum into String (TypeError)
   from minesweeper.rb:28:in 'block (2 levels) in create_hint_board'
   from minesweeper.rb:28:in 'each_index'
   from minesweeper.rb:28:in 'block in create_hint_board'
   from minesweeper.rb:28:in 'each_index'
   from minesweeper.rb:28:in 'create_hint_board'
   from minesweeper.rb:28:in '(main')

1 个答案:

答案 0 :(得分:1)

问题出在以下一行

subarray2 = board|i|

你在做:

board.each_index do |i|

在下一行中,您尝试在该索引处获取board的值。为了实现这个目标,你应该这样做:

subarray2 = board[i]

最后,使用each_with_index可以更好地实现此目的。

例如:

board.each_with_index do |v, i|
  subarray2 = v
  ...
end