HTML5 prototype of iOS “o:anquan” puzzle game powered with Phaser – finished prototype with flood fill implementation

Read all posts about "" game
A couple of days ago I published the HTML5 prototype of iOS “o:anquan” puzzle game powered with Phaser and despite being not complete because I did not include flood fill algorithm, the post got an enormous success with almost 6K views only in its first day. I planned to release the version featuring flood fill next week, but as a blogger I also have to give people what they are asking for, so here is the finished prototype with flood fill implementation to remove tiles after a move. Let me jump straight to the point and show you the final prototype:
Select a number and drag it around free tiles to decrease it leaving a trail of “ones”, at the end of each mode a flood fill algorithm is applied to each new tile and will remove all matching tiles. Flood fill is quite easy to add, here you can see a Flash and HTML5 version in action. It took me less than 30 lines to add the flood fill, and some of them was to do some kind of indexOf method using pointInArray function because indexOf standard javascript method does not work with Phaser Point class. Now the concept is: 1 – Place three random numbers (explained in the first part) 2 – Make a move (same as above) 3 – Remove matching number using flood fill 4 – Back to point 1 I highlighted the new lines of code, still no comments because I did not have the time, but I am sure you will appreciate it.
var game;

var gameOptions = {
	gameWidth: 800, 
	gameHeight: 1400, 
	tileSize: 120, 
	fieldSize: {
          rows: 6,
          cols: 6
     },
     fallSpeed: 250,
     diagonal: false,
     colors: [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff]
}

window.onload = function() {	
	game = new Phaser.Game(gameOptions.gameWidth, gameOptions.gameHeight);
     game.state.add("TheGame", TheGame);
     game.state.start("TheGame");
}

var TheGame = function(){};

