2048

From Noisebridge
Revision as of 17:51, 27 March 2014 by R3pl1cant (talk | contribs) (go go ECMAScript)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page is a dissection of the massive time hole game otherwise known as 2048

Since this game has been remarkably successful at gobbling up time in a possibly pointless sort of way, lets see if we can take apart the mechanics of it as an example of fundamental game architecture and development.

http://gabrielecirulli.github.io/2048/

The documentation contained here in is unindented for the lulz and as a mechanism for learning and inquiry. The initial methodology will be based on reverse engineering the game play itself, and recreating the logic with original code. After this process there will likely be an analysis and comparison of the original code and further digging in to ideological differences, optimizations and possible alternatives.

Code samples are written in ECMA-262 edition 3

Layout[edit]

-----------------
| 2 | 2 |   |   |
-----------------
| 4 |   |   |   |
-----------------
|   |   |   |   |
-----------------
|   |   |   |   |
-----------------

The basic game layout is based on a 4x4 grid of tiles.

Logic[edit]

Start Game[edit]

The game starts with a simple randomization of two tiles placed on the board. The initial tiles have a value of either '2' or '4', and is heavily weighted towards a value of '2'.


Make Move[edit]

Moves are made by shifting tiles either left, right, up or down.

Rough code for shifting tiles left, does not include logic for "merging" matching tiles...

xAngle = -1;
for (var r:int = 0; r < 4; r++)
{
	for (var c:int = 1; c < 4; c++)
	{
		if (tileGrid.hasOwnProperty(c.toString() + r.toString()))
		{
			var xMin:int = 0;
			
			tileIndex = tileGrid[c.toString() + r.toString()];
			tile = tiles[tileIndex];
			
			for (var t:int = c - 1; t > -1; t--)
			{
				if (tileGrid.hasOwnProperty(t.toString() + r.toString()))
				{
					xMin = t + 1;
					break;
				}
			}
			
			tile.gridX = xMin;
			moveTile(tiles[tileIndex]);
		}
	}
}


End Game[edit]