flood fill (4 directions) algorithm w/o classes?

● ARCHIVED · READ-ONLY
Started by Napoleon 3 posts View original ↗
  1. I have a grid as follow (pseudo):

    @map_width = 100@grid = {}def set_tile(x, y, value)  @grid[y * @map_width + x] = valueend...def set_tile(50, 50, true)But because a tile is nothing but a coordinate (not a class) I can not use the typical recursive algorithm to flood fill tiles to true.

    Code:
    def flood_fill4(x, y)  # erm... yeah... what to put here...end...flood_fill4(50, 50)
  2. I don't understand what problem you are having.


    You have a grid, you have the dimensions, you have a starting coordinate where the fill begins...
  3. I can't use recursion without creating a new custom object for every tile that I check. Because I have a grid of bools and not a grid with classes.

    Update:

    Oh wait... I can just use a 2nd method... I'm so silly... I solved it.

    For whoever is interested, here is my (unoptimized) solution:

    Spoiler
    Code:
        def flood_fill4_recursive(x, y, width, matching_hash, new_hash, ignore_condition=false)      location_key = y * width + x      return if ($game_map.region_data[location_key] != matching_hash) && !ignore_condition      $game_map.region_data[location_key] = new_hash.clone            flood_fill4_recursive(x + 1, y, width, matching_hash, new_hash) if (x + 1) < width      flood_fill4_recursive(x - 1, y, width, matching_hash, new_hash) if x > 0      flood_fill4_recursive(x, y + 1, width, matching_hash, new_hash) if (y + 1) < $game_map.height      flood_fill4_recursive(x, y - 1, width, matching_hash, new_hash) if y > 0    end