Search code examples
2d-gamesprobability-density

What is the probability distribution of the new tiles in 2048?


This question is about the popular game 2048. I am wondering about the details of the appearance of new tiles at each turn:

  • Is it 50% twos and 50% fours?
  • Is the probability of appearance evenly distributed on all free slots?

Solution

  • 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)];
      }
    };