My Tetris with AI Functionalities

(click on image to play tetris)

Tetris game was invented by a Russian scientist. The concept is very simple but effective – even today there are a lot of people enjoying this game.

I am not only interested in playing this game, but also interested in writing it (since I am a programmer 🙂 ). It has been my habit to implement Tetris games to practise GUI programming with a newly learned language. The earliest one was done on MS-DOS using C language (in an Borland IDE called Turbo C, by the way), I worked on some tricks like writing directly to Video-RAM to increase the performance, etc; then I did it using Visual Basic 6.0; then Visual C++ 6.0. Unfortunately all these versions are lost due to hardware upgrades.

Then in 2000, I joined a New York based company called Financial Sciences Company. There Java was chosen as the main language to work on their next generation trading applications. I knew nothing about Java at that time (I was accepted because of my C++ skills). So I learned it by reading ‘Core Java 2’ and ‘Professional Java Server Programming (J2EE edition)’, and practised and sharpened the skill by writing some programs in my spare time. This implementation of Tetris was the result of my study and practice of Java GUI programming using Swing. I think that it is the best one among my Tetris series.

Lately I don’t have the time to play with these Swing stuffs any more, so I just got some time to improved the algorithm in AI part to make it demo better. You can choose ‘X’ level to see the demo.

Features:

* Employing M-V-C pattern, game logic, data and the game screen are seperated, this makes the code very clean;
* The game panel is encapsulated as a component, so it can be dropped to any container conveniently using an IDE;
* Except the control buttons etc., it doesn’t use any control in Java API, instead, it draws directly to screen;
* It supports multimedia;
* It supports internationalization (i18n), with built in English, Chinese (simplified & traditional) and Japanese languages support;
* It has a very easy-to-use and intuitive user interface;
* It has an effective AI logic to demonstrate how to play this game.

About AI:

The AI part was implemented in following way:

The basic idea was to try to turn and put the coming block at each possible positions, evaluate the results, then choose the best one. But how to evaluate the result is the main problem.

First, we need to find which factors affect the final result, and get them out. For example, the height is definitely not a good thing (0 is the best); and holes under other blocks are evil, and so on. Then put these factors into a formula, with a set of coefficients, one for each of the factors. Now the remaining problem is to find the optimal vector of coefficients.

In order to do that, a training program is needed. I prefer the ‘quick-death’ way to screen out the bad ones as quickly as possible. So I just put it in a server and let it run during the day while I left home to work, and when I came back home at night, I got the result…

Click here to have fun with this Tetris game!