Complete Bejeweled game in less than 2KB

Read all posts about "" game

You should know Bejeweled from PopCap Games.

It’s a game which have been played millions, if not billions, of times. Now PopCap just released the 3rd version of his blockbuster.

To celebrate this event, just like I made when I coded a complete Flash Sokoban game in less than 2KB, I made a Bejeweled version in less than 2KB. The swf file is 2035 bytes, less than the 2048 need to make 2KB.

Do you think it does not have any feature? Take a look:

* colorful representation of all 7 kinds of jewel
* select a jewel clicking on it
* swap jewels only if they make a chain
* combo chains recognizing
* falling jewels animation
* complex (let’s say “not that basic”) score system
* hint on a possible move when clicking outside the game area

Everything in less than 2KB. The code was mode with the only purpose of making the game fit in less than 2KB, there’s not readability nor optimization.

Here it is the source:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.text.TextField;
	public class n extends Sprite {
		private var jw:Array=new Array();
		private var g:Sprite;
		private var s:Sprite=new Sprite();
		private var pr:int=-10;
		private var pc:int=-10;
		private var cl:Array=new Array(0xFF0000,0xFF00,0xFF,0XFFFF00,0xFF00FF,0xFFFF,0xFFFFFF);
		private var cp:Boolean=false;
		private var ts:TextField=new TextField();
		private var th:TextField=new TextField();
		private var sc:uint=0;
		private var m:uint=0;
		public function n() {
			addChild(ts);
			ts.textColor=0xFFFFFF;
			ts.x=500;
			addChild(th);
			th.textColor=0xFFFFFF;
			th.x=550;
			for (var i:uint=0; i<8; i++) {
				jw[i]=new Array();
				for (var j:uint=0; j<8; j++) {
					do {
						jw[i][j]=Math.floor(Math.random()*7);
					} while (rk(i,j)>2||ck(i,j)>2);
					g=new Sprite();
					g.graphics.beginFill(cl[jw[i][j]]);
					g.graphics.drawCircle(30,30,29);
					g.graphics.endFill();
					g.name=i+"_"+j;
					g.x=j*60;
					g.y=i*60;
					addChild(g);
				}
			}
			addChild(s);
			s.graphics.lineStyle(2,0xff0000,1);
			s.graphics.drawRect(0,0,60,60);
			s.visible=false;
			stage.addEventListener(MouseEvent.CLICK,ci);
			addEventListener(Event.ENTER_FRAME,ef);
		}
		private function ef(e:Event):void {
			var f:Boolean=false;
			for (var i:int=6; i>=0; i--) {
				for (var j:uint=0; j<8; j++) {
					if (jw[i][j]!=-1&&jw[i+1][j]==-1) {
						f=true;
						jw[i+1][j]=jw[i][j];
						jw[i][j]=-1;
						getChildByName(i+"_"+j).y+=60;
						getChildByName(i+"_"+j).name=(i+1)+"_"+j;
						break;
					}
				}
				if (f) {
					break;
				}
			}
			if (! f) {
				var h:Boolean=false;
				for (i=7; i>=0; i--) {
					for (j=0; j<8; j++) {
						if (jw[i][j]==-1) {
							h=true;
							jw[0][j]=Math.floor(Math.random()*7);
							g=new Sprite();
							g.graphics.beginFill(cl[jw[0][j]]);
							g.graphics.drawCircle(30,30,29);
							g.graphics.endFill();
							g.name="0_"+j;
							g.x=j*60;
							g.y=0;
							addChild(g);
							break;
						}
					}
					if (h) {
						break;
					}
				}
				if (! h) {
					var r:Boolean=false;
					for (i=7; i>=0; i--) {
						for (j=0; j<8; j++) {
							if (rk(i,j)>2||ck(i,j)>2) {
								r=true;
								var tr:Array=[i+"_"+j];
								var u:uint=jw[i][j];
								var t:int;
								if (rk(i,j)>2) {
									t=j;
									while (chk(u,i,t-1)) {
										t--;
										tr.push(i+"_"+t);
									}
									t=j;
									while (chk(u,i,t+1)) {
										t++;
										tr.push(i+"_"+t);
									}
								}
								if (ck(i,j)>2) {
									t=i;
									while (chk(u,t-1,j)) {
										t--;
										tr.push(t+"_"+j);
									}
									t=i;
									while (chk(u,t+1,j)) {
										t++;
										tr.push(t+"_"+j);
									}
								}
								for (i=0; i0&&mouseY<480&&mouseY>0) {
					var sr:uint=Math.floor(mouseY/60);
					var sc:uint=Math.floor(mouseX/60);
					if (!(((sr==pr+1||sr==pr-1)&&sc==pc)||((sc==pc+1||sc==pc-1)&&sr==pr))) {
						pr=sr;
						pc=sc;
						s.x=60*pc;
						s.y=60*pr;
						s.visible=true;
					} else {
						swp(pr,pc,sr,sc);
						if (rk(pr,pc)>2||ck(pr,pc)>2||rk(sr,sc)>2||ck(sr,sc)>2) {
							th.text="";
							cp=false;
							getChildByName(pr+"_"+pc).x=sc*60;
							getChildByName(pr+"_"+pc).y=sr*60;
							getChildByName(pr+"_"+pc).name="t";
							getChildByName(sr+"_"+sc).x=pc*60;
							getChildByName(sr+"_"+sc).y=pr*60;
							getChildByName(sr+"_"+sc).name=pr+"_"+pc;
							getChildByName("t").name=sr+"_"+sc;
						} else {
							swp(pr,pc,sr,sc);
						}
						pr=-10;
						pc=-10;
						s.visible=false;
					}
				} else {
					for (var i:uint=0; i<8; i++) {
						for (var j:uint=0; j<8; j++) {
							if (i<7) {
								swp(i,j,i+1,j);
								if ((rk(i,j)>2||ck(i,j)>2||rk(i+1,j)>2||ck(i+1,j)>2)) {
									th.text = i.toString()+","+j.toString()+"->"+(i+1).toString()+","+j.toString();
								}
								swp(i,j,i+1,j);
							}
							if (j<7) {
								swp(i,j,i,j+1);
								if ((rk(i,j)>2||ck(i,j)>2||rk(i,j+1)>2||ck(i,j+1)>2) ) {
									th.text = i.toString()+","+j.toString()+"->"+(i).toString()+","+(j+1).toString();
								}
								swp(i,j,i,j+1);
							}
						}
					}
				}
			}
		}

		private function swp(r1:uint,c1:uint,r2:uint,c2:uint):void {
			var t:uint=jw[r1][c1];
			jw[r1][c1]=jw[r2][c2];
			jw[r2][c2]=t;
		}
		private function rk(r:uint,c:uint):uint {
			var u:uint=jw[r];
			var stk:uint=1;
			var t:int=c;
			while (chk(u,r,t-1)) {
				t--;
				stk++;
			}
			t=c;
			while (chk(u,r,t+1)) {
				t++;
				stk++;
			}
			return (stk);
		}
		private function ck(r:uint,c:uint):uint {
			var u:uint=jw[r];
			var stk:uint=1;
			var t:int=r;
			while (chk(u,t-1,c)) {
				t--;
				stk++;
			}
			t=r;
			while (chk(u,t+1,c)) {
				t++;
				stk++;
			}
			return (stk);
		}
		private function chk(g:uint,r:int,c:int):Boolean {
			if (jw[r]==null) {
				return false;
			}
			if (jw[r]==null) {
				return false;
			}
			return g==jw[r];
		}
	}
}

This class was compiled with “Compress movie” checked and “Include XMP metadata” unchecked. There’s still room to save some bytes, but not enough to develop power jewels I am afraid… anyway you are free to try to reduce the code some more.

Download the source code. What’s your best score?

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

215 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
// Stairs
// 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