DevLabs > DevLabs Forums > Small Basic > Twenty Five Line Challenge
Ask a questionAsk a question
 

StickyTwenty Five Line Challenge

  • Saturday, October 17, 2009 2:44 AMCoding CatAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Working on developing projects for my Intro to Programming class, I reminded myself of an article I read back in the early days of the PC. The article was in one of the first PC only magazines, and it was publishing the results of a contest the magazine sponsored. Preliminaries complete, here is what they did:

    They challenged their readers to come up with the most useful BASIC programs they could using only a single line of code in the BASIC interpreter. With the colon instruction separator you could easily get twenty commands on a single line. And the winning submissions were brilliant. One was a screen saver-esk art program, a second was a working word processor, and the third was a full sub chase game. I wish I could share the article and results with this group, but my net search for mention of the article turned up nothing.

    So, in the spirit of the early days of PC programming, here is my proposal to all of you:

    The 25 Line Small Basic Challenge!

    Give us your clever, entertaining, most creative best, but keep it under twenty five lines.

    To get the ball rolling I wrote a text based version of Rock-Paper-Scissors in 24 lines (25 if you count my comment). Import Code: CTB433

All Replies

  • Saturday, October 17, 2009 9:31 PMlitdevAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Entering the spirit here is a swarm of bees at 25 lines, I guess I could loose the window title if it has to be under 25.  Import Code: MSH031.

    • Edited bylitdevAnswererSunday, October 18, 2009 10:49 AMproblem with image download
    • Edited bylitdevAnswererSunday, October 18, 2009 11:01 AM
    • Edited bylitdevAnswererSunday, October 18, 2009 2:12 PMBee image web address fixed
    •  
  • Sunday, October 18, 2009 12:04 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Heres mine: RRS083

    its simulates something like an atom and the mouse is the core. I didnt look at litdevs bee project to make this. my code is really different from his anyway.

    i did this some time ago and just changed it so it fits in 25 lines of code. (the original version looks prettier and runs faster..)

    have fun!

    btw, litdev. does your code run fine on your computer?

    it lags on mine. but runs very good at 50 bees.
    Live for nothing, OR CODE FOR SOMETHING!
  • Sunday, October 18, 2009 7:40 PMGrzegorz TworekMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Simple, nice (it's why I love Small Basic) and working: GQP876
    It draws Lissajous curves.

    Still thinking about something more interactive... ;)
    Grzesio
  • Sunday, October 18, 2009 8:50 PMGrzegorz TworekMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yet another one.
    Paint program: FNW050
    You can paint using your mouse and choose one of the 8 colors from the palette.
    Grzesio
  • Sunday, October 25, 2009 6:17 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    just a little swarm of flies:
    LNR359

    just move the mouse around, and you'll see.

    you can increase the number of flies much more!
    i was too lazy to comment the code this time..

    have fun!
    Live for nothing, OR CODE FOR SOMETHING!
  • Sunday, October 25, 2009 8:38 PMRobiY Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Here's your Rock-Paper-Scissors in 18 lines: CTB433-0
  • Monday, October 26, 2009 4:35 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    heres another version of the swarm of flies. this time i did it with shapes.animating. i removed the zooming, but this version supports about 200 flies without lag on my computer!

    WKN366

    and heres another version, wich runs even faster, but the code is a bit crappier:

    RPW587

    have fun!
    Live for nothing, OR CODE FOR SOMETHING!
  • Friday, October 30, 2009 1:31 PMJason Jacques Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Got Rock/Paper/Scissors down to 9 lines. Import code: CTB433-1

    The code does this by a) utilising the fact that a command window is 80 characters wide, thus concatenating the first three lines of output into a single line of code and b) generating the array dynamically in only five lines.

    You may want to use the array generating trick in your own code for this challenge if you will need an array comprising of more than 5 items.

    Best,

    Jason.

  • Friday, October 30, 2009 2:55 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    wow! nice work man!
    Live for nothing, OR CODE FOR SOMETHING!
  • Friday, October 30, 2009 5:48 PMVijaye RajiMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Very nice, Jason. 
  • Sunday, November 01, 2009 8:27 PMJason Jacques Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    So, my submission to this challenge is a 25 line Sudoku Solver.

    The code does have some limitations:

    1. The solver only works for unambiguous puzzles, where every number can be determined by using the core rules of the game (each row column and three-by-three grid must contain the numbers one through nine once only). The code is iterative rather than recursive and does not try speculative patterns and thus can only solve sudoku puzzles typically classified as easy.
    2. Each puzzle must be serialised for use in the solver. The solver expects an 81 character string (exactly) with no line-endings and spaces representing the squares to be solved.

    The solver works by assuming any square can contain any number one through nine, until it is demonstrated otherwise using the above rules. After checking all the dependant squares the solver determines if there is an unambiguous solution available, and if so applies it, before checking the next square in the grid. The grid is checked 81 times which allows for only a single square to be solved on each pass - if no square is solved on a complete pass of all squares this indicates that the puzzle needs more advanced logic to solve and it is thus safe to terminate regardless.

    If you ignore my gratuitous use of blank lines and the comments (including a couple of additional puzzles to try) the code weighs in at exactly 25 lines. I can shorten it to between 22-24 if required by rearranging the logic and removing an if-statement (making it significantly slower) or removing the blank lines in the output (which make it much more difficult to determine the solution from the steps displayed).

    The solver does always terminate, even if it can not find a comprehensive solution, and displays the incomplete solution that it manages to discern.

    There are a couple of interesting 'cheats' used such as using compound for-loops where the values of what would be nested loops are calculated on the fly using Math.Ceiling() and Math.Remainder() saving around 6 lines of code (at a very high price in performance) and a special or-condition which saves an additional two lines of code in the form of a separate if-statement. I have once again used dynamic generation of the array to create a 9-by-9 grid in only 6 lines of code.

    The solution is highly inefficient, and I suspect some of the Math.Remainder() calls could be factored to reduce their number, but this is the price you pay for brevity of length.

    After that immense build up, the import code is: CGW786

    Hope everyone finds this both amusing and educational.

    Best,

    Jason.
  • Friday, November 06, 2009 9:26 PMCoding CatAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sub Hunt!

    My latest tiny opus: KDN168

    Complete with a graphic playing board. Be patient with the picture rendering, the photos have to be repeatedly loaded from flickr as there was no room for optimization.

    I found that writing this was highly entertaining. I hope you enjoy playing it and marveling at the ridiculously long lines of code.


  • Saturday, November 07, 2009 3:04 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    wow! really nice!

    would have been nice if you could control it with the mouse...
    Live for nothing, OR CODE FOR SOMETHING!
  • Sunday, November 08, 2009 10:56 PMDavey-Wavey Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    wow! really nice!

    would have been nice if you could control it with the mouse...
    Live for nothing, OR CODE FOR SOMETHING!

    I've re-written Coding Cat's Sub Hunt to work with the mouse. :-)

    Import code: KDN168-0


    Ideally, I'd also swap the 'patrol area' to be a single 400x400 image and use one of the spare lines to show how many depth charges are left, which I couldn't do in this version.

    I also removed the line that moves the sub when you get a narrow miss.
  • Monday, November 09, 2009 5:50 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    cool!

    nice work!
    Live for nothing, OR CODE FOR SOMETHING!
  • Tuesday, November 10, 2009 6:45 PMCoding CatAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Very nice job of one upping me DW. :-)

    This is a lesson in tunnel vision and the need for code reviews. The original version I wrote was recreating a text based game from my early days on the PC. All of the changes I made were a play on that idea and an attempt to cram code into 25 lines. The idea using the mouse and loading the pictures as single pieces didn't even occur to me.

    I have put my current 25 line project on hold and have already started playing with your updated code. I should have a version with full mouse support and faster graphics in a few days. I should be able to drop the code down to about 20 lines... or add more to the game.

  • Tuesday, November 10, 2009 8:06 PMRushworksAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Here's a tip for you 25-liners:

      You can create an array in one line

    'setup card array
    cardValues = "1=One;2=Two;3=Three;4=Four;5=Five;6=Six;7=Seven;8=Eight;9=Nine;10=Ten;11=Jack;12=Queen;13=King"
    
    suit = "1= of Hearts;2= of Spades;3= of Diamonds;4= of Clubs"
    
    For I = 1 To 13
      r = Math.GetRandomNumber(4)
      TextWindow.WriteLine(cardValues[I] + suit[r])
    EndFor
    
    
  • Tuesday, November 10, 2009 8:55 PMVijaye RajiMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Good trick, Rushworks!
  • Wednesday, November 11, 2009 1:49 AMDavey-Wavey Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hehe, hope you didn't mind Cool Cat.

    Actually, I realised yesterday that the code I uploaded isn't quite correct as there can only ever be two results: Either 'dpthChrg=10' or 'dpthChrg=11'. I'd coded it to allow for any number of remaining depth charges from 1 to 9, but as it's always set to 11 when the sub is hit, it's irrelevant.

    This means that the following two lines should be changed from my published version to these...

    finishMsg="10 "+Text.GetCharacter(10)+Text.GetCharacter(10)+Text.GetCharacter(10)+"   Depth charges depleted."+Text.GetCharacter(10)+"   The sub has escaped."+Text.GetCharacter(10)+"   You are responsible for the deaths of 5 billion people." + Text.GetCharacter(10) + Text.GetCharacter(10) + "11 " + Text.GetCharacter(10)+Text.GetCharacter(10)+Text.GetCharacter(10)+"   Bull's Eye!" + Text.GetCharacter(10)+"   You sank the sub." + Text.GetCharacter(10)+"   Billions praise you as the savior of humanity." +Text.GetCharacter(10)+Text.GetCharacter(10)

    and

    TextWindow.WriteLine( Text.GetSubText(finishMsg, text.GetIndexOf(finishMsg, dpthChrg)+2, 114)) 'Describe the outcome. Did we get the sub? Did the sub escape?

    Also, if you're re-doing the graphics, you could remove the instructions going to the text window and include them in the graphic which will save another line of code. In fact, with these savings, you should be able to do away with the text window altogether :-)

    Anyway, have fun!


    @RushWorks... cool, thanks for that array tip. Duh, I'd noticed arrays were stored like that, but it hadn't clicked to actually create them that way.
  • Wednesday, November 11, 2009 3:24 AMDavey-Wavey Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Got Rock/Paper/Scissors down to 9 lines. Import code: CTB433-1

    The code does this by a) utilising the fact that a command window is 80 characters wide, thus concatenating the first three lines of output into a single line of code and b) generating the array dynamically in only five lines.

    You may want to use the array generating trick in your own code for this challenge if you will need an array comprising of more than 5 items.

    Best,

    Jason.

    Hehe, Jason, I got your Rock/Paper/Scissors down to 3 lines.

    Import code: CTB433-2
  • Wednesday, November 11, 2009 3:36 AMVijaye RajiMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hehe, Jason, I got your Rock/Paper/Scissors down to 3 lines.

    Import code: CTB433-2
    Wow!
  • Wednesday, November 11, 2009 3:49 AMCoding CatAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes, Wow. I was thinking on my drive home tonight that I could get the RPS code down to five or six lines by doing away with the array and accessing a string containing the values directly using the text commands. But this beats my idea.

    So you definitely win... This round. ;-)


    This exercise has reminded me of the programmers theorem:

    1) Every program can be reduced by at least one line
    2) Every program has at least one bug.

    By induction:
    -- Every program can be reduced to one line; which will be wrong.


    :-D
  • Wednesday, November 11, 2009 6:24 AMGrzegorz TworekMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    By induction:
    -- Every program can be reduced to one line; which will be wrong.

    And then to zero lines which makes it 100% bugless ;))
    Grzesio
  • Wednesday, November 11, 2009 4:18 PMDavey-Wavey Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    By induction:
    -- Every program can be reduced to one line; which will be wrong.

    And then to zero lines which makes it 100% bugless ;))
    Grzesio

    ROCK-PAPER-SCISSORS, again. No, I haven't improved on 3 lines, lol, although I tried hard.

    As this forum is about learning as well as helping, I've published the R-P-S again but with comments to describe what is happening.

    New import code: CTB433-3
  • Wednesday, November 11, 2009 11:14 PMDavey-Wavey Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    SBace-Invaders-25 : Import code: XDK444

    Here it is, Space Invader(s) in 20 lines!

    3 lives, increasing difficulties, skill ratings, awesome graphics ;-)

    What more can you ask for in under 25 lines?  Well, I've left you 4 lines to add your own features (possibly add more aliens, or make them fire back?). Enjoy.

    *EDIT* See below for updated version.
    • Edited byDavey-Wavey Tuesday, November 17, 2009 1:03 AMNew version added below.
    •  
  • Saturday, November 14, 2009 4:21 AMJason Jacques Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Davey-Wavey,

    While I do think your solution was utterly brilliant, not to be out done here is Rock-Paper-Scissors in only two lines: CTB433-5

    I do note that my 'solution' is possibly the most manual and verbose way of coding the game, but hey, it's vertically very short!

    Even though I doubt it can be done (due to the write-read-write cycle required), I'd love to see a one line version!

    As an aside, Rushworks, the manual serialisation of the arrays is a wonderful discovery and while I won't be using it in any of my normal coding (I doubt it is a 'supported' array generation technique) I am extremely impressed by its brevity.

    Best,

    Jason.
  • Saturday, November 14, 2009 4:14 PMDavey-Wavey Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Davey-Wavey,

    While I do think your solution was utterly brilliant, not to be out done here is Rock-Paper-Scissors in only two lines: CTB433-5



    Ha Ha, absolutely awesome, well done Jason.
  • Sunday, November 15, 2009 12:42 AMVijaye RajiMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Davey-Wavey,

    While I do think your solution was utterly brilliant, not to be out done here is Rock-Paper-Scissors in only two lines: CTB433-5



    Ha Ha, absolutely awesome, well done Jason.

    You guys are absolutely incredible.  I had never imagined this being possible in Small Basic :)

  • Sunday, November 15, 2009 12:53 AMVijaye RajiMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
  • Tuesday, November 17, 2009 12:53 AMDavey-Wavey Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well, feeling inspired by our new found stardom in Vijaye's blog, I went back to play with my SBaceInvaders-25, and I proudly present...

    SBace-Invaders-25 v2:

    Now there's 5 aliens to pit your skills against, and they're not just sitting there, they're coming to get ya!
    I've sorted the bullet 'bug' (well, it bugged me) so now you can only kill one alien with each bullet.

    Oh yeah, and I figured that I'd cut it down to only 18 lines of code while I was at it, hehe, so you've got plenty of room to add new features.

    See v2 here: http://smallbasic.com/program/?SPR072 - it is heavily commented, which I'm not counting in the '18' lines.

    Or import it to play: SPR072
  • Tuesday, November 17, 2009 8:09 PMCoding CatAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Not wanting to be left out of the festivities I hereby submit the update for Sub Hunt. Version 2.0 expands on DW's idea of using the mouse to hunt down the sub. Also, everything is image based, the pictures load in sheets rather than tiles,  and transparency in png files is used to layer the splashes. All of this does away with the textwindow entirely.

    Import: KDN168-1

    I also posted a fully commented version of the game with four extra lines that separates out the math making the logic easier to follow.

    Import KDN168-2

    Enjoy... on to my next idea.
  • Tuesday, November 17, 2009 8:39 PMThirdMagus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Ah, this is great! Congratulations on the amazing entries so far!

    Here's my submission: CCN060

    It's a 24 line 3D renderer that displays a rotating triangle based model.
    As an example I've included the definition of a triangular piramid, mostly because it's the 3D shape with the fewest triangles ;) . Theoretically it can draw any convex model though, so I'll try and make something a bit more interresting.

    Edit: Make that any convex model. There's no hidden surface removal, obviously.

    Update:

    Image and video hosting by TinyPic

    Here's a new version containing a (somewhat glitchy) model of the Arwing from StarFox (StarWing in Europe): JNV862 (updated )

    As I mentioned above, the program can only render convex models, i.e. no part of the model may overlap with another. The Arwing model does not meet this requirement. By carefully sorting the triangle list I've made sure that the inner triangles of the blue sections (the triangles facing towards the main body of the ship) are draw first, then the grey body, and lastly the outer triangles of the blue sections (the triangles facing away from the main body). This means that the innner triangles of the blue sections are always overdrawn by the body of the ship, and the body of the ship always overdrawn by the outer triangles of the blue sections. A sort of primitive Painter's Algorithm. This approach is by no means perfect, as you can see from the glitchy result, but at least it doesn't even require a single line of code ;) .
    • Edited byThirdMagus Wednesday, November 18, 2009 9:36 PMTypo in source
    • Edited byThirdMagus Thursday, November 19, 2009 8:00 PM
    •  
  • Thursday, November 19, 2009 6:22 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    now this sucks.


    just kidding! this is one of the coolest projects ive seen so far! this beats the pants off  my 3d projects!
    really awesome!
    Live for nothing, OR CODE FOR SOMETHING!
  • Thursday, November 19, 2009 7:58 PMThirdMagus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Of course, getting my matrix multiplications right does help. Here's another update (hopefully the last): JNV862

    Thanks to me screwing up basic linear algebra the previous version didn't actually do any perspective projecting... I messed around with the model and viewer distances as well, I think it looks okay now. And finally I've increased the window size to 800 x 600.
  • Saturday, November 21, 2009 8:05 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I just made an Imagesaver, wich saves an image to a textfile, wich can be loaded with the imageloader.

    Imagesaver:
    VQS181 (the imagesaver saves an image to a textfile, wich can be loaded with the imageloader after that. and it can have much less lines, but i didnt want to make it have less lines...)

    Imageloader:
    SKK869 (WARNING! YOU NEED TO HAVE RUNNED THE IMAGESAVER AT LEAST ONCE FOR THIS TO WORK! i know, when you zoom the image, it becomes "transparent". thats because i didnt code it to duplicate the pixels. maybe someone wants to?^^)


    Live for nothing, OR CODE FOR SOMETHING!
  • Thursday, November 26, 2009 6:26 PMDudeson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    is this thread dead or what?
    Live for nothing, OR CODE FOR SOMETHING!
  • 15 hours 31 minutes agoViperTompkinz771 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am entering my pebble guess game, it is very basic, i am still a very novice programmer, around 20 lines i believe
    code MRR315
    it is, like i said, very basic