Java Tetris With AI

The Killer App that Demonstrates that Java isn't Completely Useless

The Java VM is now using megabytes of RAM on your machine

Watch the computer player use dubious piece placement and leave holes everywhere!

Java Tetris With AI

Ok, so maybe "Java Tetris With AI" is not the catchiest name I could possibly come up with. But that's okay - because the "Java Tetris With AI" program is not about glamour - it's about delicious content. This program will allow you to play a regular game of tetris on any platform with a Java VM, pretty sweet, no? Maybe it's not. Tetris is a quintessential programming project and every hack out there has written his own copy. Many of them are better-looking than the screen shot above. The difference between those programs and this one is that my tetris game will play itself - it's got built-in, and potentially extensible, AI that will play tetris. For tetris buffs, it also has "Malice Mode" and "Happy Mode" that, when enabled, will either try to screw you up by giving you the worst possible blocks at the worst time, or will toss you a bone every now and then when you need it. Give it a whirl.

- August 2002

A Note on the Source Code

First off, I should put in a little disclaimer about the authorship of this code. This was an assignment that I did as a freshman last year for CS108, Stanford's object-oriented programming class. We were provided with a skeleton of interfaces and utility functions and instructed to complete the project. So a good deal of credit (I daresay all) as far as architecting the structure of the code belongs to my professor, Nick Parlante, and the rest of the CS108 staff. That being said, I wrote perhaps 80-90% of the working code, so I feel justified in claiming some credit too :-).

In order to run this program, you'll need a Java compiler and an implementation of the SWING (GUI stuff) library for your platform. I believe that this is available at Sun's website, provided that they're still in business. If you have problems getting this stuff set up, please don't ask me. I hate the brain-damaged way in which Java programs have to be set up just so before they will deign to compile. Hearing about your plight will only ruin my day. Complain to Sun instead, providing that they're still in business. JTetris is the main class for the game, though some of the other classes have test mains. This program is a lot better than some of my others for learning from because the source is rather well commented. Good luck and have fun!

NOTE: If you are a Stanford student taking CS108 or an equivalent class wherein programming a tetris game is a class requirement, DO NOT download this code, even to just peruse it. Not only is it a violation of the honor code, but the CS department runs all submitted code through tools that are very good at finding programs that are even semantically similar to previously submitted material. It's dumb. Don't do it.


Code Download

Java Tetris Source Code



Return to Shedletsky's Code Library

Return to the Nexus