0

I'm trying to make a flood-fill that asks for user input starting from the top right corner of a randomly generated array filled with numbers 1-6 that are represented by 'colours'. I added the oldColor/newColor feature just now and I get error message and I'm not exactly sure why. Other then that, the algorithm keeps going on asking for input without printing what the new flood-fill looks like in each step.

    def floodfill(array_1, row, column, colours, oldColor, newColor)
          #colours is an array of the 6 colours i'm going to be using
          boxHeight = array_1.length
          boxWeight = array_1[0].length
          oldColor = array_1
          #puts oldColor
          print "> "
          newColor = gets.chomp.downcase

          if array_1[row][column] != oldColor
            return
            if newColor == "r"
              newColor = colours[:red]
              array_1[row][column] = newColor
              floodfill(array_1, row + 1, column, colours, newColor) # right
              floodfill(array_1, row - 1, column, colours, newColor) # left
              floodfill(array_1, row, column + 1, colours, newColor) # down
              floodfill(array_1, row, column - 1, colours, newColor)# up
              print_it
            else
              puts "didnt get that"
              array_1.each do |row|
                row.each do |c|
                  print c
                end
              puts
            end
          end
        end
      end
floodfill(array_1,14,9,colours,0,0)

I cant directly post images, but here is what my output currently looks like and then the failure message https://i.stack.imgur.com/BzdVB.jpg

LeeKay220
  • 11
  • 1
  • Please read "[mcve]". We need the minimal code and input data that demonstrates the problem, along with your expected output. What error code do you get? Also, in Ruby we use snake_case for variable names. camelCaseIsTooHardToRead. – the Tin Man Dec 13 '16 at 22:33
  • This `oldColor = array_1` makes no sense. Why are you discarding the argument and replacing it with a copy of the image? – Max Dec 14 '16 at 13:47
  • My thought process was to have the oldColors contain what was originally there then to have newColors have the fill responsibility. Will this not work out? I'm fairly new to ruby and would like to be pointed in the right direction to get this working – LeeKay220 Dec 15 '16 at 16:36

2 Answers2

1

This short-circuits your code execution:

if array_1[row][column] != oldColor
  return

Once it hits return it returns nil from the method and nothing else will be evaluated.

boxHeight and boxWeight are never initialized and newColor gets overwritten by the gets which probably shouldn't happen.

And finally, the code is missing a trailing end. I'd recommend using a tool to reformat or reindent your code automatically, which will really help avoid such problems.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Can you explain further? How would I go about correcting it? I havent gotten around to implementing boxHeight and boxWeight because I first wanted to make sure the recursive fill function works for at least one of the colours – LeeKay220 Dec 14 '16 at 06:07
0

Ruby if statements don't work like in C or Java where you can write something like

if array_1[row][column] != oldColor
  return

You either need an end or you need to put the if after the return.

if array_1[row][column] != oldColor
  return
end
# or
return if array_1[row][column] != oldColor
Max
  • 21,123
  • 5
  • 49
  • 71