/// <summary> /// Adds a tile to one of the empty cells on the grid. If no cells are empty, /// this method throws InvalidOperationException. /// </summary> /// <remarks> /// The tile's position is chosen uniformly at random from the set of empty /// cells. The tile's value is chosen with a weighted coin flip. /// </remarks> /// <returns>the tile that was added</returns> public Tile AddRandomTile() { if (IsFull) { throw new InvalidOperationException("There are no empty cells to place a tile in."); } var emptyCells = new List <GridCell>(GetEmptyCells()); GridCell cell = RandomProvider.Select(emptyCells); int number = RandomProvider.FlipCoin(Constants.TILE_PROBABILITY_2) ? 2 : 4; var tile = new Tile(cell, number); AddTile(tile); return(tile); }