0

I'm getting the error:

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

when attempting to check a 2D array for a value, and adding 1 to all cells adjacent to that index location. The error occurs at subarray2 = board|i|. I'm trying to iterate over the entire 2D array

The entire code is

    #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

The area giving me trouble is

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

I've also tried moving the addition section above, as an elsif statement below the if, like so

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

This results in the error message:

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
  • 3
    Do you mean board[i]? What you have, board |i| doesn't really make any sense. Commented Apr 8, 2015 at 13:16

1 Answer 1

1

The issue is at following line

subarray2 = board|i|

You are doing:

board.each_index do |i|

And in following line you are trying to get the value of board at that index. To achive this you should do:

subarray2 = board[i]

At last, there is a better way to achieve this by using each_with_index.

Eg:

board.each_with_index do |v, i|
  subarray2 = v
  ...
end
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.