28 July 2016

1010! is a simple puzzle game by Gram Games, originating from Turkey in August 2014. It feels like Tetris but without the time constraint and with more choices. It is available on Android and iOS.

Advices are available online on how to get a high score. I’m more interested in devising a decision strategy, automate it efficiently and optimize it through simulations… which also results in advices on how to get a high score, but more substanciated.

Teaser: below, learn how to get a score over 1 million every 8 games…

Game Presentation

1010! consists of a 10 by 10 grid on which variously-shaped pieces can be placed. When a row or column is full it is removed from the grid. At each round, 3 pieces are presented which must be fit on the grid, in any order . The game stops when no more pieces can be placed.

Points are attributed when putting a piece (the number of cells of the piece) and when rows or columns are removed (if r rows or columns are removed, add 5 r (r+1) to score).

There are 19 possible pieces ranging from 1 to 9 cells, with different drawing probabilities. There is talk about a special 5x5 square piece but I have never encountered it. Pieces are given a one character name for convenience:

  .: ■      -: ■ ■    i: ■        _: ■ ■ ■    I: ■      r: ■ ■    l: ■
                         ■                       ■         ■         ■ ■
  j:   ■    t: ■ ■                 O: ■ ■ ■      ■
     ■ ■         ■    h: ■ ■ ■ ■      ■ ■ ■          H: ■ ■ ■ ■ ■   V: ■
                                      ■ ■ ■   v: ■                     ■
  J:     ■   T: ■ ■ ■    R: ■ ■ ■                ■      L: ■           ■
         ■          ■       ■      o: ■ ■        ■         ■           ■
     ■ ■ ■          ■       ■         ■ ■        ■         ■ ■ ■       ■

The relative weighted drawing probabilities, as infered from manually collected statistics, seems to be:

  • 1 R T J L
  • 2 . r t j l h v H V O
  • 3 i - I _
  • 6 o

The total weight is 42. Piece o is drawn 6/42 = 1/7. There seems to be a bias on j which does not come out as often as it should in my iOS version. Also it would be nicer if O had the lowest probability.

Given these drawing probabilities, the 3 pieces on each round weight on average 11 cells: 3 * (1 * 5*4 + 2 * (1 + 3*4 + 4*2 + 5*2 + 9) + 3 * (2*2 + 3*2) + 6 * 4) / 42 = 11. The probability of the OOO event is (2/42)³, i.e. 1 every 9,261 rounds.

Game Strategy

The metrics I’m going to optimize is the number of rounds successfully completed, without taking into account the actual score. They are quite correlated anyway, as for steady-state the player must put and remove 11 cells per round on average, getting at least 22 points per round. Moreover, if this strategy succeeds in avoiding the end of the game, it should result in higher scores as well. However, it will not seek multi row/column removals to get bonus points at the price of taking any risks.

Decision criteria

I’ve considered 5 criteria:

  1. maximize free space in the grid. The rational is that the more free space the easier to fit the next pieces. However keeping only this criteria is highly ineffective because it does not consider the geometry of the grid being filled. The next four criteria aim at taking this into account.

  2. large fake piece can be placed. Try to keep space for an artificial 5x5 square piece used as a proxy for the various large real pieces.

  3. large real pieces can be placed. Same as above for 3 real large pieces: HOV.

  4. alignment maximization. The idea is to try to have filled cells in the grid aligned as much as possible so that by filling a few holes a row or column can be removed. A measure of alignments is to count for every row and column the number of filled cells, then to sum these numbers squared, so as to emphasize the longest alignments, and finally to normalize.

  5. surface minimization. The idea is to favor compact lumps of cells, preferably accumulating in corners and on borders, so as to let as much space elsewhere as possible, where large pieces may be put if necessary. A measure of compaction is the surface, which can be derived by counting the number of neighboring empty and filled cells, and then by normalizing. For instance, a lone cell on the inside has surface 4, but the same in a corner counts 2.

On top of this criteria it is possible to add a low priority preference to better scoring by adding the expected score increment with a very light weight, so that everything else being equal, a better scoring option is chosen. The effect on the score seems to be between 1 and 2 percent.

Complexity

When analyzing complexity, n = 10 is the grid size, g = 3 is the number of pieces at each round, p = 19 is the number of distinct pieces.

The number of possible grid layouts is about 2^{n²}. For each layout the number of unordered combination of pieces is about pᵍ/g!, minus a few combinations with repeated pieces. In order to evaluate the best choices for a round, one must first compute the possible final grid layout from the starting layout and the given pieces:

  • consider every piece order: g! = 6 (or less if a piece is duplicated in the round)
  • consider every possible position of the first piece:
    • look for fulfilled rows and columns: 2n n
  • consider every possible position of the second piece:
    • look for fulfilled rows and columns: 2n n
  • consider every possible position of the third piece:
    • look for fulfilled rows and columns: 2n n

Then on each final grid, evaluate the different criteria, which may involve:

  1. count free cells:
  2. check whether a large fake piece fits: worst case
  3. check whether 3 large real pieces fit: worst case 3n²
  4. count aligned cells on each row and column: 2n n
  5. count distinct adjacent empty/filled cells:

The overall worst-case complexity is in g! (n²)ᵍ n² per round, and this is reached sometimes when the grid is quite empty. However on average number of evaluations performed for each round is around 550,000, instead of g! (n²)ᵍ = 6,000,000. Taking into account the next round would append a pᵍ (n²)ᵍ n² factor, quite unpractical.

