class FloodFill < Array def initialize(data, options = {}) data.each_line { |s| self << s.strip.split(//) } @options = options end def fill(x, y, target_color, replacement_color) return unless self[y][x] # valid point? return if self[y][x] != target_color return if self[y][x] == replacement_color (dump; sleep(0.2)) if @options[:animation] self[y][x] = replacement_color fill(x+1, y, target_color, replacement_color) fill(x-1, y, target_color, replacement_color) fill(x, y+1, target_color, replacement_color) fill(x, y-1, target_color, replacement_color) end def dump each { |l| puts l.join } end end data = < true) puts " START: " b.dump b.fill(5,3, ' ', 'O') puts " DONE: " b.dump