gabrielecirulli/2048 — pull request #155 by giodamelio (2014-04-15 01:41:20+00:00)
Title: Add DOM Event based api

A simple interface to allow commands to be sent to the game via javascript events. This is the upgraded version of #152.

**Story:** I was trying to make a userscript that would automate 2048. There didn't seem to be any easy way to control the game short of sending keystrokes(cross-browser support is a pain), so I added this.

To send an event, simple run any of the following code samples from a script on the same page(userscript/bookmarklet).

```javascript
// Move the tiles
// You can send "up"/"right"/"down"/"left"
var event = new CustomEvent("doMove", { "detail": { "direction": "right"} })
document.dispatchEvent(event);

// Restart the game
var event = new CustomEvent("doRestart")
document.dispatchEvent(event);

// Get notified when a move has taken place
document.addEventListener("move", function (event) {
console.log("Game moved: ", event.detail.direction);
});

// Get notified when the game is restarted
document.addEventListener("restart", function (event) {
console.log("Game restarted");
});

// Get notified when the player keeps playing after they get a 2048
document.addEventListener("keepPlaying", function (event) {
console.log("Player kept playing after 2048");
});

// Get notified when the game is over
document.addEventListener("gameOver", function (event) {
console.log("Game over");
});
```