TheGame.prototype = {
     preload: function(){
          game.stage.backgroundColor = 0x222222;
          game.load.spritesheet("tiles", "assets/sprites/tiles.png", gameOptions.tileSize, gameOptions.tileSize); 
     },
  	create: function(){
          game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
		game.scale.pageAlignHorizontally = true;
		game.scale.pageAlignVertically = true;   
  		this.createLevel();
          game.input.onDown.add(this.pickTile, this);
  	}, 
	createLevel: function(){
          this.tilesArray = [];
		this.tileGroup = game.add.group();
          this.tileGroup.x = (game.width - gameOptions.tileSize * gameOptions.fieldSize.cols) / 2;
          this.tileGroup.y = (game.height -  gameOptions.tileSize * gameOptions.fieldSize.rows) / 2;
  		for(var i = 0; i < gameOptions.fieldSize.rows; i++){
               this.tilesArray[i] = [];
			for(var j = 0; j < gameOptions.fieldSize.cols; j++){
				this.addTile(i, j);
			}
		}
          this.placeNumbers();
	},
     placeNumbers: function(){
          var emptySpots = [];
          for(var i = 0; i < gameOptions.fieldSize.rows; i++){
			for(var j = 0; j < gameOptions.fieldSize.cols; j++){
				if(this.tilesArray[i][j].value == 0){
                         emptySpots.push(this.tilesArray[i][j].coordinate);
                    }
			}
		} 
          for(i = 0; i < 3; i++){
               var item = Phaser.ArrayUtils.removeRandomItem(emptySpots);
               if(item){
                    var randomValue = game.rnd.integerInRange(2, 9);
                    this.tilesArray[item.y][item.x].value = randomValue;
                    this.tilesArray[item.y][item.x].frame = randomValue;
               }     
          }   
     },
	addTile: function(row, col){
		var tileXPos = col * gameOptions.tileSize + gameOptions.tileSize / 2;
		var tileYPos = row * gameOptions.tileSize + gameOptions.tileSize / 2;
		var theTile = game.add.sprite(tileXPos, tileYPos, "tiles");
		theTile.anchor.set(0.5);
          theTile.value = 0;
          theTile.picked = false;
          theTile.coordinate = new Phaser.Point(col, row);
          this.tilesArray[row][col] = theTile;
	     this.tileGroup.add(theTile);	
	},
     pickTile: function(e){
          this.visitedTiles = [];
          this.visitedTiles.length = 0;
          if(this.tileGroup.getBounds().contains(e.position.x, e.position.y)){
               var col = Math.floor((e.position.x - this.tileGroup.x) / gameOptions.tileSize);
               var row = Math.floor((e.position.y - this.tileGroup.y) / gameOptions.tileSize);
               if(this.tilesArray[row][col].value &gt; 0){
                    this.tilesArray[row][col].alpha = 0.5;  
                    game.input.onDown.remove(this.pickTile, this);
	     	     game.input.onUp.add(this.releaseTile, this);
                    game.input.addMoveCallback(this.moveTile, this);   
                    this.visitedTiles.push(this.tilesArray[row][col].coordinate);
               }
          } 
     },
     moveTile: function(e){
          if(this.tileGroup.getBounds().contains(e.position.x, e.position.y)){
               var col = Math.floor((e.position.x - this.tileGroup.x) / gameOptions.tileSize);
               var row = Math.floor((e.position.y - this.tileGroup.y) / gameOptions.tileSize);
               if(row != this.visitedTiles[this.visitedTiles.length - 1].y || col != this.visitedTiles[this.visitedTiles.length - 1].x){
                    var distance = new Phaser.Point(e.position.x - this.tileGroup.x, e.position.y - this.tileGroup.y).distance(this.tilesArray[row][col]);
                    if(distance < gameOptions.tileSize * 0.4){
                         var previousTileValue = this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].value;
                         if(!this.tilesArray[row][col].picked &amp;&amp; this.checkAdjacent(new Phaser.Point(col, row), this.visitedTiles[this.visitedTiles.length - 1]) &amp;&amp; previousTileValue &gt; 1 &amp;&amp; this.tilesArray[row][col].value == 0){
                              this.tilesArray[row][col].picked = true;
                              this.tilesArray[row][col].alpha = 0.5;
                              this.tilesArray[row][col].value = previousTileValue - 1;
                              this.tilesArray[row][col].frame = previousTileValue - 1;
                              this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].value = 1;
                              this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].frame = 1;
                              this.visitedTiles.push(this.tilesArray[row][col].coordinate);
                         }                         
                         else{
                              if(this.visitedTiles.length &gt; 1 &amp;&amp; row == this.visitedTiles[this.visitedTiles.length - 2].y &amp;&amp; col == this.visitedTiles[this.visitedTiles.length - 2].x){
                                   this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].value = 0;
                                   this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].frame = 0;
                                   this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].picked = false;
                                   this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].alpha = 1;
                                   this.visitedTiles.pop();
                                   this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].value = previousTileValue + 1;
                                   this.tilesArray[this.visitedTiles[this.visitedTiles.length - 1].y][this.visitedTiles[this.visitedTiles.length - 1].x].frame = previousTileValue + 1;
                              }
                         }
                    }
               }
          }
     },
     releaseTile: function(){
          game.input.onUp.remove(this.releaseTile, this);
		game.input.deleteMoveCallback(this.moveTile, this);
          game.input.onDown.add(this.pickTile, this);   
          for(var i = 0; i < this.visitedTiles.length; i++){
			this.tilesArray[this.visitedTiles[i].y][this.visitedTiles[i].x].picked = false;
               this.tilesArray[this.visitedTiles[i].y][this.visitedTiles[i].x].alpha = 1;       
		}
          if(this.visitedTiles.length &gt; 1){
               for(i = 0; i < this.visitedTiles.length; i++){
                    this.filled = [];
                    this.filled.length = 0;
                    this.floodFill(this.visitedTiles[i], this.tilesArray[this.visitedTiles[i].y][this.visitedTiles[i].x].value);
                    if(this.filled.length &gt; 1){
                         for(var j = 0; j < this.filled.length; j++){                              
                              this.tilesArray[this.filled[j].y][this.filled[j].x].value = 0;
                              this.tilesArray[this.filled[j].y][this.filled[j].x].frame = 0;
                         }
                    }
               }
               this.placeNumbers();
          }
     },
     checkAdjacent: function(p1, p2){
          if(gameOptions.diagonal){
               return (Math.abs(p1.x - p2.x) <= 1) &amp;&amp; (Math.abs(p1.y - p2.y) <= 1);
          }
          else{
               return (Math.abs(p1.x - p2.x) == 1 &amp;&amp; p1.y - p2.y == 0) || (Math.abs(p1.y - p2.y) == 1 &amp;&amp; p1.x - p2.x == 0);   
          }
     },
     floodFill: function(p, n){
          if(p.x < 0 || p.y < 0 || p.x &gt;= gameOptions.fieldSize.cols || p.y &gt;= gameOptions.fieldSize.rows){
               return;
          }
          if(this.tilesArray[p.y][p.x].value == n &amp;&amp; !this.pointInArray(p)){
               this.filled.push(p);
               this.floodFill(new Phaser.Point(p.x + 1, p.y), n);
               this.floodFill(new Phaser.Point(p.x - 1, p.y), n); 
               this.floodFill(new Phaser.Point(p.x, p.y + 1), n);
               this.floodFill(new Phaser.Point(p.x, p.y - 1), n);       
          }
     },
     pointInArray: function(p){
          for(var i = 0; i < this.filled.length; i++){
               if(this.filled[i].x == p.x &amp;&amp; this.filled[i].y == p.y){
                    return true;
               }
          }
          return false;
     }
}
Now let’s see if you can create something interesting out of it, download the source code.

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