# Flash flood fill implementation

Do you know what is the flood fill algorithm?

From Wikipedia: Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array. It is used in the “bucket” fill tool of paint programs to determine which parts of a bitmap to fill with color, and in puzzle games such as Minesweeper, Puyo Puyo, Lumines, and Magical Drop for determining which pieces are cleared.

So now you know there is an algorithm to manage some issues in “click somewhere and…” games.

Let’s make it!

My version of the algorithm looks for all light grey tiles (two-dimensional array elements) which are connected to the starting light grey tile (the one we click) by a path of same tiles, and changes them to pink.

It’s a ricorsive algorithm acting this way: if the clicked tile is grey, then turn it into pink, then act as we clicked the tile at the top of the clicked tile, the one at the bottom, the one at the left and the one and the right. This is called 4-directions flood fill, because we extend the flooding to four directions (up, down, left, right).

You can also perform a 8-directions flood fill, extending the flooding to the original four directions plus the four diagonals

Look at this actionscript:

```fill_map = new Array();
fill_map[0] = [1, 1, 1, 1, 1, 1, 1, 1, 1];
fill_map[1] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
fill_map[2] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
fill_map[3] = [1, 0, 0, 1, 0, 0, 0, 0, 1];
fill_map[4] = [1, 1, 1, 0, 0, 0, 1, 1, 1];
fill_map[5] = [1, 0, 0, 0, 0, 1, 0, 0, 1];
fill_map[6] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
fill_map[7] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
fill_map[8] = [1, 1, 1, 1, 1, 1, 1, 1, 1];
for (y=0; y<9; y++) {
for (x=0; x<9; x++) {
tile = _root.attachMovie("tile", "tile_"+(y+x*9), _root.getNextHighestDepth(), {_x:y*50, _y:x*50});
tile.gotoAndStop(1+fill_map[x][y]);
}
}
_root.attachMovie("cursor", "cursor", _root.getNextHighestDepth());
cursor.onEnterFrame = function() {
this._x = 50*Math.floor(_root._xmouse/50);
this._y = 50*Math.floor(_root._ymouse/50);
};
_root.onMouseDown = function() {
pos_x = Math.floor(_root._xmouse/50);
pos_y = Math.floor(_root._ymouse/50);
flood_fill(pos_x, pos_y);
};
function flood_fill(x, y) {
pos = x+y*9;
if (fill_map[y][x] == 0) {
fill_map[y][x] = 2;
_root["tile_"+(x+y*9)].gotoAndStop(3);
flood_fill(x+1,y);
flood_fill(x-1,y);
flood_fill(x,y+1);
flood_fill(x,y-1);
}
}```

The 4-directions flood function goes from line 27 to line 37 and allows me to paint grey areas this way:

As you can see, you can paint only one "room" at time because the diagonal "walls" does not allow the four direction flood to paint everywhere.

Look at the 8-directions flood:

```fill_map = new Array();
fill_map[0] = [1, 1, 1, 1, 1, 1, 1, 1, 1];
fill_map[1] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
fill_map[2] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
fill_map[3] = [1, 0, 0, 1, 0, 0, 0, 0, 1];
fill_map[4] = [1, 1, 1, 0, 0, 0, 1, 1, 1];
fill_map[5] = [1, 0, 0, 0, 0, 1, 0, 0, 1];
fill_map[6] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
fill_map[7] = [1, 0, 0, 0, 1, 0, 0, 0, 1];
fill_map[8] = [1, 1, 1, 1, 1, 1, 1, 1, 1];
for (y=0; y<9; y++) {
for (x=0; x<9; x++) {
tile = _root.attachMovie("tile", "tile_"+(y+x*9), _root.getNextHighestDepth(), {_x:y*50, _y:x*50});
tile.gotoAndStop(1+fill_map[x][y]);
}
}
_root.attachMovie("cursor", "cursor", _root.getNextHighestDepth());
cursor.onEnterFrame = function() {
this._x = 50*Math.floor(_root._xmouse/50);
this._y = 50*Math.floor(_root._ymouse/50);
};
_root.onMouseDown = function() {
pos_x = Math.floor(_root._xmouse/50);
pos_y = Math.floor(_root._ymouse/50);
flood_fill(pos_x, pos_y);
};
function flood_fill(x, y) {
pos = x+y*9;
if (fill_map[y][x] == 0) {
fill_map[y][x] = 2;
_root["tile_"+(x+y*9)].gotoAndStop(3);
flood_fill(x+1,y);
flood_fill(x+1,y+1);
flood_fill(x+1,y-1);
flood_fill(x-1,y);
flood_fill(x-1,y+1);
flood_fill(x-1,y-1);
flood_fill(x,y+1);
flood_fill(x,y-1);
}
}```

Now you can paint everywhere

What's better? The one that fits your needs, of course?

What kind of game can we make out of that? I don't know (lie! lie!), it's up to you

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 10000000
// 2 Cars
// 2048
// Avoider
// Ballz
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Breakout
// Bricks
// Columns
// CubesOut
// Dots
// DROP'd
// Dudeski
// Eskiv
// Filler
// Fling
// Globe
// HookPod
// Hundreds
// InkTd
// Iromeku
// Lumines
// Magick
// MagOrMin
// Maze
// Memdot
// Nano War
// Nodes
// o:anquan
// Ononmin
// Pacco
// Phyballs
// Platform
// Poker
// Pool
// Poux
// Pudi
// qomp
// Racing
// Renju
// SameGame
// Security
// Sling
// Slingy
// Sokoban
// Splitter
// Sproing
// Stack
// Stairs
// Stringy
// Sudoku
// Tetris
// Threes
// Toony
// Turn
// TwinSpin
// vvvvvv
// Wordle
// Worms
// Yanga
// Zhed
// zNumbers