我试图做一个洪水填充,要求用户输入从随机生成的数组中填充数字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
请阅读“[mcve]”。我们需要显示问题的最小代码和输入数据,以及您的预期输出。你得到什么错误代码?另外,在Ruby中,我们使用snake_case作为变量名称。 camelCaseIsTooHardToRead。 –
这个'oldColor = array_1'没有任何意义。你为什么放弃这个论点,并用图片的副本来替代它? – Max
我的思考过程是让oldColors包含最初那里的内容,然后让newColors承担填充责任。这会不会奏效?我对ruby相当陌生,并希望指出正确的方向来实现这个工作 – LeeKay220