【问题标题】:Can't convert Fixnum into array (TypeError) Ruby无法将 Fixnum 转换为数组(TypeError)Ruby
【发布时间】:2015-04-08 13:12:55
【问题描述】:

我收到了错误:

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)'

在尝试检查二维数组的值时,并将与该索引位置相邻的所有单元格加 1。错误发生在 subarray2 = board|i|。我正在尝试遍历整个二维数组

整个代码是

    #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')

【问题讨论】:

  • 你的意思是board[i]?你所拥有的,board |i| 没有任何意义。
  • 就是这样!谢谢!

标签: ruby arrays loops


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    相关资源
    最近更新 更多