81 Comments
- Chompy, on 10/12/2007, -2/+74You know I was just wondering "Just how *does* xpaladin feel about code challenges?", and now I have my answer.
- faulkner, on 10/12/2007, -3/+62i don't remember tetris letting you move pieces through other pieces.
- FreakTrap, on 10/12/2007, -8/+59Rofl...
<a href="www.pygame.org">PyGame (the drawing api)</a>
He may know python, but he fails at HTML - newtonapple, on 10/12/2007, -2/+48actually since python is "space sensitive" line count is actually not a bad metric.
- xample, on 10/12/2007, -7/+41burn
- klang, on 10/12/2007, -2/+35import sys,os,math,pygame,random,pprint
is fair.
import tetris
is not. - webmonk, on 10/12/2007, -1/+25Eh, every now and then they'll cause you to learn aspects of your particular language you wouldn't have otherwise. Not all bad...
- rtfx, on 10/12/2007, -2/+19Actually, he only used one "non-standard" library; pygame, which he would need to do a game with actual graphics(and not some kind of ghetto console scroll-to-redraw thing).
- MikeCerm, on 10/12/2007, -0/+17Tetris, because it is so simple and has definite rules that are known to everyone in the world, is a great thing to optimize. Optimizing Tetris is like trying to solve a Rubik's Cube: there are infinite ways to do it, but the trick is to find the simplest way.
- jfreeman, on 10/12/2007, -6/+20He sure used a lot of non-standard libraries. If you build up a large enough framework, you could do this in one line.
- Vektuz, on 10/12/2007, -0/+13unfortunately, python cares about whitespace.
It uses tabs and spaces (indentation) instead of braces and things, and you must seperate your lines. (One real statement per line).
This means that when someone writes a short, short program in python that does something big and crazy, its truly a good feat because they can't resort to C++ tricks of shortening things with #defines and putting everything on one line. - sHARD>>, on 10/12/2007, -12/+24This isn't in 100 lines of code. He used an external library that is not normally included in Python. That's cheating.
- ArmyOfFun, on 10/12/2007, -0/+11Download and install Python.
Download and install PyGame.
Save the source to a text file (call it tetris.py).
From a command line, call "[Python Install dir]/python.exe tetris.py" - nunofgs, on 10/12/2007, -5/+16It's called code recycling and if everyone did it, the software world would me much better off.
- kodek, on 10/12/2007, -0/+10Wrong. Spaghetti code is code that jumps all over the place ala GOTO.
- mapkinase, on 10/12/2007, -1/+11You know, if you gzip it, and cat in pieces by 80 symbols per line it will fit even 15 lines.
- ScrappyLaptop, on 10/12/2007, -1/+11@xpaladin:
Yes, it is just you. And maybe a few other grumpy people. Code challenges are the closest thing coding has to drag racing. Sometimes it goes a bit overboard, and of course the code is less maintainable, but the point is that it is a challenge of skill. - BigSassy, on 10/12/2007, -6/+14Tetris in 30 lines of code using C++ and DirectX:
http://www.gamedev.net/columns/hardcore/tetris/page2.asp - spliznork, on 10/12/2007, -1/+9That's not C++, it's plain C. And, it's only 30 lines as a mashed together text file -- appropriately indented it's closer to 140 lines or so. Neat, though.
- therealfoz, on 10/12/2007, -1/+8Forgive a non-programmer.. how does one go about playing/seeing this game?
- SuperJared, on 10/12/2007, -3/+10View the source: http://www.nikolajbaer.us/Files/tetris.html
- klang, on 10/12/2007, -1/+8The code that has to do with tetris certainly is 100 lines.
The libraries have to do with general stuff .. painting the windows, doing math and stuff like that .. libraries are fair game.
Obfuscating, like in the C++ example below is NOT fair game though (eventhough that's a quite small implementation of the game as well) - Virak, on 10/12/2007, -1/+8There *is* no article. The blog post is the original thing. There's the source code, but if you consider that an article, you're insane.
- MarkByers, on 10/12/2007, -1/+7Modifying the following four lines fixes the bug where pieces can move sideways through each other:
if keys[pygame.K_LEFT]:
if px > 0 and not collide_piece(piece,px-1,py): px-=1
if keys[pygame.K_RIGHT]:
if px+len(piece.split('n')[0]) < bw and not collide_piece(piece,px+1,py): px+=1 - Virak, on 10/12/2007, -1/+7It's physically and logically one single line. Multiple expressions != multiple lines.
- noamsml, on 10/12/2007, -1/+6Except for the fact that this is python, which uses linebreaks to signify new lines.
- niteskunk, on 10/12/2007, -8/+13I don't know Python, but I can do it in C++ pretty easily:
int main() {
niteskunk = new human;
while (niteskunk.alive()) {
niteskunk.school();
niteskunk.work();
niteskunk.jackOff();
sleep(300000);
}
return 0;
} - klang, on 10/12/2007, -1/+6that implementation is 600 lines..
- diecastbeatdown, on 10/12/2007, -1/+5File "./tetris.py", line 20
if px > 0: px-=1
^
SyntaxError: invalid syntax
---------------
using python 2.4.3 and pygame 1.6.2 - headzoo, on 10/12/2007, -3/+7It's got a couple bugs (lets you move a piece off the screen, and then throws a fit), but not too shabby for 100 lines of code.
- Kamaji, on 10/12/2007, -1/+5It's simple. Sure it won't be perfect.
I also don't recall tetris mandating a crash if you try to rotate pieces at the edge of the screen either. - diagonalfish, on 10/12/2007, -1/+4My God, it's full of obfuscation.
See the International Obfuscated C Code Contest (http://www.ioccc.org/) for more fine examples of eye-bending code. - MarkByers, on 10/12/2007, -0/+2Code here: http://markbyers.com/moinmoin/moin.cgi/SmallTetrisProgram
- Guspaz, on 10/12/2007, -0/+2Console apps mostly don't scroll to redraw. They move the cursor location, and then draw over, like you would expect. This can be done with DOS / windows commandline, Linux terminals, even telnet.
- ScrappyLaptop, on 10/12/2007, -4/+6...and then you end up with .NET. Which is not to say that that is always a bad thing, especially for speed of development, but it means that the feat must be measured against the libraries and functions available. Taken to the extreme, if there was a Tetris library in language xyz, a single line of code could suffice in the "program" written.
- diagonalfish, on 10/12/2007, -1/+3Tricky in Python though because of the requirements of its syntax. Things must be on new lines and indented lest they explode.
Definitely true of many other languages, though. Such as Perl, for example. You can put an entire 1000-statement program on one line and everything would work just the same as if it was beautifully laid out and commented. - Xhargh, on 10/12/2007, -3/+5How about tetris in 256 bytes instead?
http://www.256b.com/demo/477 - merreborn, on 10/12/2007, -1/+3You'll need the python interpreter.
http://www.python.org/download/
Then you just give it the .py file. - rynoon, on 10/12/2007, -5/+7I'm not sure why everybody is on xpalidin's case about this comment. I'm not a big fan of line-wise code challenges either. I'm sorry, but a coding challenge should not include the words "in under n lines". That's not how people code in their day-to-day lives and that's not how a challenge should be conducted. Want a challenge? Give them a set of conditions and a deadline. If you can create a working solution that meets all requirements within the deadline, you win.
This solution does not meet all of the requirements of a functional Tetris game. Therefore, it fails. - MarkByers, on 10/12/2007, -0/+2This version fixes most (all?) of the bugs in the program, and is just 44 lines with some obfuscation. I also remapped the keys to make it easier to play. I suspect that it can be made a lot shorter than this if you can be bothered ;). The original code can also be simplified and shortened without doing any obfuscating.
*Edit : removed: Digg messes up the code by removing spaces :( - shoover, on 10/12/2007, -0/+1Thanks for sharing, now go back to your own thread.
- lowerlogic, on 10/12/2007, -0/+1Have you even looked at the code? If you know even basic python syntax, you can tell what each line of code does. If you think that's messy, stay away from Perl and C.
- Scarblac, on 10/12/2007, -0/+1@ScrappyLaptop: to illustrate your point, consider the following elisp program that works on a default install of Emacs 21:
(tetris)
:-)
That said, I think PyGame is perfectly fair to use; it's standard as in _the_ Python cross-platform graphics library, and it'd be hard to do this sort of thing at all using only the standard library (without writing a C extension, they would use external libraries too). - klang, on 10/12/2007, -1/+2you are right... MemorySpace vs. CPUTime vs. Deadline vs. Brains is the eternal battle in programming .. linecounts matter, if you intend to write obfuscated code.
All in all, this specific piece of code is not unreadable, though I would have liked some more whitespace (not newlines) ..different strokes and all that.. - lowerlogic, on 10/12/2007, -0/+1I'm using the same version of python and it works fine. But when I do cat -n tetris.py that if statement is on line 19.
- sbrown123, on 10/12/2007, -1/+1@spliznork - I took a look at the code and ran it through a few rounds on my text editor. At first glance it appears to be actually less than 100 lines! I'm at work, can someone run this thing through AStyle and find out?
- JamesWilson, on 10/12/2007, -3/+3Get a python interpreter.
- drspin, on 10/12/2007, -0/+0Do you remember Apple ][ days? Beagle Bros. 2-liners? Full of peeks and pokes, and I remembered entering the 2 line applesoft BASIC code for a 'snake' game. Now, that was cool.
- yenta4shop, on 09/07/2008, -0/+0http://www.yenta4shop.co.uk/
http://astore.amazon.com/12.volt.battery.charger-2 ...
http://astore.amazon.com/5.gallon.water.bottle-20
http://astore.amazon.com/aerobed.raised-20
http://astore.amazon.com/bug.zapper-20
http://astore.amazon.com/flowtron.insect.killer-20
http://astore.amazon.com/furniture.chaise.lounge-2 ...
http://astore.amazon.com/inflatable.bed-20
http://astore.amazon.com/steam.cleaner.mop-20 - Hexxagonal, on 10/12/2007, -1/+1i was just going to post this link from gamedev good eye, i remember playing around with it back when it was released
-
Show 51 - 79 of 79 discussions



What is Digg?
Browsing Digg on your phone just got easier with our enhancements to the