Tetris

I found out that some work has gone into "Tetris randomizers," i.e., the algorithms that choose which Tetris piece you get next.

Simon Laroche wrote an interesting post on the subject:
https://simon.lc/the-history-of-tetris-randomizers

The idea is that you want the pieces to be random, but in such a way that you avoid floods (getting the same piece over and over) and droughts (not getting a particular piece for a long time).

I think I've come up with a pretty good way to do this.

It becomes obvious if you state the problem like this:
  • You want to avoid floods, i.e., you want the probability of choosing a recently-chosen piece to be decreased.
  • You want to avoid droughts, i.e., you want the probability of choosing an infrequently-chosen piece to be increased.

The Algorithm
  • Make an array of 7 integers (let's call it pieceProb) that will indicate the relative probability of choosing each piece.
  • Initialize each element of pieceProb to 10.
  • Every time you want to choose a piece, choose it randomly according to pieceProb. (This is easy, but if you have questions, please get in touch.)
  • Make sure you account for the possibility of pieceProb[n] being negative. (Treat it as 0 when choosing a piece.)
  • After you choose a piece p:
    • Add 1 to each element of pieceProb.
    • Subtract 7 from pieceProb[p].

This algorithm seems to perform well in my simulations.

An advantage of the algorithm is that it can be tuned by changing the initial value of pieceProb[1 ... 7]. A value of 1 will result in optimal flood and drought avoidance but also be very predictable. A value of a million will make it almost perfectly random. I think a value of 10 is a good place to start but I'm not a Tetris expert.

I'm making this algorithm public domain. If you want to use it, I would appreciate an email at tom.kerrigan@gmail.com.