Function floodTiles

Returns a range that iterates through tiles based on a flood filling algorithm.

Prototype

auto floodTiles(alias pred, T)(
  T grid,
  RowCol origin,
  Diagonals diags = Diagonals.no
)
if (is(typeof(pred(grid.tileAt(RowCol(0, 0)))) : bool));

Parameters

NameDescription
pred predicate that returns true if the flood should progress through a given tile.
grid grid to apply flood to.
origin coordinate at which to begin flood.
diags by default, flood only progresses to directly adjacent tiles. Diagonals.yes causes the flood to progress across diagonals too.

Example

import std.array;
import std.algorithm : equal;

// let the 'X's represent 'walls', and the other letters 'open' areas we'd link to identify
auto grid = rectGrid([
  // 0    1    2    3    4    5 <-col| row
  [ 'X', 'X', 'X', 'X', 'X', 'X' ], // 0
  [ 'X', 'a', 'a', 'X', 'b', 'X' ], // 1
  [ 'X', 'a', 'a', 'X', 'b', 'X' ], // 2
  [ 'X', 'X', 'X', 'X', 'X', 'c' ], // 3
  [ 'd', 'd', 'd', 'X', 'c', 'X' ], // 4
  [ 'd', 'd', 'd', 'X', 'X', 'X' ], // 5
]);

// starting on a wall should return an empty result
assert(grid.floodTiles!(x => x == 'a')(RowCol(0,0)).empty);
assert(grid.floodTiles!(x => x == 'a')(RowCol(3,3)).empty);

// flood the 'a' room
assert(grid.floodTiles!(x => x == 'a')(RowCol(1,1)).equal(['a', 'a', 'a', 'a']));
assert(grid.floodTiles!(x => x == 'a')(RowCol(1,2)).equal(['a', 'a', 'a', 'a']));
assert(grid.floodTiles!(x => x == 'a')(RowCol(2,1)).equal(['a', 'a', 'a', 'a']));
assert(grid.floodTiles!(x => x == 'a')(RowCol(2,2)).equal(['a', 'a', 'a', 'a']));

// flood the 'a' room, but asking for a 'b'
assert(grid.floodTiles!(x => x == 'b')(RowCol(2,2)).empty);

// flood the 'b' room
assert(grid.floodTiles!(x => x == 'b')(RowCol(1,4)).equal(['b', 'b']));

// flood the 'c' room
assert(grid.floodTiles!(x => x == 'c')(RowCol(4,4)).equal(['c']));

// flood the 'd' room
assert(grid.floodTiles!(x => x == 'd')(RowCol(4,1)).equal(['d', 'd', 'd', 'd', 'd', 'd']));

// flood the 'b' and 'c' rooms, moving through diagonals
assert(grid.floodTiles!(x => x == 'b' || x == 'c')(RowCol(4,4), Diagonals.yes)
    .equal(['c', 'c', 'b', 'b']));

Authors

Copyright

License