我試圖做一個洪水填充,要求用戶輸入從隨機生成的數組中填充數字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