This question is about the popular game 2048. I am wondering about the details of the appearance of new tiles at each turn:
https://github.com/gabrielecirulli/2048/blob/master/js/game_manager.js#L72
// Adds a tile in a random position
GameManager.prototype.addRandomTile = function () {
if (this.grid.cellsAvailable()) {
var value = Math.random() < 0.9 ? 2 : 4;
var tile = new Tile(this.grid.randomAvailableCell(), value);
this.grid.insertTile(tile);
}
};
https://github.com/gabrielecirulli/2048/blob/master/js/grid.js#L36
// 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)];
}
};