{"id":7410,"date":"2010-03-08T12:52:04","date_gmt":"2010-03-08T17:52:04","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=7410"},"modified":"2010-03-08T13:53:48","modified_gmt":"2010-03-08T18:53:48","slug":"my-version-of-tetris","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=7410","title":{"rendered":"My Version of Tetris"},"content":{"rendered":"<p>In 1990 I was given my first IBM PC as a graduation gift.  The machine came with a copy of Borland Turbo C, which was a programming environment.  I didn&#8217;t know the C programming language at the time.  Before then, I&#8217;d been using Basic.  I realized that Basic wasn&#8217;t going to cut it in the professional world, and it was time for me to learn a grownup programming language. <\/p>\n<p>The machine came with the Borland reference books.  You could use these to look up individual functions and remember their syntax.  And that&#8217;s all. They contained no real instruction on the structure and behavior of the language itself. Learning C with these books was like trying to learn English using only an English dictionary, or learning to drive using only the owner&#8217;s manual for your car.<\/p>\n<p>This was also pre-internet (for me) so I couldn&#8217;t look up anything online.  I didn&#8217;t even <em>know<\/em> any C programmers.  I&#8217;d graduated and it was summer, so I no longer had any teachers to ask.  Looking back, I suppose a trip to the library might have been worth a try, although that didn&#8217;t occur to me and I&#8217;m not sure if they would have kept any relevant books on hand. <\/p>\n<p>So my only way forward was to sit in my bedroom and try to puzzle out the language based on my own guesses and whatever truths could be gleaned from the three-line code snippets scattered throughout the book.<\/p>\n<p>So I wound up with some gaps in my knowledge. Specifically, I didn&#8217;t know you could make your own functions.  This is a hilarious thing to not know. A C program might look like this:<\/p>\n<pre><code>\r\nvoid DoAwesomeThing ()\r\n{\r\n     be awesome here;     \r\n}\r\n&nbsp;\r\nvoid DoNeatThing ()\r\n{\r\n     do some stuff;\r\n     DoAwesomeThing ();\r\n}\r\n&nbsp;\r\nvoid main ()\r\n{\r\n     do some stuff;\r\n     DoNeatThing ()\r\n     do some more stuff;\r\n     DoNeatThing ()\r\n     do still more stuff;\r\n     DoNeatThing ()\r\n}\r\n<\/code><\/pre>\n<p>But if you don&#8217;t use functions &#8211; if you can&#8217;t (or don&#8217;t know how to) make DoAwesomeThing or DoNeatThing &#8211; then the program will turn into this:<\/p>\n<pre><code>\r\nvoid main ()\r\n{\r\n     do some stuff;\r\n     {\r\n          do some stuff;\r\n          {\r\n               be awesome here;     \r\n          }\r\n     }\r\n     do some more stuff;\r\n     {\r\n          do some stuff;\r\n          {\r\n               be awesome here;     \r\n          }\r\n     }\r\n     do still more stuff;\r\n     {\r\n          do some stuff;\r\n          {\r\n               be awesome here;     \r\n          }\r\n     }\r\n}\r\n<\/code><\/pre>\n<p>If you find a bug in awesome thing, then you need to fix it in three different places.  Copy &#038; paste errors will propagate and the code gradually becomes an unreadable mess.  Doing things this way basically defeats the most important features of the language.  (I did things this way because I was trying to map my knowledge to C to my knowledge of Basic.)<\/p>\n<p>Most programmers will look at the above code and get all twitchy.  It&#8217;s just so perfectly <em>wrong<\/em>.<\/p>\n<p>Never one to settle for small, reasonable goals, I decided that the best project after <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hello_World\">Hello World<\/a> was a full-featured graphical version of Tetris. <\/p>\n<p>I wrote Tetris without a single function call.  The entirety of the program was contained within main ().  Start screen, the game loop, user input, line removal, scoring, increasing levels of difficulty, game over, high score list. Everything. All in main (). I cannot stress how convoluted this was.  The code indenting was so deep that if you scrolled halfway down the (terrifyingly long) page, you would see nothing but blank space. The line indents were all the way off the right side of the screen. You had to scroll a full page width horizontally in order to see the code. <\/p>\n<p>I finally got my hands on real C code and learned how things are supposed to work.  I wrote and re-wrote Tetris a few times over the next few years.  I had a friend who would put the new versions up on a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bulletin_board_system\">BBS<\/a> for me.  When you exited the game, it printed out my real, actual home address and invited you to send a postcard.  (I got three. Furthest away was from Japan.  I was so excited. <em>I communicated with someone in Japan!<\/em>  I&#8217;m sure I do that every single day now.)<\/p>\n<p>The program and the source vanished ages ago in one of my early computer migrations. Shame. I would pay good money to see the no-function version of the Tetris source again, if only to gape at the horrifying awfulness of the thing.<\/p>\n<p>On a whim, I Googled around to see if it had made the jump from BBS to internet. It did:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/shamus_tetris.png' class='insetimage'   alt='shamus_tetris.png' title='shamus_tetris.png'\/><\/td><\/tr><\/table><\/p>\n<p>That&#8217;s a 16 color graphics mode.  I made it look like it had more colors by using dithering. (I was SO proud of figuring that out.  Wrote a whole bunch of code to do smooth dithered fades. Spent ages on it, experimenting and looking for ways to make the blend as natural as possible. But the whole system was obsolete before I wrote the first line of code. 256 color mode was all the rage and 16 bit color mode was on the horizon.)<\/p>\n<p>The program was more future proof than many.  It still runs on my computer and is still playable. The only thing that breaks is that there was no CPU throttle on the PC speaker based sound effects, so the sounds are reduced to almost completely inaudible millisecond long blips. (Sounds work right under <a href=\"http:\/\/www.dosbox.com\/\">Dosbox<\/a>, though.)<\/p>\n<p>You can get it <a href=\"http:\/\/www.shamusyoung.com\/files\/tetris.zip\">here<\/a>.<\/p>\n<p>Controls:<\/p>\n<p>Arrow keys.  And you use the &lt; and &gt; keys for rotate left and right. For some reason.<\/p>\n<p>And the source is gone.  If it doesn&#8217;t work for you, then there is nothing to be done. Good luck.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In 1990 I was given my first IBM PC as a graduation gift. The machine came with a copy of Borland Turbo C, which was a programming environment. I didn&#8217;t know the C programming language at the time. Before then, I&#8217;d been using Basic. I realized that Basic wasn&#8217;t going to cut it in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-7410","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/7410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7410"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/7410\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}