Slashdot is powered by your submissions, so send in your scoop


Forgot your password?
Chrome Classic Games (Games) Programming Games

Javascript Game of Tron In 226 Bytes 115

Have you upgraded your hardware to play something beefier than 140-byte Tetris? New submitter alokmenghrajani writes with "a detailed view of how we size-optimized a game of Tron to just 226 bytes." It's also optimized for Chrome, and very fast.
This discussion has been archived. No new comments can be posted.

Javascript Game of Tron In 226 Bytes

Comments Filter:
  • by TheGoodNamesWereGone ( 1844118 ) on Sunday March 25, 2012 @12:00PM (#39466525)
    1KB Chess for the Sinclair still has that beat.
    • by lobiusmoop ( 305328 ) on Sunday March 25, 2012 @12:21PM (#39466701) Homepage

      Actually it was only 672 bytes - the 1024 byte memory had to include the screen memory also, much like shared video memory today - could take up to 768 of the 1024 bytes for a full 32x24 screen! (the chess game only used an 11-line screen for the board etc)

      And it's even considered by some to be the greatest program ever written. []

      • Re: (Score:3, Interesting)

        by Anonymous Coward

        And what about the 20 MB browser and all other cram below it? I can do a 3D FPS in 0 lines of code by just inserting a DVD into my PS3! Yay for me!

        • by mcgrew ( 92797 ) *

          Uh, mods, the AC was going for "funny". You might want to think before you moderate. See how well that FPS runs if you take the memory out of the computer.

          He does have a good point about the 20 mb browser. A web browser shouldn't take near that much memory. I don't remember the first browser I used, but it came on a single floppy.

      • by mcgrew ( 92797 ) *

        I had that, and it was indeed impressive -- and not just the programming for the game itself. I learned a few tricks studying its code.

        Your linked article, though -- the computer was buggy? A hack and a kludge, perhaps, but impressive kludges and hacks. The only flaws I saw in it were the poorly fitting expansion modules (not needed for the chess game) and the membrane keyboard. But fuggy? What was buggy about it?

        I wrote a two player battle tanks game for it in a couple hundred bytes of machine code, but it

    • There is a Befunge program for chess which is 1.5KB, and plays legally at least. Proof either of the great ingenuity of the human race or of the fact that some people have way too much time on their hands.
    • Just tried it, it works fine in Firefox 13...

    • ie6 also was written within 1k. the thing is, nobody seems surprised.
    • Re: (Score:3, Insightful)

      by hairyfeet ( 841228 )
      Not to mention this is NOT "Tron" this is just a variation on slither. These don't even look like light cycles they are just dots. Lame. And since when do we not count all the overhead of the browser? I'm sure you could get a 5Kb checkers game by using all the existing code in ubuntu or Win 7 but we wouldn't say that it was really a 5Kb checkers game would we?
  • by x0d ( 2506794 )
    It's Chrome-only.
    • by cynyr ( 703126 )

      I'm running chrome ( Version 19.0.1068.0 (0) ) and i get this nice fast black square....

      • So I don't know weather to be pissed that it is not standards based and working in Firefox, or pissed because it is slashdotted...
      • by Anonymous Coward

        working fine on 19.0.1074.0
        press j k i l

      • Apparently they don't consider "how to play the game" as part of the game. Hit i,j,k,l (well any key really)
    • by Anonymous Coward

      Works on Firefox 11 just fine.

    • No it's not. It works fine under firefox. I'd write down what version I'm running, but I can't type that fast.
      You can also look at the code, there's nothing non-standard in it.

    • Re:Damn. (Score:5, Informative)

      by TheRaven64 ( 641858 ) on Sunday March 25, 2012 @12:14PM (#39466635) Journal
      Works fine in Safari. That said, if you press a key other than ijkl it does something random. I assumed wasd would work, and was very confused until I read the instructions. It's also single player, so you basically drive around until you hit your own wall - there's no way of winning. It's more a drawing program with a crappy UI than a game. Making it only 226 bytes of source isn't that impressive, it's basically:
      • Change direction if key is pressed
      • Test if pixel next in that direction is white, if so exit
      • If not, set it to white, set current location to that address
      • Repeat.

      I wouldn't be surprised if you could write the same game in under 226 bytes of Z80 or 6502 assembly, so doing it in a high-level language seems much less impressive.

      • Re: (Score:1, Interesting)

        by Anonymous Coward

        Under 226? instead of being a critical asshole, that "someone" who sits outside every event and just points, please post your code that does just that. Or, give people a fucking break and realize that pushing boundaries like this is what science, and computer science, is all about. Take off your cynical sunglasses and watch the world become so much more clear. Or not.

        • by tibit ( 1762298 )

          1. Computer science has got zilch to do with computers. It's a field of mathematics.

          2. This "tron" program does not push the boundaries of any science.

          3. Neither is it some a software engineering feat.

          It's a hack with some neatness to it, that's about it.

      • Works fine in Safari. That said, if you press a key other than ijkl it does something random. I assumed wasd would work, and was very confused until I read the instructions.

        I use Vimperator, you insensitive clod! Even ijkl don't work for me, and my fingers kept trying hjkl anyway!

    • Works fine for me in Firefox 12.0
    • by x0d ( 2506794 )
      oh, my mistake. I saw that chrome logo, tried it in opera and it didn't work. I'm also too lazy to open safari just for that.
      • Saw the Chrome logo, so I tried it in Opera. I'm too lazy to open Safari, so I just assumed it didn't work in Firefox. Sounds logical.
      • by Ksevio ( 865461 )
        Well it works fine in Opera 12.
        • by bvimo ( 780026 )

          and Opera 11.61 (although it is a little fast).

          Version: 11.61
          Build: 1250
          Platform: Linux
          System: x86_64, 3.0.0-16-generic

    • by Luckyo ( 1726890 )

      Firefox 3.6.x (latest 3.6 version) runs it fine.

      Note that code itself suggests that game will not start until you press one of the four control buttons.

    • by antdude ( 79039 )

      It worked OK in my old Mozilla's SeaMonkey v2.0.14 web browser. ;)

  • by Anonymous Coward

    keyup instead of keydown makes the game play like junk. Much more apparently unresponsive.

  • by Anonymous Coward

    Tried it in ie9. It simply doesn't work.

    • Re:portability (Score:5, Interesting)

      by hobarrera ( 2008506 ) on Sunday March 25, 2012 @12:14PM (#39466633) Homepage

      It works on chrome and firefox. This isn't a portability issue: normally developers add huge amounts of code to support IE. If it needs tweaking for IE, it would probably be 50% larger.

      • If it needs tweaking for IE, it would probably be 50% larger.

        The program uses an HTML5 canvas. So it would need a lot more than "tweaking" to work in IE. It would need a completely different implementation.

        But IE 10.0 is supposed to (finally) have canvases.

      • Actually, does NOT work in my firefox.

        But who cares. It just shows that it is highly and amazingly dependent upon the many megabytes of code that it sits on top of. It needs a javascript engine and a browser. You may as well add a byte code that invokes a game of Tron and claim it only needs 1 byte.

        • That's odd. Maybe you should report this to the dev, with your FF version, platform, etc. I played quite a while with FF and it worked fine. The controls are i,j,k,l, and you *may* need to click the black canvas to focus it.

  • I'd love to have another 226 bytes explaining the controls. They don't seem to make much sense...

    • Re: (Score:3, Informative)

      by Anonymous Coward

      IJKL instead of WASD or arrow keys. It saves space because IJKL is actually in alphabetical order, which makes it a neat modulus away from being the four directions.

  • 2 player? (Score:5, Informative)

    by Ambitwistor ( 1041236 ) on Sunday March 25, 2012 @12:25PM (#39466735)

    It's not really a game of Tron without a competing lightcycle. (Without fruit, it's not really a game of Snake either.)

  • by readandburn ( 825014 ) on Sunday March 25, 2012 @12:31PM (#39466785)
    It should be called "Drawing-A-Line-In-A-Tiny-Black-Square-With-Terrible-Controls".

    Come to think of it, that might make a better movie than that last Tron.

  • if you keep spamming i,j,k and l, your score keeps going up.

    also, why doesn't it work in firefox??

  • by zAPPzAPP ( 1207370 ) on Sunday March 25, 2012 @12:34PM (#39466827)

    226 byte which I am sure include library calls, only runs on top of the multi megabyte of browser code, may use various other resources as well.

    By this standard, I can write Tron in 1 bit.


    Call it the "Run Tron Bit". It runs on top of a full impletation of Tron.

    • Re:This again... (Score:4, Interesting)

      by sco08y ( 615665 ) on Sunday March 25, 2012 @01:17PM (#39467145)

      226 byte which I am sure include library calls, only runs on top of the multi megabyte of browser code, may use various other resources as well.

      By this standard, I can write Tron in 1 bit.


      Call it the "Run Tron Bit". It runs on top of a full impletation of Tron.

      Anything that has a display of some sort has library calls to control that display. If you were writing this on an old C-64, it had a comprehensive sprite library that performed collision detection and perfectly synchronized updates, without you doing more than setting a few registers carefully. If you were playing 6502 machine code golf, you could probably write this in half the bytes. Switching to a graphics mode was (IIRC) a single assignment, clearing the screen was a function provided by the BASIC interpreter, and polling for key events was a simple call. Updating the display, naturally, involved writing to a predesignated area of memory. You could even switch back to text mode to print out GAME OVER when you were done.

      In this example, the library calls, which are deliberately designed to be fairly verbose in order to promote readability, chew up much of their byte budget with verbose keywords like onkeyup, getContext, fillRect, etc.

      Also, since you're dealing with complex libraries that provide lots features, code has to turn off some of those features. In this case, they actually generate javascript errors and ignore them. They then have to go through handling key events, handling movement and such, and all with some very cleverly compressed techniques. For instance, they grab the mod of the keyvalue and use that as an index to an array of values to update the player's direction.

      • by mcgrew ( 92797 ) *

        Anything that has a display of some sort has library calls to control that display

        Well, IIRC the Sinclair had a 2k rom. That would have to include the display as well as the BASIC language, keyboard detection, read/write to the cassette, etc. The tanks game I wrote for it I wrote in hand-assembled machine code was only a couple hundred bytes, not counting the display code in ROM. It read the keyboard directly, so both players could hold their keys down at the same time without conflicts. I would imagine tha

    • by Ksevio ( 865461 )
      The challenge was to write the game in standard javascript - and he did that. It wasn't to include the graphics display drivers (not all that interesting a problem these days). It's not like he included a jquery library (or anything really). Just 221 bytes (in the latest version) that you can post into an html file and run on your computer (no need for internet access either!)
    • by Menkhaf ( 627996 )


      Hey, that's no one bit. That's a UTF-16 character, which means it is (or rather, may be) a full 32 bits! Back to the drawing board, young lad.

    • It runs on top of a full impletation of Tron.

      Fucking bloat!

  • Our old low-bit graphics games used to do something or have some goal...
  • Even Shorter (Score:3, Interesting)

    by BrokenCube ( 896491 ) on Sunday March 25, 2012 @12:55PM (#39467001) Homepage
    The current version of the code (on the linked site) is only 221 bytes long - and I've had a quick play with it and it can be reduced further to 219 bytes by swapping out the boolean ANDs "&&" for bitwise ANDs "&" twice.
    • your are right, thanks! I'll update the code & give you credits.
      • your are right, thanks! I'll update the code & give you credits.

        i meant to write you are right. Thanks!

        • your are right, thanks! I'll update the code & give you credits.

          i meant to write you are right. Thanks!

          Seems someone else got there first, as it's been attributed to "skrounge" - oh well :(

  • by Juippi ( 416370 ) on Sunday March 25, 2012 @01:34PM (#39467271)
    This was fun, because I got pretty much the same thing down to 56 bytes in x86 assembly some 15 years ago. I remember the best entry in the competition I wrote it for being around 48 bytes or so; I missed at least one trick for setting the graphics segment more efficiently, and also something related to either collision handling or keyboard input, don't remember which.
    In any case, this [] is possibly the right version of the code. Should compile with NASM, and is even playable in Dosbox with arrow keys if you turn the emulation speed as low as it can go.
    • by PCM2 ( 4486 )

      56 bytes of assembly language source code? I highly doubt it.

      • by Mr Z ( 6791 )

        There's only 56 bytes that the computer has to read from somewhere to have a runnable program. In the case of Javascript, since there's no compiled bytecode, you have to download and run from the source. For assembly language, I think it's fair to measure the binary that results from assembling, since it's pretty much a 1:1 translation at that level.

        Under DOS, I believe you could actually key the whole program's machine code in with Alt-Keypad sequences using "copy con:" as long as your program did

    • Are you referring to hugi ( ? I believe the x86 size optimization people were doing in the 80s and 90s required a different mindset. You needed to really understand the OS environment & memory layout (e.g. putting DOS in graphics mode using a single byte). When optimizing javascript you focus on the javascript language (e.g. what happens when you do things like undefined xor 1). But overall you can't really compare assembly to javascript. Some things in jav
  • by davidbrit2 ( 775091 ) on Sunday March 25, 2012 @01:43PM (#39467355) Homepage
    How many megabytes of supporting code are necessary to run those 226 bytes?
    • Yes, this is exactly my point too. As an limiting argument, let's say my window manager had some-sort of graphics interface with more bells and whistles than emacs. "ooo look! i wrote a script that resulted in a full Donkeykong game in less than 32 characters!!" of course, that was one of the features of the fancy fictitious graphics interface that there are a number of example games that could be started with the equivalent of "run('donkkong')"
    • Yeah, seriously. In Python it would be just 11 bytes, 'import tron'.

    • by toriver ( 11308 )

      If you cook a meal, do you say that you and the store clerk and the transport driver and the farmer and the butcher and the miller and the service technician at the electricity company cooked the meal?

      • I don't expect them to engineer and construct the computer system from scratch, but claiming it's 226 bytes and ignoring the mountains of libraries and APIs being relied upon throughout the OS and web browser is a bit like ordering from McDonald's and saying you "made dinner".
    • If you're going that far, then how many more lines of code were required to write the kernel of your operating system?

      You've just stuck your head down a rabbit hole, and the rabbit ripped your head off.

      • Invalid straw-man exception.

        I don't think the byte size is important for "golf" purposes unless you count everything down to the bare metal. If they can do Tron in 226 bytes of x86 machine code, then I'll take note. Otherwise the size is only meaningful from a practical standpoint, e.g. how much more disk space and RAM will be necessary to use this program.

        I can put "" in autoexec.bat, but that doesn't mean I've implemented Windows 3.1 in 7 bytes.

        • No straw-man here. You're original comment was just all inclusive. All you said was "supporting code [...] necessary to run". So you would have to include all supporting code. If you had excluded OS level instructions, then I wouldn't have had a problem. Because if Tron had been written in Java, you would also have to include all the source for the JVM. Now, a C level program that compiled down to machine code, then cool. But you didn't specify the scope of "supporting code".

  • by Anonymous Coward

    1) It's not really the game, something that kind of maybe looks sort of like the game
    2) doesn't really even have the right functionality
    3) is only a small bit of logic


    Tron in 7 bytes in javascript.

  • not tron (Score:5, Funny)

    by eyenot ( 102141 ) <> on Sunday March 25, 2012 @02:52PM (#39467923) Homepage

    I think there's a difference between Tron and Suicidal Etch-A-Sketch

  • by bartoku ( 922448 ) on Sunday March 25, 2012 @03:26PM (#39468133)
    Now it is like I am playing in Matrix time and I rock!
  • I was super excited when I found out someone had done this for tron... Until I realized it was actually the old qbasic nibbles or worm game. This is neat and all, but its not tron. Its not even the tron light cycle game since you play by yourself. Oh well.
  • ... 226 bytes is cool and all, but is useless to me since it doesn't work on my platform. I would gladly accept it taking, say, even 15,000 bytes if it actually worked. It's not as if I can't afford the RAM.

  • I started with a TRS-80 III, cassette drive storage. There was a magazine,
    "80 Micro" which held a contest, who could write the best one line program.

    There was a trick to get more characters (an extra 10 or so) in one line I forget
    now the total length but think around 120 characters (bytes) on one line.

    This was done in Basic using Peeks and Pokes to keep them short.

    Lot's of entries; most being this Java Tron cycle type, be it Tron, skiing downhill
    or racing; just something controllable going forwards
  • Has all of /. gone stupid?
    Sure it's a lame name for the "project", and it's barely playable, and doesn't work in all browsers... but is a freaking awesome piece of code and all I read here is childish whining. Go back to facebook.

    What happened to /.?

Lavish spending can be disastrous. Don't buy any lavishes for a while.