In a recent section, the site has argued that providing an effective demo mode for computer games is not usually a trivial task. This section describes the development of a program yielding the automatic mode for the computer game of Tetris.
Tetris rules are as follows: - The pit is 8 blocks wide by 19 blocks deep. (An internet search showed there doesn't seem to be a standard size.)
- There are seven falling shapes, made of 4 blocks each, randomly chosen. Shapes can be moved to the left, the right, or rotated in increments of 90 degrees.
- Ten entire rows of blocks must be filled to win a game, complete rows disappear by shifting everything above downwards. The game is lost if the well is overfilled.
The early warning of the next shape to fall is standard, but ignored in this implementation- otherwise the program becomes more complicated and much slower. This choise will hopefully be justified by the end of the section.
The program assumes the falling shapes are chosen randomly and attempts to find the best location and orientation for the specific falling shape. As the number of different possibilities is under two hundred, it performs an exhaustive search. On the other side, another program could select the falling shapes so as to hinder (or even help) the player.
The following objectives have been identified: - for as long as possible, reject moves which overfill the pit, immediately losing the game
- favour low shape placements
- promote placements which leave no gaps underneath
- give credit for moves which fill entire rows
While the first goal is obvious, the balance between the remaining targets is not: The first version, for example, did not (explicitly) promote filling entire rows of blocks. Even this, remarkably, still wins over 60% of games! This percentage, though, is not acceptable, and the second try expressly favoured the formation of complete rows.
Statistics are only as good as the random sample size; a program which has to provide a display is no good for fast statistics. Another version was written which replaces BoxFill and (get colour of) Point statements by writing to and reading from an array. (Each screen block corresponds to one element of the array). On a 1.8 GHz processor, this version plays 1,000 games in a couple of minutes. Here are the results:
Games won | 91% |
Average Score, games won | 10.3 |
Average Score, games lost | 7 |
Average Bonus, games won | 12 |
The 10.3 rows figure probably needs to be explained, if you have never played Tetris before: Additional rows of blocks are completed sometimes (up to a total of 13), on the verge of winning the game. The success rate of 91% is probably good enough for an initial presentation- but the site was sure it can be improved: A program should certainly switch tactics when the well is almost full. Some additional aims could be :
- penalise placements which shall make the gaps further below difficult to remove
- promote placements which increase the chance of a next good move, whatever the next falling shape
However, these are second order effects. Of the games lost, most were traced to the 4-block long shape appearing too late after it was needed. The single modification which would yield a significant improvement in the success rate was quite clear: Its last, the site expects, version of the Tetris demo mode program penalises troughs which are one block wide and more than four blocks deep. These are the improved results:
Games won | 96% |
Average Score, games won | 10.2 |
Average Score, games lost | 6 |
Average Bonus, games won | 11 |
All figures are subject to the randomness of falling shapes. The average 'low puzzle' bonus deteriorated slightly, as expected, since placing emphasis on filling entire rows and penalising deep gaps can be considered somewhat 'short-term'. At that rate of success, though, the site called it a day.
The critique of results is as follows: The preview of the next falling shape, though easy to program in manual mode, was omitted in the interests of a fair comparison. The human player is obviously given more time to manoeuver, but conditions were otherwise identical. The site is not a seasoned Tetris player (or experienced gameplayer at that), but liberally admits being beaten by its own creation! My average low-puzzle bonus is marginaly higher at 13 rows, but I lose 4 games long before I have played one hundred!
Here is a demonstration. Code for this program had not been shown earlier, for want of more descriptive variable names: The initial-letter variable names improve the speed of poor typists, but are no good when everybody else reads the program. The source is given now, but there is still a warning: The program includes too many numerical constants, (which the site does not normally like at all). More work is needed in the display procedure to reduce flicker.
Demo mode versions for invaders and snapper have been started by the site. They have almost, but not quite, won a game yet, so there is no point in showing them. For invaders, at least, it has been demonstrated to the site that far bettter results are possible.