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

Download the source code and start flooding

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers