【发布时间】:2025-12-02 08:45:02
【问题描述】:
我正在尝试制作一个泛洪填充,从一个随机生成的数组的右上角开始请求用户输入,该数组填充了由“颜色”表示的数字 1-6。我刚刚添加了 oldColor/newColor 功能,但我收到错误消息,我不确定为什么。除此之外,该算法会继续要求输入,而不会在每个步骤中打印新的洪水填充的样子。
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)
我不能直接发布图片,但这是我的输出当前的样子,然后是失败消息 http://imgur.com/a/88UrK
【问题讨论】:
-
请阅读“minimal reproducible example”。我们需要演示问题的最少代码和输入数据,以及您的预期输出。你得到什么错误代码?此外,在 Ruby 中,我们使用 snake_case 作为变量名。 camelCaseIsTooHardToRead。
-
这个
oldColor = array_1毫无意义。为什么要丢弃参数并用图像副本替换它? -
我的想法是让 oldColors 包含原来存在的内容,然后让 newColors 负责填充。这行不通吗?我对 ruby 还很陌生,希望指出正确的方向以使其正常工作
标签: ruby recursion flood-fill