lib/battlesnake/board.rb in battlesnake-0.1.5 vs lib/battlesnake/board.rb in battlesnake-0.1.7

- old
+ new

@@ -113,19 +113,23 @@ end ## # List reachable locations in each orthogonal direction. # + # @param [Location] location from which moving is desired. + # @options [Hash] + # max: max number of spaces to count before stopping search + # # @return [Hash] hash of reachable locations by direction - def flood_fills(location) + def flood_fills(location, options = {}) fills = Location::DIRECTIONS.map{ |direction| [direction, []]}.to_h available_directions(location).each do |direction| @flood_fill_checked = [] @flood_fill_matches = [] - fills[direction] = flood_fill(location.move(direction)) #.uniq(&:coords) + fills[direction] = flood_fill(location.move(direction), options) end fills end @@ -157,17 +161,18 @@ @paths.select{ |path| path.size == @shortest_path_size }.first end private - def flood_fill(location) + def flood_fill(location, options = {}) @flood_fill_checked << location.coords unless occupied?(location) @flood_fill_matches << location available_neighbors(location).each do |neighbor| - flood_fill(neighbor) unless @flood_fill_checked.include?(neighbor.coords) + return @flood_fill_matches if options[:max] && @flood_fill_matches.size >= options[:max] + flood_fill(neighbor, options) unless @flood_fill_checked.include?(neighbor.coords) end end @flood_fill_matches end \ No newline at end of file