Simulations are going to help find which combination of these criteria is the most effective.

Efficient Implementation

As the complexity is quite bad, the simulation is written in low-level C based on bit field tricks for efficiency, and is parallelized with threads.

Bit fields

In order to reduce the practical complexity, bit fields can be used to represent the grid, so that some n or even operators can be performed with single bit instructions. Note that as there does not seem to be a standard 128-bit integer type, I really used two 64-bit integers. Some of the above operations may be implemented very efficiently with bit fields:

  • move a piece on the grid:
   // r rows & c columns
   piece >>> (10*r+c)
  • check that a piece fits in the grid:
   // intersection is empty
   (grid & piece) == 0
  • place a piece on the grid:
   // just keep both
   grid | piece
  • check full row or column in the grid:
   // to be evaluated for all 2n alignments
   (grid & alignment) == alignment
  • empty some cells from the grid:
   // keep every other
   grid & ~toempty
  • count bits: many algorithms (lookup table, Hamming weight) and a nice builtin implemented as one POPCNT instruction on some processors.
   // call compiler builtin
   __builtin_popcount(grid)
  • count aligned cells in a row or column:
   // to be evaluated for all 2n alignments
   count(grid & alignment)
  • compute surface requires to deal correctly with borders:
   // LEFT is first column, TOP is first row
   // shift & xor to detect differing neighbors
   count((((grid >> 1) & ~LEFT) | (grid & LEFT)) ^ grid) +
   count((((grid >> 10) & ~TOP) | (grid & TOP)) ^ grid))

In order to take best advantage of these tricks, special compiler options for the target hardware must be selected.

Parallelization

Exploring all orders and placements of three pieces at a round is highly parallel: all combinations must be checked and evaluated if legal.

However the operations are not very regular, as for one test the placement may fail at one level while for another it may fail at another level, so there is some irregularity which does not make it very suitable to GPGPU’s data-parallel SIMD model.

The implementation parallelize the exploration with the pthread library, first at the order level (6 combinations) and then the placement of the first piece can be further split. Each independent recursion evaluates its best ply and the final aggregation between threads is performed at the end, so as to avoid any interference between threads. Note that this parallelization is not very effective because each thread has not that much to do and the cost of creating and joining the threads must be amortized.

Experimental Results

Given these optimizations and enough cores, a simulation may proceed at a pace of more than 50 rounds per second. Different combined criteria have been evaluated on a set of 500 distinct random seeds for the number generator used to choose the pieces at each round with the weighted probabilities outlined above.

Each combined criteria is presented below by the weight given to each five criteria under the strategy column. Then statistics about the number of rounds are displayed, and finally the percentage of games which achieved a score over 1 million on those seeds.

strategyaveragestd deviationminimum1st quart.median3rd quart.maximumscore>1M
1002522,226.623,720.7495,67915,13930,126155,05913.8%
1001321,082.520,707.2436,39014,65328,491145,98312.2%
1001220,670.519,902.8495,56813,90529,676117,61312.8%
1002320,235.320,243.1645,39613,78828,483118,32011.2%
1000117,890.217,972.0434,86712,74424,38197,8629.2%
00001492.1499.361383156532,9230.0%
1000062.442.203451773400.0%
0010021.49.76142026610.0%
0000017.97.05131722520.0%
0001016.08.14101419660.0%

The figures are pretty consistent, and show that the 10025 criteria is the best among those tested. However, the first 4 best strategies do not pass a χ² test, that is they cannot be statistically distinguished. Futher testing with 10025 suggest that they are indeed equivalent and that the long term average is probably rather around 21,000. The p-value on 500 seeds with the fifth (10001) is 2.26%, which is not so great a distinction. However further testing showed that the difference is indeed significant, with a p-value under 1/10,000 for a few thousand tests.

Overall, the best strategy consist in considering the number of free cells (between 0 and 100), and then to distinguish equal results with the best lumpiest group of cells which display the more alignments, somehow.

Given the nature of a (long) run, which iterates around a steady-state (at each round on average as many cells are placed as are removed through full alignments), one would expect a geometrical/exponential distribution of the number of rounds, where the iterations can stop with a constant (although pretty small) probability all along the run. Indeed, the above figures are consistent with this view: standard deviation is roughly equal to mean, median over mean is about ln(2)

Conclusion

The best run on the tests presented above achieved a 3,548,228 score in 155,059 rounds (3 pieces placed per round) for random seed 4128567858 and weights 10025. The average number of filled cells is quite low 18.66 (standard deviation 6.24). The run came 11 times back to an empty grid.

Further random tests yielded a 4,851,264 score in 213,214 rounds for random seed 323067730 and weights 10025.

The overall best run seen during the various tests with a slightly altered decision function with weights 10025 but also with a small contribution for maximizing the score gave a 7,674,915 score in 332,674 rounds on this randomly generated sequence.

My personnal best by hand is 15,676 in probaby around 700 rounds… which took quite a long time.

If anyone wishes to tinker with the simulation, feel free to use this C code (license is GPLv3+). Also on my github. The documentation is the source code. The program can be used as an adviser by giving the pieces submitted, but playing like that takes a long time, and it is cheating, so what’s the point?

Note: Updated 2016-08-01, 19, 22, 23 for higher score, significance and better optimization criteria, links…

rule