Skip to content

Instantly share code, notes, and snippets.

@rymate1234
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rymate1234/9880737 to your computer and use it in GitHub Desktop.
Save rymate1234/9880737 to your computer and use it in GitHub Desktop.
function Ai() {
this.init = function() {
// This method is called when AI is first initialized.
}
this.restart = function() {
// This method is called when the game is reset.
}
this.step = function(grid) {
// This method is called on every update.
// Return one of these integers to move tiles on the grid:
// 0: up, 1: right, 2: down, 3: left
// Parameter grid contains current state of the game as Tile objects stored in grid.cells.
// Top left corner is at grid.cells[0][0], top right: grid.cells[3][0], bottom left: grid.cells[0][3], bottom right: grid.cells[3][3].
// Tile objects have .value property which contains the value of the tile. If top left corner has tile with 2, grid.cells[0][0].value == 2.
// Array will contain null if there is no tile in the slot (e.g. grid.cells[0][3] == null if bottom left corner doesn't have a tile).
// Grid has 2 useful helper methods:
// .copy() - creates a copy of the grid and returns it.
// .move(dir) - can be used to determine what is the next state of the grid going to be if moved to that direction.
// This changes the state of the grid object, so you should probably copy() the grid before using this.
// Naturally the modified state doesn't contain information about new tiles.
// Method returns true if you can move to that direction, false otherwise.
// sample AI:
var x = Math.floor((Math.random()*4));
console.log(x);
return Math.floor(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment