1
0
mirror of https://github.com/danog/2048.git synced 2024-12-03 09:47:49 +01:00
2048/js/grid.js

118 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-03-22 16:24:11 +01:00
function Grid(size, previousState) {
2014-03-08 13:10:54 +01:00
this.size = size;
2014-03-22 16:24:11 +01:00
this.cells = previousState ? this.fromState(previousState) : this.empty();
2014-03-08 13:10:54 +01:00
}
// Build a grid of the specified size
2014-03-22 16:24:11 +01:00
Grid.prototype.empty = function () {
var cells = [];
2014-03-22 16:24:11 +01:00
2014-03-08 13:10:54 +01:00
for (var x = 0; x < this.size; x++) {
var row = cells[x] = [];
2014-03-08 13:10:54 +01:00
for (var y = 0; y < this.size; y++) {
row.push(null);
}
}
2014-03-22 16:24:11 +01:00
return cells;
};
2014-03-22 16:24:11 +01:00
Grid.prototype.fromState = function (state) {
var cells = [];
for (var x = 0; x < this.size; x++) {
var row = cells[x] = [];
2014-03-22 16:24:11 +01:00
for (var y = 0; y < this.size; y++) {
var tile = state[x][y];
row.push(tile ? new Tile(tile.position, tile.value) : null);
}
2014-03-22 16:24:11 +01:00
}
return cells;
2014-03-08 13:10:54 +01:00
};
// Find the first available random position
Grid.prototype.randomAvailableCell = function () {
var cells = this.availableCells();
if (cells.length) {
return cells[Math.floor(Math.random() * cells.length)];
}
};
Grid.prototype.availableCells = function () {
var cells = [];
2014-03-09 14:32:30 +01:00
this.eachCell(function (x, y, tile) {
if (!tile) {
cells.push({ x: x, y: y });
}
});
return cells;
};
// Call callback for every cell
Grid.prototype.eachCell = function (callback) {
2014-03-08 13:10:54 +01:00
for (var x = 0; x < this.size; x++) {
for (var y = 0; y < this.size; y++) {
2014-03-09 14:32:30 +01:00
callback(x, y, this.cells[x][y]);
2014-03-08 13:10:54 +01:00
}
}
2014-03-09 14:32:30 +01:00
};
2014-03-08 13:10:54 +01:00
2014-03-09 14:32:30 +01:00
// Check if there are any cells available
Grid.prototype.cellsAvailable = function () {
return !!this.availableCells().length;
2014-03-08 13:10:54 +01:00
};
// Check if the specified cell is taken
Grid.prototype.cellAvailable = function (cell) {
return !this.cellOccupied(cell);
};
Grid.prototype.cellOccupied = function (cell) {
2014-03-09 14:32:30 +01:00
return !!this.cellContent(cell);
};
Grid.prototype.cellContent = function (cell) {
2014-03-09 20:03:04 +01:00
if (this.withinBounds(cell)) {
return this.cells[cell.x][cell.y];
} else {
return null;
}
2014-03-08 13:10:54 +01:00
};
// Inserts a tile at its position
Grid.prototype.insertTile = function (tile) {
this.cells[tile.x][tile.y] = tile;
};
2014-03-09 20:03:04 +01:00
Grid.prototype.removeTile = function (tile) {
this.cells[tile.x][tile.y] = null;
};
Grid.prototype.withinBounds = function (position) {
return position.x >= 0 && position.x < this.size &&
position.y >= 0 && position.y < this.size;
};
2014-03-22 16:24:11 +01:00
Grid.prototype.serialize = function () {
var cellState = [];
2014-03-22 16:24:11 +01:00
for (var x = 0; x < this.size; x++) {
var row = cellState[x] = [];
for (var y = 0; y < this.size; y++) {
2014-03-22 16:24:11 +01:00
row.push(this.cells[x][y] ? this.cells[x][y].serialize() : null);
}
}
return {
2014-03-22 16:24:11 +01:00
size: this.size,
cells: cellState
};
};